Posts

Post marked as solved
4 Replies
Your answer was very helpful. Thank you. Our fundamental mistake was, in our custom Subscription, not adding the current demand to the result of Subscriber.receive(_ value: Input) -> Subscribers.Demand. Instead, we were replacing the previous demand with the newly returned value. In the case of Subscribers.Sink, the new value was always .none. So, after initially draining the publisher for the sink (into the sink?), we would never give the sink any new values. As you pointed out, though, we should have added the sink's initial demand of .unlimited to the new value of .none, which would have returned a new demand of .unlimited (infinity + 0 = infinity).
Post not yet marked as solved
6 Replies
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 - https://github.com/daltoniam/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: thread #2, queue = 'NSOperationQueue 0x100b77a10 (QOS: UNSPECIFIED)', stop reason = Fatal error: Only one of message or error should be nil &#9;* frame #0: 0x00007fff71109380 libswiftCore.dylib`_swift_runtime_on_report &#9;&#9;frame #1: 0x00007fff71183243 libswiftCore.dylib`_swift_stdlib_reportFatalErrorInFile + 211 &#9;&#9;frame #2: 0x00007fff70e4a9de libswiftCore.dylib`closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -> () in closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -> () in closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 286 &#9;&#9;frame #3: 0x00007fff70e4a5e7 libswiftCore.dylib`closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -> () in closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 87 &#9;&#9;frame #4: 0x00007fff70e4abd7 libswiftCore.dylib`function signature specialization &lt;Arg[1] = [Closure Propagated : closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8&gt;) -> () in closure #1 (Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never, Argument Types : [Swift.StaticStringSwift.UnsafeBufferPointer&lt;Swift.UInt8&gt;Swift.UIntSwift.UInt32]> of generic specialization &lt;()&gt; of Swift.String.withUTF8&lt;A&gt;((Swift.UnsafeBufferPointer&lt;Swift.UInt8&gt;) throws -> A) throws -> A + 183 &#9;&#9;frame #5: 0x00007fff70e491c0 libswiftCore.dylib`Swift._assertionFailure(_: Swift.StaticString, _: Swift.String, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 528 &#9;&#9;frame #6: 0x00007fff713b9a4a libswiftFoundation.dylib`partial apply forwarder for closure #1 (Swift.Optional&lt;C.NSURLSessionWebSocketMessage&gt;, Swift.Optional&lt;Swift.Error&gt;) -> () in (extension in Foundation):C.NSURLSessionWebSocketTask.receive(completionHandler: (Swift.Result&lt;(extension in Foundation):__C.NSURLSessionWebSocketTask.Message, Swift.Error&gt;) -> ()) -> () + 458 &#9;&#9;frame #7: 0x00007fff71367fc7 libswiftFoundation.dylib`reabstraction thunk helper from @escaping @callee_guaranteed (@guaranteed Swift.Optional&lt;C.NSURLSessionWebSocketMessage&gt;, @guaranteed Swift.Optional&lt;Swift.Error&gt;) -> () to @escaping @callee_unowned @convention(block) (@unowned Swift.Optional&lt;C.NSURLSessionWebSocketMessage&gt;, @unowned Swift.Optional&lt;__C.NSError&gt;) -> () + 71 &#9;&#9;frame #8: 0x00007fff39f1aba5 Foundation`NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 7 &#9;&#9;frame #9: 0x00007fff39f1aac6 Foundation`-[NSBlockOperation main] + 80 &#9;&#9;frame #10: 0x00007fff39f1aa61 Foundation`NSOPERATION_IS_INVOKING_MAIN + 17 &#9;&#9;frame #11: 0x00007fff39f19c93 Foundation`-[NSOperation start] + 722 &#9;&#9;frame #12: 0x00007fff39f199b9 Foundation`NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION + 17 &#9;&#9;frame #13: 0x00007fff39f19889 Foundation`__NSOQSchedule_f + 182 &#9;&#9;frame #14: 0x00007fff717a02b9 libdispatch.dylib`_dispatch_block_async_invoke2 + 83 &#9;&#9;frame #15: 0x00007fff71794658 libdispatch.dylib`_dispatch_client_callout + 8 &#9;&#9;frame #16: 0x00007fff71796818 libdispatch.dylib`_dispatch_continuation_pop + 414 &#9;&#9;frame #17: 0x00007fff71795f16 libdispatch.dylib`_dispatch_async_redirect_invoke + 703 &#9;&#9;frame #18: 0x00007fff717a2957 libdispatch.dylib`_dispatch_root_queue_drain + 326 &#9;&#9;frame #19: 0x00007fff717a3097 libdispatch.dylib`_dispatch_worker_thread2 + 92 &#9;&#9;frame #20: 0x00007fff719ee9f7 libsystem_pthread.dylib`_pthread_wqthread + 220 &#9;&#9;frame #21: 0x00007fff719edb77 libsystem_pthread.dylib`start_wqthread + 15
Post marked as solved
4 Replies
I may have misunderstood the problem. In our app, we connect multiple subscribers to a single PassthroughSubject. Most of the subscribers are instances of Subscribers.Sink, but one is a custom type we created which always requests Demand.unlimited number of values. Having worked with Subscribers.Sink before and based on what we've read in the documentation, Subscribers.Sink requests an unlimited number of values upon subscription but then returns Demand.none from receive(_ value: Input) -> Subscribers.Demand. In practice, this means a sink would drain everything from the publisher when it subscribes to the publisher, but then never receive any more values, even if the publisher keeps producing them. Our custom subscriber type, on the other hand, was designed to keep draining values from the publisher forever. What we expected to see when connecting multiple sinks and our custom subscriber to our publisher was the sinks receiving all of the available values immediately but none after initially draining the publisher while our custom subscriber continued to receive values indefinitely. What we actually observed was all of the subscribers (sinks and our custom subscriber) receiving values from the publisher indefinitely. We thought this was due to PassthroughSubject aggregating the demand from all of the subscribers (0 + 0 + 0 + infinite = infinite). Based on the example code I pasted below, though, it now seems to me like PassthroughSubject doesn't respect the demand of its subscribers. In the example code, we have a StringPublisher which publishes a new UUID string every 500ms through a PassthroughSubject to downstream subscribers. We subscribe to this publisher via Subscribers.Sink. Expected output: receiveValue &lt;UUID&gt; Actual output: receiveValue &lt;UUID&gt; receiveValue &lt;UUID&gt; receiveValue &lt;UUID&gt; receiveValue &lt;UUID&gt; It's equally like that I simply don't understand how PassthroughSubject or Subscribers.Sink are supposed to behave, but, given what I know today, this looks like a bug. class StringPublisher: Publisher { &#9;typealias Output = String &#9;typealias Failure = Never &#9;private let subject = PassthroughSubject&lt;Output, Failure&gt;() &#9;private var timer: Timer? = nil &#9;func receive&lt;S: Subscriber&gt;(subscriber: S) &#9;&#9;where StringPublisher.Failure == S.Failure, StringPublisher.Output == S.Input &#9;{ &#9;&#9;if self.timer == nil { &#9;&#9;&#9;let timer = Timer(timeInterval: 0.5, repeats: true) { [weak self] (_) in &#9;&#9;&#9;&#9;self?.subject.send(UUID().uuidString) &#9;&#9;&#9;} &#9;&#9;&#9;self.timer = timer &#9;&#9;&#9;RunLoop.main.add(timer, forMode: .default) &#9;&#9;} &#9;&#9; &#9;&#9;subject.receive(subscriber: subscriber) &#9;} &#9;func cancel() { &#9;&#9;timer?.invalidate() &#9;} } let pub1 = StringPublisher() let sink1 = pub1.sink(receiveValue: { print("receiveValue", $0) }) defer { sink1.cancel() } RunLoop.main.run(until: Date(timeIntervalSinceNow: 2))