Network/NWConnection/NWProtocolWebSocket error on what should be a graceful disconnect

Hello everybody,

I'm working on an app that implements a communication protocol on top of WebSocket using the Network Framework via NWProtocolWebSocket/NWConnection.

The issue I'm seeing is that on macOS (at least under macOS 13.0 Ventura) when calling .cancel() on the NWConnection on say the server side, the client does not disconnect gracefully. Instead the client receives a POSIXErrorCode(rawValue: 96): No message available on STREAM on the NWConnection.receiveMessage block.

You can verify this behaviour in the testDisconnect() test case in https://github.com/pusher/NWWebSocket (Disclaimer: I'm not the author of NWWebSocket but it shows the same issues that I'm experiencing in my code).

The same issue didn't show up in the iOS Simulator, where the connection closed correctly. Now I'm wondering if this is a bug in macOS 13 Ventura or if we can do something about it by changing the disconnect code.

Any help is greatly appreciated...

Thank you, Bastian Roessler

I just had a chance to verify and in fact the problem does not occur on macOS 12.6. So it seems that this is a bug that has been introduced in macOS 13. Still, the question remains if there is a workaround from a developer's perspective...

Interesting, this error code is usually sent when all data has already been read, and there is nothing more to be delivered via receive, potentially after a graceful close. All subsequent attempts to read will likely "fail", which is really just repeating "yes, this flow is finished".

For each of the receives leading up to this one, can you confirm the state of the isComplete flag on the receive and the isFinal flag on the ContentContext that accompanies it?

In theory, at least one receive prior to the one that failed with this error should have indicated that it was both complete, indicating that there is no more data for this message, and final, indicating that there are no more messages coming.

Network/NWConnection/NWProtocolWebSocket error on what should be a graceful disconnect
 
 
Q