machinezone / IXWebSocket

websocket and http client and server library, with TLS support and very few dependencies
BSD 3-Clause "New" or "Revised" License
551 stars 177 forks source link

No Close event when server TCP socket closed #469

Open mduint32 opened 1 year ago

mduint32 commented 1 year ago

I'm investigating an issue where a linux client application using ix::WebSocket sometimes doesn't react to a websocket server (not IXWebSocket) closing its TCP socket. This is not a graceful websocket close handshake, but on TCP level a RST is being sent (according to WireShark) so the client is actually notified that the connection goes away.

Sometimes this results in the OnMessageCallback being called with a Close message with "abnormal closure" and sometimes it doesn't. When it doesn't, the next time any message is sent (either websocket ping or a regular message) it seems to realize the connection has failed and the OnMessageCallback is called with a Close message with "internal error".

My investigation so far has taken me to WebSocketTransport::poll: if _socket->isReadyToRead returns PollResultType::Error, it calls closeSocket, but the function will still return PollResult::Succeeded.

https://github.com/machinezone/IXWebSocket/blob/f79c64ae9741796acf4144ec86336880b644463d/ixwebsocket/IXWebSocketTransport.cpp#L377

Won't that mean WebSocket::run will keep retrying poll on a closed socket? Is that intentional?

I experimented with adding return PollResult::AbnormalClose; after closeSocket in that if clause. It improved behavior in my case: I reliably get Close directly and with "abnormal closure".

engun commented 1 year ago

me too

MichelJansson commented 12 months ago

Same here, server is stopped but no Close event is received. @mduint32, have you found any downsides to your fix? Should we make a PR?