Repository navigation
fix(mqtt): shutdown sockets on relay teardown to ensure dropped clients are closed - #263
Conversation
…ts are closed When a client or the backend broker drops a connection or disconnects, calling close() without shutdown(SHUT_RDWR) leaves the peer relay thread blocked in recv() on Linux/POSIX sockets, preventing the connection from closing cleanly and evicting dropped clients. Calling endpoint.shutdown(socket.SHUT_RDWR) unblocks blocking peer recvs and sends TCP FIN/RST immediately. Fixes Python-roborock#262
There was a problem hiding this comment.
Automated review: changes requested (one small fix)
This fixes the dropped-client symptom in #262. On Linux, close() doesn't wake the c2b relay blocked in recv(), and shutdown(SHUT_RDWR) does. It also makes _close_conn_endpoints actually drop rejected onboarding connections. A double shutdown is harmless, since the OSError is caught. The tests do assert shutdown_calls.
Requested fix: a small race. ssl.SSLSocket.shutdown sets _sslobj = None before the real shutdown, so the other relay thread can:
- have
recvfall through toread()and raiseValueError, which_relaydoesn't catch (the thread dies with a traceback;finallystill runs); - briefly fall back to a plaintext
send.
Suggestion: call socket.socket.shutdown(endpoint, socket.SHUT_RDWR) to skip the SSL override, and catch ValueError in _relay. Catching AttributeError isn't needed.
Optional: the tests use fake sockets, so a socketpair test that shows a blocked recv actually wakes up would be stronger. The new shutdown in _handle_client when the relay never started isn't tested.
This is an automated review by Claude Code, checked against the decompiled Roborock app (APK + device plugin bundles), python-roborock and a vacuum firmware dump. It can be incorrect, so please reply if any finding looks wrong.
|
Following up on the automated review above: I've converted this PR to draft while the requested change is addressed. Mark it ready for review again once it's fixed and it will go through another review. The review above was automated and can be incorrect, so please reply if any finding looks wrong. |
…dpoints Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SSLSocket.shutdown() clears the SSL object before the real shutdown, so the other relay thread could fall through to read() and raise ValueError, or briefly send plaintext. Shut SSL endpoints down at the socket level, catch ValueError in the relay loop, and share one teardown helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Socketpair tests (plain and TLS) prove relay threads blocked in recv() wake when a session is torn down, plus the _handle_client path where the relay never started and a relay hitting ValueError from a closed SSL socket. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the fix! I've taken this over as maintainer to get it merge-ready.
Full suite: 878 passed. This closes out the earlier review's requested change, so I've taken it out of draft. |
Summary
When a client or Mosquitto drops a connection (e.g. Client has exceeded timeout, disconnecting), calling close() on both sockets in _relay() or _close_conn_endpoints() does not interrupt a concurrent blocking
ecv(4096) in the other relay thread on POSIX/Linux systems. This prevents the proxy from cleanly tearing down the connection, sending a FIN/RST packet, and logging [conn N] closed.
Adding endpoint.shutdown(socket.SHUT_RDWR) before endpoint.close() immediately interrupts any blocked
ecv() calls and delivers TCP FIN/RST to both the vacuum/client and the backend broker, allowing the relay threads to join cleanly and the connection to be deregistered.
Changes
Resolves #262