Fix serial fd leak in SerialConnection.connect() on timeout - #101
Open
joeyleake wants to merge 1 commit into
Open
Fix serial fd leak in SerialConnection.connect() on timeout#101joeyleake wants to merge 1 commit into
joeyleake wants to merge 1 commit into
Conversation
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.
Fixes #95
Problem
SerialConnection.connect() leaked 3 file descriptors (the serial port fd
plus two internal pipe fds) every time a connection attempt timed out.
In a reconnect loop against an unresponsive device, this exhausts the
~1024 fd limit and starts failing with "Too many open files".
Root cause
serial_asyncio.create_serial_connection() opens the fds immediately, but
the transport was only ever captured via self.transport, which is
assigned inside MCSerialClientProtocol.connection_made() — an async
callback. If asyncio.wait_for(self._connected_event.wait(), ...) timed
out before connection_made() ran, self.transport stayed None, so there
was no way to close the already-opened transport. It was simply
abandoned.
Fix
connect() now captures transport directly from create_serial_connection()'s
own return value instead of relying on self.transport being set. If the
subsequent wait_for() raises for any reason (timeout or otherwise), the
transport is closed before re-raising. Added a narrow guard
(
if self.transport is transport: self.transport = None) for the casewhere connection_made() fires right at the timeout boundary, so closing
this attempt's transport never clobbers self.transport if it's already
pointing at a different, unrelated connection. The success path
(logging + return self.port) is unchanged.
Testing
Added tests/unit/test_serial_connection.py::test_connect_closes_transport_on_timeout,
which patches create_serial_connection to return a transport whose
connection_made() never fires, and asserts transport.close() is called
before the TimeoutError propagates.
Full suite: 180 passed, 2 failed (test_send_cmd,
test_advert_path_preserves_embedded_zero_bytes) — both pre-existing on a
clean main checkout, unrelated to this change.