AzureAD / azure-activedirectory-library-for-objc

The ADAL SDK for Objective C gives you the ability to add support for Work Accounts to your iOS and macOS applications with just a few lines of additional code. This SDK gives your application the full functionality of Microsoft Azure AD, including industry standard protocol support for OAuth2, Web API integration with user level consent, and two factor authentication support.
MIT License
178 stars 113 forks source link

Crash in iOS13 #1455

Closed Don2012 closed 5 years ago

Don2012 commented 5 years ago

Crash in iOS13 when presenting a ViewController, but it is fine in iOS12. Tried ADAL (2.7.9) & ADAL (4.0.0), and all crash

- (void)toast:(NSString*) message {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    alert.modalPresentationStyle = 0;
    [self presentViewController:alert animated:YES completion:nil];
    int duration = 4;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}
2019-08-25 23:11:59.255323+0800 LJT[15367:304991] *** Assertion failure in -[FBSSerialQueue assertOnQueue], /BuildRoot/Library/Caches/com.apple.xbs/Sources/FrontBoardServices_Sim/FrontBoard-610.1/FrontBoardServices/FBSSerialQueue.m:98
2019-08-25 23:12:00.433355+0800 LJT[15367:304991] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'threading violation: expected the main thread'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23b60e3e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff502f3b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23b60bb8 +[NSException raise:format:arguments:] + 88
    3   Foundation                          0x00007fff2560fbb1 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
    4   FrontBoardServices                  0x00007fff36141acd -[FBSSerialQueue assertOnQueue] + 236
    5   FrontBoardServices                  0x00007fff360f17d3 -[FBSSceneImpl updateClientSettings:withTransitionContext:] + 71
    6   FrontBoardServices                  0x00007fff360f1aba -[FBSSceneImpl updateClientSettingsWithTransitionBlock:] + 154
    7   FrontBoardServices                  0x00007fff360f19df -[FBSSceneImpl updateClientSettingsWithBlock:] + 110
    8   UIKitCore                           0x00007fff468b6730 -[FBSScene(UIApp) updateUIClientSettingsWithBlock:] + 160
    9   UIKitCore                           0x00007fff464bc6a1 -[_UISystemAppearanceManager updateScreenEdgesDeferringSystemGestures] + 374
    10  UIKitCore                           0x00007fff46715e54 __70-[UIViewController setNeedsUpdateOfScreenEdgesDeferringSystemGestures]_block_invoke_2 + 118
    11  UIKitCore                           0x00007fff466f43a2 -[UIViewController _updateSystemAppearanceWithRecursionBlock:action:] + 295
    12  UIKitCore                           0x00007fff46715a9b -[UIViewController _setPresentedStatusBarViewController:] + 220
    13  UIKitCore                           0x00007fff46705ed2 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1381
    14  UIKitCore                           0x00007fff467078bd -[UIViewController _presentViewController:withAnimationController:completion:] + 4325
    15  UIKitCore                           0x00007fff4670a137 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 98
    16  UIKitCore                           0x00007fff4670a64f -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 511
    17  UIKitCore                           0x00007fff4670a095 -[UIViewController _presentViewController:animated:completion:] + 187
    18  UIKitCore                           0x00007fff4670a2fc -[UIViewController presentViewController:animated:completion:] + 150
    19  TT                                0x000000010eda5e60 -[TTLoginViewController toast:] + 160
    20  TT                                0x000000010edb4a77 __36-[TTLoginViewController getToken:]_block_invoke_2 + 1255
    21  TT                                0x000000010eddbeb3 __70-[ADAuthenticationRequest(AcquireToken) acquireToken:completionBlock:]_block_invoke + 3059
    22  TT                                0x000000010eddc871 __63-[ADAuthenticationRequest(AcquireToken) validatedAcquireToken:]_block_invoke + 113
    23  TT                                0x000000010eddcc57 __56-[ADAuthenticationRequest(AcquireToken) getAccessToken:]_block_invoke + 263
    24  TT                                0x000000010edc4e19 __40-[ADAcquireTokenSilentHandler getToken:]_block_invoke + 729
    25  TT                                0x000000010edc76a7 -[ADAcquireTokenSilentHandler getAccessToken:] + 871
    26  TT                                0x000000010edc4b02 -[ADAcquireTokenSilentHandler getToken:] + 178
    27  TT                                0x000000010eddcaf4 -[ADAuthenticationRequest(AcquireToken) getAccessToken:] + 484
    28  TT                                0x000000010eddc77b -[ADAuthenticationRequest(AcquireToken) validatedAcquireToken:] + 331
    29  TT                                0x000000010eddc24c __70-[ADAuthenticationRequest(AcquireToken) acquireToken:completionBlock:]_block_invoke.160 + 636
    30  TT                                0x000000010ededf5b __74-[ADAuthorityValidation checkAuthority:validateAuthority:completionBlock:]_block_invoke + 299
    31  TT                                0x000000010edee825 __76-[ADAuthorityValidation validateAADAuthority:requestParams:completionBlock:]_block_invoke_2 + 53
    32  libdispatch.dylib                   0x000000010ffc8df0 _dispatch_call_block_and_release + 12
    33  libdispatch.dylib                   0x000000010ffc9d64 _dispatch_client_callout + 8
    34  libdispatch.dylib                   0x000000010ffcc20b _dispatch_queue_override_invoke + 1022
    35  libdispatch.dylib                   0x000000010ffdb29a _dispatch_root_queue_drain + 351
    36  libdispatch.dylib                   0x000000010ffdbba4 _dispatch_worker_thread2 + 132
    37  libsystem_pthread.dylib             0x00007fff513566b3 _pthread_wqthread + 583
    38  libsystem_pthread.dylib             0x00007fff513563fd start_wqthread + 13
