Skip to content

fix(mqtt): keep relay socket fds open until both relay threads exit - #314

Merged
Lash-L merged 1 commit into
mainfrom
fix/mqtt-relay-fd-reuse
Nov 29, 2026
Merged

Lash-L merged 1 commit into
mainfrom
fix/mqtt-relay-fd-reuse

Conversation

@Lash-L

@Lash-L Lash-L commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a file-descriptor reuse race in the MQTT TLS proxy's connection teardown that wrote raw TLS records into decompiled_mqtt.jsonl.

Each proxied connection has two relay threads (c2b, b2c). Whichever one finished first shut down and closed both sockets in _relay's finally, and _close_conn_endpoints did the same. The other thread could still be about to write to the TLS socket (e.g. forwarding the broker's last packet). Once the fd was closed the kernel handed the same number to the next file opened. That is usually the trace worker, which opens decompiled_mqtt.jsonl once per line. The SSL write already in flight then landed in the log as a raw TLS record.

Found on a real deployment: one line of a 213 MB decode log began with 24 raw bytes, 17 03 03 00 13 plus 19 bytes. That is one complete TLS 1.3 application-data record, followed by an intact JSON entry. It matches a Home Assistant MQTT connection closing 3 ms after its last PUBLISH, whose decode entry is that line. A build that read the log as strict UTF-8 then failed every routine execute from that point on. Current main reads it tolerantly, but the log still collects corrupt lines, and the client loses that last record.

Change:

  • _shutdown_and_close is split into _shutdown_endpoint and _close_endpoint.
  • _relay and _close_conn_endpoints only shut sockets down. That still wakes a thread blocked in recv/send, which is what fix(mqtt): shutdown sockets on relay teardown to ensure dropped clients are closed #263 needs. The SSLSocket path still uses the plain-socket shutdown, as before.
  • _handle_client shuts down both endpoints, joins both relay threads, and only then closes the sockets.

The JSON writer isn't involved; append_jsonl already uses ensure_ascii=True.

Proof that it works

Tested on:

  • Vacuum model: Qrevo MaxV 2 (a87), Home Assistant client. The corrupt line came from this deployment's log.
  • Firmware version: 02.34.04
  • Server version / commit: main (d09fb0f) + this change
  • Install method: tests and a local instance on macOS. Not yet run as the HA add-on; CI covers Linux.

What I did and what happened:

  • Reproduced the exact byte pattern by writing on a stale SSL socket after close() and reopening a file into the freed fd, both in a container built from the affected release image and on macOS.
  • New regression test test_relay_teardown_keeps_the_tls_fd_until_the_other_relay_thread_is_done:
    • It parks the b2c thread inside SSLSocket.sendall, makes the c2b thread tear the connection down, opens the log into the lowest free fds, then releases the write.
    • On unfixed main the log contains the raw TLS record and the test fails. With the fix the fd is still held, so the test passes.
  • Tests that asserted the old close-in-relay behavior now assert shutdown-only in the relay plus close in _handle_client.
  • tests/test_mqtt_tls_proxy.py: 51 passed, three runs in a row. Full suite: 881 passed.
  • Ran about 9,600 sessions plus malformed, garbage, plain-HTTP and non-UTF-8-topic input against a local instance; no corrupt lines. The race is narrow, which is why it shows up rarely in the field.

Screenshots (required for any UI change)

No UI changes.

🤖 Generated with Claude Code

Whichever relay thread finished first shut down and closed both sockets
while the other thread could still be about to write to the TLS socket.
Once the fd was closed the kernel reused its number for the next file
opened, usually the trace worker appending to decompiled_mqtt.jsonl, and
the pending TLS record landed in that log as raw bytes. A strict UTF-8
reader of the log then failed on every scene execute.

Relay threads and _close_conn_endpoints now only shut the sockets down
(which still wakes blocked recv/send); _handle_client closes them after
joining both relay threads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 29, 2026 18:56

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 b14569d into main Nov 29, 2026
3 checks passed
Lash-L added a commit that referenced this pull request Nov 29, 2026
Bump the package to 1.3.0-rc2 and point the opt-in Beta add-on at it.
The v1.3.0-rc1 release build failed on a flaky test (fixed in #308), so
no rc1 image was published; rc2 replaces it and adds #312, #313 and
#314. The stable add-on stays on 1.2.1.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants