URLSessionWebSocketTask

Every time I try using the new URLSessionWebSocketTask I eventually receive an error that crashes my app.


Fatal error: Only one of message or error should be nil: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang_overlay_Foundation_Device/swiftlang-1100.2.32.33/swift/stdlib/public/Darwin/Foundation/URLSession.swift, line 46


Has anyone experienced this issue? If so, do you know what's causing the problem?


Development Setup:

macOS 10.14.5 - Xcode 11 beta

iPhone 8 Plus - iOS 13 beta

The version of Swift in the first Xcode 11 beta appears to be swiftlang-1100.0.38.29. I'm assuming Apple put the wrong version of Swift 5.1 beta in Xcode.

I've been getting this error randomly since I recently used URLSessionWebSocketTask, which causes my program to crash every time:


Fatal error: Only one of message or error should be nil


But I can't find a specific way to reproduce it, because I have no idea. It doesn't seem to be caused by network quality or any unsafe code I wrote.


So I'd like to ask, have you guys encountered this error recently? I need to know if it's caused by the SDK.


Xcode 11.3.1 (11C504)

macOS 10.15.3 (19D76)

iOS 13.3.1 (17D50)

I've changed my WebSocket implementation to URLSessionWebSocketTask and can see the same issue in the crash reporting tool.

No clear way to reproduce the issue yet. Here is some stack trace if maybe it can help.


Crashed: NSOperationQueue 0x15fd97930 (QOS: UNSPECIFIED)
0  libswiftCore.dylib             0x1b8317aa4 specialized _assertionFailure(_:_:file:line:flags:) + 440
1  libswiftCore.dylib             0x1b813471c _assertionFailure(_:_:file:line:flags:) + 32
2  libswiftFoundation.dylib       0x1e0700644 partial apply for closure #1 in NSURLSessionWebSocketTask.receive(completionHandler:) + 404
3  libswiftFoundation.dylib       0x1e06a2e30 thunk for @escaping @callee_guaranteed (@guaranteed NSURLSessionWebSocketMessage?, @guaranteed Error?) -> () + 80
4  Foundation                     0x1ab21f3a0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
5  Foundation                     0x1ab1290c8 -[NSBlockOperation main] + 100
6  Foundation                     0x1ab221628 __NSOPERATION_IS_INVOKING_MAIN__ + 20
7  Foundation                     0x1ab128d60 -[NSOperation start] + 732
8  Foundation                     0x1ab222020 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 20
9  Foundation                     0x1ab221aec __NSOQSchedule_f + 180
10 libdispatch.dylib              0x1aaad97dc _dispatch_block_async_invoke2 + 104
11 libdispatch.dylib              0x1aab27184 _dispatch_client_callout + 16
12 libdispatch.dylib              0x1aaacfa3c _dispatch_continuation_pop$VARIANT$mp + 412
13 libdispatch.dylib              0x1aaacf190 _dispatch_async_redirect_invoke + 600
14 libdispatch.dylib              0x1aaadbfa4 _dispatch_root_queue_drain + 376
15 libdispatch.dylib              0x1aaadc770 _dispatch_worker_thread2 + 128
16 libsystem_pthread.dylib        0x1aab76b48 _pthread_wqthread + 212
17 libsystem_pthread.dylib        0x1aab79760 start_wqthread + 8

Xcode 11.3.1 (11C504)

iOS 13.3.1 (17D50)

@admglo, did you found solution? I got mostly the same stack trace report, can't figure out what is causing it.
We see this exact issue in our integration tests, which run against a simple Phoenix WebSocket server running locally on the test machine. We do not see an issue when using Starscream as our WebSocket implementation.

We haven't been able to reproduce it reliably, but, occasionally when running our integration tests locally, it happens, and I'm able to catch the exception breakpoint.

This is the backtrace we see:

Code Block
* thread #2, queue = 'NSOperationQueue 0x100b77a10 (QOS: UNSPECIFIED)', stop reason = Fatal error: Only one of message or error should be nil
* frame #0: 0x00007fff71109380 libswiftCore.dylib`_swift_runtime_on_report
frame #1: 0x00007fff71183243 libswiftCore.dylib`_swift_stdlib_reportFatalErrorInFile + 211
frame #2: 0x00007fff70e4a9de libswiftCore.dylib`closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 286
frame #3: 0x00007fff70e4a5e7 libswiftCore.dylib`closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 87
frame #4: 0x00007fff70e4abd7 libswiftCore.dylib`function signature specialization <Arg[1] = [Closure Propagated : closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never, Argument Types : [Swift.StaticStringSwift.UnsafeBufferPointer<Swift.UInt8>Swift.UIntSwift.UInt32]> of generic specialization <()> of Swift.String.withUTF8<A>((Swift.UnsafeBufferPointer<Swift.UInt8>) throws -> A) throws -> A + 183
frame #5: 0x00007fff70e491c0 libswiftCore.dylib`Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 528
frame #6: 0x00007fff713b9a4a libswiftFoundation.dylib`partial apply forwarder for closure #1 (Swift.Optional<C.NSURLSessionWebSocketMessage>, Swift.Optional<Swift.Error>) -> () in (extension in Foundation):C.NSURLSessionWebSocketTask.receive(completionHandler: (Swift.Result<(extension in Foundation):C.NSURLSessionWebSocketTask.Message, Swift.Error>) -> ()) -> () + 458
frame #7: 0x00007fff71367fc7 libswiftFoundation.dylib`reabstraction thunk helper from @escaping @callee_guaranteed (@guaranteed Swift.Optional<C.NSURLSessionWebSocketMessage>, @guaranteed Swift.Optional<Swift.Error>) -> () to @escaping @callee_unowned @convention(block) (@unowned Swift.Optional<C.NSURLSessionWebSocketMessage>, @unowned Swift.Optional<C.NSError>) -> () + 71
frame #8: 0x00007fff39f1aba5 Foundation`NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 7
frame #9: 0x00007fff39f1aac6 Foundation`-[NSBlockOperation main] + 80
frame #10: 0x00007fff39f1aa61 Foundation`NSOPERATION_IS_INVOKING_MAIN + 17
frame #11: 0x00007fff39f19c93 Foundation`-[NSOperation start] + 722
frame #12: 0x00007fff39f199b9 Foundation`NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION + 17
frame #13: 0x00007fff39f19889 Foundation`__NSOQSchedule_f + 182
frame #14: 0x00007fff717a02b9 libdispatch.dylib`_dispatch_block_async_invoke2 + 83
frame #15: 0x00007fff71794658 libdispatch.dylib`_dispatch_client_callout + 8
frame #16: 0x00007fff71796818 libdispatch.dylib`_dispatch_continuation_pop + 414
frame #17: 0x00007fff71795f16 libdispatch.dylib`_dispatch_async_redirect_invoke + 703
frame #18: 0x00007fff717a2957 libdispatch.dylib`_dispatch_root_queue_drain + 326
frame #19: 0x00007fff717a3097 libdispatch.dylib`_dispatch_worker_thread2 + 92
frame #20: 0x00007fff719ee9f7 libsystem_pthread.dylib`_pthread_wqthread + 220
frame #21: 0x00007fff719edb77 libsystem_pthread.dylib`start_wqthread + 15

Now I have the same problem you have. How did you solve the crash

URLSessionWebSocketTask
 
 
Q