antrix1989 commented 5 years ago

Hi @Don2012, there is no guarantee on which thread completion block will be invoked, so I recommend you wrapping your UI calls into:

dispatch_async(dispatch_get_main_queue(), ^{
    /* Do UI work here */
}); 
mahmutpinarbasi commented 5 years ago

Hi @antrix1989, I'm also having main thread warnings on iOS 13 Simulator after calling

- (void)acquireTokenWithResource:(NSString*)resource
                        clientId:(NSString*)clientId
                     redirectUri:(NSURL*)redirectUri
                 completionBlock:(ADAuthenticationCallback)completionBlock;

This is happening with Xcode 11 Beta-6, Adal 4.0.0

=================================================================
Main Thread Checker: UI API called on a background thread: -[UIViewController init]
PID: 10529, TID: 274678, Thread name: (none), Queue name: com.apple.root.default-qos, QoS: 0
Backtrace:
4   ADAL                                0x000000010e74e1cf -[MSIDWebviewUIController initWithContext:] + 79
5   ADAL                                0x000000010e733f77 -[MSIDOAuth2EmbeddedWebviewController initWithStartURL:endURL:webview:customHeaders:context:] + 535
6   ADAL                                0x000000010e6d9a39 -[MSIDAADOAuthEmbeddedWebviewController initWithStartURL:endURL:webview:customHeaders:context:] + 361
7   ADAL                                0x000000010e6e290e -[MSIDAADWebviewFactory embeddedWebviewSessionFromConfiguration:customWebview:context:] + 430
8   ADAL                                0x000000010e74af81 +[MSIDWebviewAuthorization startEmbeddedWebviewAuthWithConfiguration:oauth2Factory:webview:context:completionHandler:] + 225
9   ADAL                                0x000000010e6c80c4 +[ADWebAuthController(Internal) startWithRequest:promptBehavior:context:completion:] + 2020
10  ADAL                                0x000000010e6a341b -[ADAuthenticationRequest(WebRequest) requestCode:] + 955
11  ADAL                                0x000000010e69b879 -[ADAuthenticationRequest(AcquireToken) requestTokenImpl:] + 1369
12  ADAL                                0x000000010e69b17b -[ADAuthenticationRequest(AcquireToken) requestToken:] + 1563
13  ADAL                                0x000000010e69a768 __63-[ADAuthenticationRequest(AcquireToken) validatedAcquireToken:]_block_invoke + 216
14  ADAL                                0x000000010e69ab27 __56-[ADAuthenticationRequest(AcquireToken) getAccessToken:]_block_invoke + 263
15  ADAL                                0x000000010e682919 __40-[ADAcquireTokenSilentHandler getToken:]_block_invoke + 729
16  ADAL                                0x000000010e68683d -[ADAcquireTokenSilentHandler tryFRT:completionBlock:] + 781
17  ADAL                                0x000000010e686213 -[ADAcquireTokenSilentHandler tryMRRT:] + 755
18  ADAL                                0x000000010e68558d -[ADAcquireTokenSilentHandler getAccessToken:] + 1405
19  ADAL                                0x000000010e682602 -[ADAcquireTokenSilentHandler getToken:] + 178
20  ADAL                                0x000000010e69a9c1 -[ADAuthenticationRequest(AcquireToken) getAccessToken:] + 433
21  ADAL                                0x000000010e69a60b -[ADAuthenticationRequest(AcquireToken) validatedAcquireToken:] + 331
22  ADAL                                0x000000010e699f3c __70-[ADAuthenticationRequest(AcquireToken) acquireToken:completionBlock:]_block_invoke.160 + 636
23  ADAL                                0x000000010e6a8aeb __74-[ADAuthorityValidation checkAuthority:validateAuthority:completionBlock:]_block_invoke + 299
24  ADAL                                0x000000010e6a93b5 __76-[ADAuthorityValidation validateAADAuthority:requestParams:completionBlock:]_block_invoke_2 + 53
25  libdispatch.dylib                   0x0000000110462df0 _dispatch_call_block_and_release + 12
26  libdispatch.dylib                   0x0000000110463d64 _dispatch_client_callout + 8
27  libdispatch.dylib                   0x000000011046620b _dispatch_queue_override_invoke + 1022
28  libdispatch.dylib                   0x000000011047529a _dispatch_root_queue_drain + 351
29  libdispatch.dylib                   0x0000000110475ba4 _dispatch_worker_thread2 + 132
30  libsystem_pthread.dylib             0x00007fff511c26b3 _pthread_wqthread + 583
31  libsystem_pthread.dylib             0x00007fff511c23fd start_wqthread + 13

