Repository navigation
fix(mqtt): keep relay socket fds open until both relay threads exit - #314
Merged
Merged
Conversation
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>
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sfinally, and_close_conn_endpointsdid 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 opensdecompiled_mqtt.jsonlonce 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 13plus 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. Currentmainreads it tolerantly, but the log still collects corrupt lines, and the client loses that last record.Change:
_shutdown_and_closeis split into_shutdown_endpointand_close_endpoint._relayand_close_conn_endpointsonly shut sockets down. That still wakes a thread blocked inrecv/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_clientshuts down both endpoints, joins both relay threads, and only then closes the sockets.The JSON writer isn't involved;
append_jsonlalready usesensure_ascii=True.Proof that it works
Tested on:
main(d09fb0f) + this changeWhat I did and what happened:
close()and reopening a file into the freed fd, both in a container built from the affected release image and on macOS.test_relay_teardown_keeps_the_tls_fd_until_the_other_relay_thread_is_done:b2cthread insideSSLSocket.sendall, makes thec2bthread tear the connection down, opens the log into the lowest free fds, then releases the write.mainthe log contains the raw TLS record and the test fails. With the fix the fd is still held, so the test passes._handle_client.tests/test_mqtt_tls_proxy.py: 51 passed, three runs in a row. Full suite: 881 passed.Screenshots (required for any UI change)
No UI changes.
🤖 Generated with Claude Code