Skip to content

fix(mqtt): shutdown sockets on relay teardown to ensure dropped clients are closed - #263

Merged
Lash-L merged 4 commits into
Python-roborock:mainfrom
boomesh11:fix-relay-shutdown-endpoints
Nov 29, 2026
Merged

Lash-L merged 4 commits into
Python-roborock:mainfrom
boomesh11:fix-relay-shutdown-endpoints

Conversation

@boomesh11

Copy link
Copy Markdown
Contributor

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

  1. Call endpoint.shutdown(socket.SHUT_RDWR) in _relay finally block, _close_conn_endpoints, and _handle_client connection failure cleanup.
  2. Update fake sockets in ests/test_mqtt_tls_proxy.py and verify shutdown(socket.SHUT_RDWR) is called upon relay termination and endpoint closure.

Resolves #262

…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
@Lash-L Lash-L added area: mqtt MQTT broker, bridge, credentials and connection lifecycle. bug Something isn't working labels Nov 15, 2026

@Lash-L Lash-L left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 recv fall through to read() and raise ValueError, which _relay doesn't catch (the thread dies with a traceback; finally still 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.

@Lash-L Lash-L added the failing auto review Automated review requested changes before merge label Nov 21, 2026
@Lash-L

Lash-L commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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.

@Lash-L
Lash-L marked this pull request as draft November 21, 2026 16:07
Lash-L and others added 3 commits November 28, 2026 22:50
…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>
@Lash-L

Lash-L commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix! I've taken this over as maintainer to get it merge-ready.

  • Merged main into the branch to resolve the test conflict (no force-push).
  • Teardown now shuts SSL sockets down at the socket level (socket.socket.shutdown), because SSLSocket.shutdown() clears the SSL object first and the other relay thread could hit a ValueError or a plaintext send. _relay also exits cleanly on ValueError.
  • Added real socketpair tests (plain and TLS) showing relay threads blocked in recv() wake on teardown, plus one for the path where the relay never starts.

Full suite: 878 passed. This closes out the earlier review's requested change, so I've taken it out of draft.

@Lash-L
Lash-L marked this pull request as ready for review November 29, 2026 03:58
@Lash-L Lash-L added passed auto review Automated review found no blocking issues and removed failing auto review Automated review requested changes before merge labels Nov 29, 2026
Copilot AI balanced review requested due to automatic review settings November 29, 2026 03:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Lash-L
Lash-L merged commit 86d6b9a into Python-roborock:main Nov 29, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: mqtt MQTT broker, bridge, credentials and connection lifecycle. bug Something isn't working passed auto review Automated review found no blocking issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MQTT TLS proxy (8881) stops answering handshakes; dropped clients not torn down; S7 Max Ultra evicted by keepalive

3 participants