Closed PatrickPijnappel closed 1 year ago
@PatrickPijnappel what does your web socket code look like? We're probably missing a hop somewhere
@0xTim Basically this:
self.queue.async {
// (…)
webSocket.onBinary { [weak self] _, data in // Called from arbitrary queue
guard let self = self else { return }
// (…)
}
}
What is self in this instance and queue? Im assuming the web socket is being created outside of the queue closure?
self
is a custom (final) class and queue
is a serial DispatchQueue
. The web socket is being created elsewhere on another queue using effectively
WebSocket.connect(to: url, on: app.eventLoopGroup.next()) { webSocket in
queue.async { self.webSocket = webSocket }
}
@PatrickPijnappel ah ok, I misread this initially.
Setting the WS callbacks must be done on the Websocket's EventLoop
. We assumed this was the case before but had no way to enforce this at a compiler level before Sendable
. Doing it off the event loop might have worked but could easily caused crashes before and for us to be Sendable
compliant we have to enforce it.
One workaround might be to wrap your calls in
websocket.eventLoop.execute {
websocket.onBinary {
// ...
}
}
That should enforce that the mutations are done on the correct event loop, but mixing concurrency, event loops and dispatch queues like this is always going to cause issues
@0xTim Ah i see, thank you!
Describe the bug
We're getting this crash with 2.12.0, then it was fixed with 2.12.1 but failing again with 2.13.0 (Swift 5.7)
To Reproduce
(Not sure at this point)
Expected behavior
No crash.
Environment