capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

[Bug]: iOS crash in Http Plugin

Open JosefBredereck opened this issue 1 year ago • 13 comments

Capacitor Version

Config: {
  appId: 'de.company.appid.mobile',
  appName: 'App Name',
  webDir: 'dist/apps/app/browser',
  backgroundColor: '#003399',
  loggingBehavior: 'none',
  android: {
    path: './native/android',
    webContentsDebuggingEnabled: false
  },
  ios: {
    path: './native/ios',
    webContentsDebuggingEnabled: false
  },
  server: { androidScheme: 'https' },
  includePlugins: [
    '@capacitor/app',
    '@capacitor/browser',
    '@capacitor/device',
    '@capacitor/filesystem',
    '@capacitor/keyboard',
    '@capacitor/network',
    '@capacitor/preferences',
    '@capacitor/splash-screen',
    '@capacitor/camera',
  ],
  plugins: {
    Keyboard: { resize: 'none' },
    SplashScreen: {
      launchShowDuration: 700,
      launchAutoHide: true,
      androidScaleType: 'CENTER_INSIDE',
      backgroundColor: '#1a3691'
    },
    CapacitorHttp: { enabled: true }
  }
}

💊 Capacitor Doctor 💊

Latest Dependencies:

  @capacitor/cli: 6.0.0
  @capacitor/core: 6.0.0
  @capacitor/android: 6.0.0
  @capacitor/ios: 6.0.0

Installed Dependencies:

  @capacitor/cli: 5.6.0
  @capacitor/core: 5.7.4
  @capacitor/android: 5.7.4
  @capacitor/ios: 5.7.4

Other API Details

npm: 10.2.4
node: v21.6.1

Platforms Affected

  • [X] iOS
  • [ ] Android
  • [ ] Web

Current Behavior

We observed an issue on iOS. At random, the app crashes in an Angular application when requests are fired and aborted. The question remains what exactly causes the issue, we could not create a successful reproduction. Sometimes it happens when a request gets unsubscribed (aborted) and sometimes when the request resolves with an error.

scheme stop
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This task has already been stopped'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000111da7761 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x000000010d3e3904 objc_exception_throw + 48
	2   CoreFoundation                      0x0000000111da763f -[NSException initWithCoder:] + 0
	3   WebKit                              0x000000010e042658 -[WKURLSchemeTaskImpl didReceiveResponse:] + 141
	4   Capacitor                           0x000000010a05272d $s9Capacitor19WebViewAssetHandlerC06handleA11HttpRequestyySo15WKURLSchemeTask_p_10Foundation3URLVSbtFyAF4DataVSg_So13NSURLResponseCSgs5Error_pSgtYbcfU_ + 2621
	5   Capacitor                           0x000000010a0529bd $s9Capacitor19WebViewAssetHandlerC06handleA11HttpRequestyySo15WKURLSchemeTask_p_10Foundation3URLVSbtFyAF4DataVSg_So13NSURLResponseCSgs5Error_pSgtYbcfU_TA + 93
	6   Capacitor                           0x000000010a01d60c $s10Foundation4DataVSgSo13NSURLResponseCSgs5Error_pSgIeghggg_So6NSDataCSgAGSo7NSErrorCSgIeyBhyyy_TR + 252
	7   CFNetwork                           0x000000010f728698 CFNetwork + 38552
	8   CFNetwork                           0x000000010f7461b1 _CFHTTPMessageSetResponseProxyURL + 16951
	9   libdispatch.dylib                   0x0000000113092747 _dispatch_call_block_and_release + 12
	10  libdispatch.dylib                   0x00000001130939f7 _dispatch_client_callout + 8
	11  libdispatch.dylib                   0x000000011309b8c9 _dispatch_lane_serial_drain + 1127
	12  libdispatch.dylib                   0x000000011309c665 _dispatch_lane_invoke + 441
	13  libdispatch.dylib                   0x00000001130a976e _dispatch_root_queue_drain_deferred_wlh + 318
	14  libdispatch.dylib                   0x00000001130a8b69 _dispatch_workloop_worker_thread + 590
	15  libsystem_pthread.dylib             0x00000001158aeb84 _pthread_wqthread + 327
	16  libsystem_pthread.dylib             0x00000001158adacf start_wqthread + 15
)
libc++abi: terminating due to uncaught exception of type NSException

The actual position where the crash happens is https://github.com/ionic-team/capacitor/blob/3c411fd0a400b18c2d032e3b8be3fc4b5449bcbd/ios/Capacitor/Capacitor/WebViewAssetHandler.swift#L184

Based on https://stackoverflow.com/a/73581176 a possible solution could be to not abort the request like you would in the web environment.

Maybe the solution Flutter has implemented could give a possible direction: https://github.com/arnaudelub/flutter_polywebview/blob/76def884f29ac13065a538a10789b761c168c5ff/ios/Classes/CustomeSchemeHandler.swift

Expected Behavior

The app does not do a hard crash.

Project Reproduction

Repo: capacitor-issue-7404

Additional Information

No response

JosefBredereck avatar Apr 16 '24 07:04 JosefBredereck

Can confirm. We're getting the same error when requests get aborted.

foo-cb avatar Apr 17 '24 10:04 foo-cb