Basically, [MSIDWebviewUIController initWithContext:] is not being called on main thread.

rossholdway commented 5 years ago

Also running into this issue on iOS 13 with v4.0.3. It seems to cause a significant delay when invoking the prompt and my call to acquireTokenWithResource is already inside dispatch_async(dispatch_get_main_queue()....

dispatch_async(dispatch_get_main_queue(), ^{
    [authContext
    acquireTokenWithResource:resourceId
    clientId:clientId
    redirectUri:redirectUri
    promptBehavior:AD_PROMPT_ALWAYS
    userId:userId
    extraQueryParameters:extraQueryParameters
    completionBlock:^(ADAuthenticationResult *result) {
...

I'm not experienced with Objective C so am unsure of how to debug this futher.

Main Thread Checker: UI API called on a background thread: -[UIViewController init]
PID: 357, TID: 12904, Thread name: (none), Queue name: com.apple.root.default-qos, QoS: 0
Backtrace:
4   ADAL                                0x0000000100654bc0 -[MSIDWebviewUIController initWithContext:] + 92
5   ADAL                                0x000000010063a9b8 -[MSIDOAuth2EmbeddedWebviewController initWithStartURL:endURL:webview:customHeaders:context:] + 472
6   ADAL                                0x00000001005e31ec -[MSIDAADOAuthEmbeddedWebviewController initWithStartURL:endURL:webview:customHeaders:context:] + 344
7   ADAL                                0x00000001005ebf70 -[MSIDAADWebviewFactory embeddedWebviewSessionFromConfiguration:customWebview:context:] + 400
8   ADAL                                0x00000001006513e0 +[MSIDWebviewAuthorization startEmbeddedWebviewAuthWithConfiguration:oauth2Factory:webview:context:completionHandler:] + 212

9   ADAL                                0x00000001005d2510 +[ADWebAuthController(Internal) startWithRequest:promptBehavior:context:completion:] + 1788
10  ADAL                                0x00000001005abefc -[ADAuthenticationRequest(WebRequest) requestCode:] + 880
11  ADAL                                0x00000001005a3ce4 -[ADAuthenticationRequest(AcquireToken) requestTokenImpl:] + 1272
12  ADAL                                0x00000001005a3608 -[ADAuthenticationRequest(AcquireToken) requestToken:] + 1524
13  ADAL                                0x00000001005a2aa0 -[ADAuthenticationRequest(AcquireToken) validatedAcquireToken:] + 448
14  ADAL                                0x00000001005a22e0 __70-[ADAuthenticationRequest(AcquireToken) acquireToken:completionBlock:]_block_invoke.160 + 656
15  ADAL                                0x00000001005b1374 __74-[ADAuthorityValidation checkAuthority:validateAuthority:completionBlock:]_block_invoke + 276
16  ADAL                                0x00000001005b1b30 __76-[ADAuthorityValidation validateAADAuthority:requestParams:completionBlock:]_block_invoke_2 + 56
17  libdispatch.dylib                   0x0000000100c09828 _dispatch_call_block_and_release + 24
18  libdispatch.dylib                   0x0000000100c0ac04 _dispatch_client_callout + 16
19  libdispatch.dylib                   0x0000000100c0d300 _dispatch_queue_override_invoke + 872
20  libdispatch.dylib                   0x0000000100c1c168 _dispatch_root_queue_drain + 376
21  libdispatch.dylib                   0x0000000100c1ca90 _dispatch_worker_thread2 + 156
22  libsystem_pthread.dylib             0x000000018b3a1f64 _pthread_wqthread + 212
23  libsystem_pthread.dylib             0x000000018b3a4ae0 start_wqthread + 8
oldalton commented 5 years ago

Should be fixed in 4.0.4. Please reopen the issue if it still persists in 4.0.4 release.