Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

ASWebAuthenticationSession crash after window closes on macOS

I'm trying to use ASWebAuthenticationSession on macOS but there is a weird crash and I have no idea what to do. It looks like there is a main thread check in a framework code that I have no control over.

Any help would be appreciated. Thank you in advance.

The stack of crashed thread has no symbols, even for supposedly my code in OAuthClient.authenticate.

macOS 15.4.1 (24E263) Xcode Version 16.3 (16E140)

Thread 11: EXC_BREAKPOINT (code=1, subcode=0x10039bb04)

Thread 12 Queue : com.apple.NSXPCConnection.m-user.com.apple.SafariLaunchAgent (serial)
#0	0x0000000100b17b04 in _dispatch_assert_queue_fail ()
#1	0x0000000100b52834 in dispatch_assert_queue$V2.cold.1 ()
#2	0x0000000100b17a88 in dispatch_assert_queue ()
#3	0x000000027db5f3e8 in swift_task_isCurrentExecutorWithFlagsImpl ()
#4	0x00000001022c7754 in closure #1 in closure #1 in OAuthClient.authenticate() ()
#5	0x00000001022d0c98 in thunk for @escaping @callee_guaranteed (@in_guaranteed URL?, @guaranteed Error?) -> () ()
#6	0x00000001c7215a34 in __102-[ASWebAuthenticationSession initWithURL:callback:usingEphemeralSession:jitEnabled:completionHandler:]_block_invoke ()
#7	0x00000001c72163d0 in -[ASWebAuthenticationSession _endSessionWithCallbackURL:error:] ()
#8	0x00000001c7215fc0 in __43-[ASWebAuthenticationSession _startDryRun:]_block_invoke_2 ()
#9	0x0000000194e315f4 in __invoking___ ()
#10	0x0000000194e31484 in -[NSInvocation invoke] ()
#11	0x00000001960fd644 in __NSXPCCONNECTION_IS_CALLING_OUT_TO_REPLY_BLOCK__ ()
#12	0x00000001960fbe40 in -[NSXPCConnection _decodeAndInvokeReplyBlockWithEvent:sequence:replyInfo:] ()
#13	0x00000001960fb798 in __88-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]_block_invoke_3 ()
#14	0x0000000194a6ef18 in _xpc_connection_reply_callout ()
#15	0x0000000194a6ee08 in _xpc_connection_call_reply_async ()
#16	0x0000000100b3130c in _dispatch_client_callout3_a ()
#17	0x0000000100b362f8 in _dispatch_mach_msg_async_reply_invoke ()
#18	0x0000000100b1d3a8 in _dispatch_lane_serial_drain ()
#19	0x0000000100b1e46c in _dispatch_lane_invoke ()
#20	0x0000000100b2bfbc in _dispatch_root_queue_drain_deferred_wlh ()
#21	0x0000000100b2b414 in _dispatch_workloop_worker_thread ()
#22	0x0000000100c0379c in _pthread_wqthread ()

My code:

    @MainActor
    func authenticate() async throws {
        let authURL = api.authorizationURL(
            scopes: scopes,
            state: state,
            redirectURI: redirectURI
        )
        
        let authorizationCodeURL: URL =  try await withUnsafeThrowingContinuation { c in
            let session = ASWebAuthenticationSession(url: authURL, callback: .customScheme(redirectScheme)) { url, error in
                    guard let url = url else {
                        c.resume(throwing: error ?? Error.unknownError("Failed to get authorization code"))
                        return
                    }
                    
                    c.resume(returning: url)
            }
            session.presentationContextProvider = presentationContextProvider
            session.start()
        }

        let authorizationCode = try codeFromAuthorizationURL(authorizationCodeURL)

        (storedAccessToken, storedRefreshToken) = try await getTokens(authorizationCode: authorizationCode)
    }

Here is disassembly of the crashed function.

libdispatch.dylib`_dispatch_assert_queue_fail:
    0x10067fa8c <+0>:   pacibsp 
    0x10067fa90 <+4>:   sub    sp, sp, #0x50
    0x10067fa94 <+8>:   stp    x20, x19, [sp, #0x30]
    0x10067fa98 <+12>:  stp    x29, x30, [sp, #0x40]
    0x10067fa9c <+16>:  add    x29, sp, #0x40
    0x10067faa0 <+20>:  adrp   x8, 71
    0x10067faa4 <+24>:  add    x8, x8, #0x951            ; "not "
    0x10067faa8 <+28>:  adrp   x9, 70
    0x10067faac <+32>:  add    x9, x9, #0x16b            ; ""
    0x10067fab0 <+36>:  stur   xzr, [x29, #-0x18]
    0x10067fab4 <+40>:  cmp    w1, #0x0
    0x10067fab8 <+44>:  csel   x8, x9, x8, ne
    0x10067fabc <+48>:  ldr    x10, [x0, #0x48]
    0x10067fac0 <+52>:  cmp    x10, #0x0
    0x10067fac4 <+56>:  csel   x9, x9, x10, eq
    0x10067fac8 <+60>:  stp    x9, x0, [sp, #0x10]
    0x10067facc <+64>:  adrp   x9, 71
    0x10067fad0 <+68>:  add    x9, x9, #0x920            ; "BUG IN CLIENT OF LIBDISPATCH: Assertion failed: "
    0x10067fad4 <+72>:  stp    x9, x8, [sp]
    0x10067fad8 <+76>:  adrp   x1, 71
    0x10067fadc <+80>:  add    x1, x1, #0x8eb            ; "%sBlock was %sexpected to execute on queue [%s (%p)]"
    0x10067fae0 <+84>:  sub    x0, x29, #0x18
    0x10067fae4 <+88>:  bl     0x1006c258c               ; symbol stub for: asprintf
    0x10067fae8 <+92>:  ldur   x19, [x29, #-0x18]
    0x10067faec <+96>:  str    x19, [sp]
    0x10067faf0 <+100>: adrp   x0, 71
    0x10067faf4 <+104>: add    x0, x0, #0x956            ; "%s"
    0x10067faf8 <+108>: bl     0x1006b7b64               ; _dispatch_log
    0x10067fafc <+112>: adrp   x8, 108
    0x10067fb00 <+116>: str    x19, [x8, #0x2a8]
->  0x10067fb04 <+120>: brk    #0x1

I managed to get rid of the crash by taking out the continuation into a nonisolated function, but I don't know why it helped and what the root cause of the issue is.

ASWebAuthenticationSession crash after window closes on macOS
 
 
Q