This issue needs more information before it can be addressed. In particular, the reporter needs to provide a minimal sample app that demonstrates the issue. If no sample app is provided within 15 days, the issue will be closed. Please see the Contributing Guide for how to create a Sample App. Thanks! Ionitron 💙

ionitron-bot[bot] avatar Apr 23 '24 13:04 ionitron-bot[bot]

Aborted requests also cause hard crashes on iOS, have had to fully disable the HTTP plugin.

If you want to reproduce, create a very simple project with CapacitorHTTP wrapping true in the config + Axios + AxiosAbortSignals

If you make a request and then cancel it using a signal you get a crash with the error message referring to the fact the request is already cancelled.

I've noticed a ton of instability with the CapHTTP plugin in the last couple of releases so it may be an area that needs a bit of love and maybe bolstered testing.

patrickjquinn avatar Apr 24 '24 08:04 patrickjquinn

I created a simple reproduction repository: https://github.com/JosefBredereck/capacitor-issue-7404 Just run npm run build:cap and click the red button.

JosefBredereck avatar Apr 25 '24 11:04 JosefBredereck

This issue has been labeled as type: bug. This label is added to issues that that have been reproduced and are being tracked in our internal issue tracker.

ionitron-bot[bot] avatar Apr 30 '24 17:04 ionitron-bot[bot]

Hello! we have the same problem, this prevents us from using the http plugin completely.

SergioMartinSanchez avatar May 01 '24 18:05 SergioMartinSanchez

We are also running into this same problem.

joelfbarber avatar May 02 '24 13:05 joelfbarber

Hello , We have the same problem. Does deactivating the http plugin temporarily solve your problem?

ZairKSM avatar May 02 '24 16:05 ZairKSM

Hello @JosefBredereck I managed to avoid the hard crash of the APP and get an error more detailed by doing this :

I created the following file in @capacitor/ios/Capacitor/Capacitor folder, This file is used to handle the error correctly

//
//  ExceptionCatcher.h
//

#import <Foundation/Foundation.h>

NS_INLINE NSException * _Nullable tryBlock(void(^_Nonnull tryBlock)(void)) {
    @try {
        tryBlock();
    }
    @catch (NSException *exception) {
        return exception;
    }
    return nil;
}

In the same folder, I've modified the Capacitor.h file and added an import to the new file I've just created, to make it available in the swift code.

Just add #import <Capacitor/ExceptionCatcher.h> to the end of the file.

https://github.com/ionic-team/capacitor/blob/3c411fd0a400b18c2d032e3b8be3fc4b5449bcbd/ios/Capacitor/Capacitor/Capacitor.h#L1-L14

I then modified these 3 calls by putting them in a tryBlock() https://github.com/ionic-team/capacitor/blob/3c411fd0a400b18c2d032e3b8be3fc4b5449bcbd/ios/Capacitor/Capacitor/WebViewAssetHandler.swift#L184 https://github.com/ionic-team/capacitor/blob/3c411fd0a400b18c2d032e3b8be3fc4b5449bcbd/ios/Capacitor/Capacitor/WebViewAssetHandler.swift#L189 https://github.com/ionic-team/capacitor/blob/3c411fd0a400b18c2d032e3b8be3fc4b5449bcbd/ios/Capacitor/Capacitor/WebViewAssetHandler.swift#L193

Like this :

let exception = tryBlock {
   urlSchemeTask.didReceive(data)
}

In my case it gives me this error:

[assertion] Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}>
2024-05-04 21:37:26.046048+0200 App[40914:671298] [ProcessSuspension] 0x10f020480 - ProcessAssertion::acquireSync Failed to acquire RBS assertion 'XPCConnectionTerminationWatchdog' for process with PID=40922, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}
2024-05-04 21:37:26.070055+0200 App[40914:671298] [assertion] Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}>
2024-05-04 21:37:26.070132+0200 App[40914:671298] [ProcessSuspension] 0x10f0207e0 - ProcessAssertion::acquireSync Failed to acquire RBS assertion 'XPCConnectionTerminationWatchdog' for process with PID=40921, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}
2024-05-04 21:37:26.071127+0200 App[40914:671298] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}>
2024-05-04 21:37:26.071177+0200 App[40914:671298] [ProcessSuspension] 0x10f020ba0 - ProcessAssertion::acquireSync Failed to acquire RBS assertion 'WebProcess Suspended Assertion' for process with PID=40921, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}
2024-05-04 21:37:56.026363+0200 App[40914:670854] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service
2024-05-04 21:37:56.050115+0200 App[40914:670854] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service

I hope I've made myself clear. This avoids the hard crash but in my case I'm looking for a better way to correct it.

ZairKSM avatar May 04 '24 20:05 ZairKSM

I would like to prevent the modification of remote files in my project. I appreciate the idea but it's not sufficient for our project.

JosefBredereck avatar May 04 '24 20:05 JosefBredereck

I'd also like to avoid doing it, but I was able to get more details from my error. In my case, it happens when I close my inappbrowser window from @awesome-cordova-plugins/in-app-browser.

ZairKSM avatar May 04 '24 21:05 ZairKSM

same error, any update about this? ty

cstriuli avatar May 07 '24 14:05 cstriuli

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Jun 23 '24 13:06 ionitron-bot[bot]