fix: disconnect() always clears _connected even if socket close fails#523
Merged
Conversation
When the socket is already broken (e.g. 'Bad file descriptor'), close() raises and _connected was left True. The monitor restart path is guarded by 'if not self._connected', so it never rebuilt the socket and the client silently stopped receiving notifications until the AtClient was recreated. Wrap close() and always clear _connected in a finally. Adds network-free unit tests for clean-close and close-raises cases.
cpswan
approved these changes
Jul 3, 2026
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.
In
at_client/connections/atconnection.py:When the socket is already broken (e.g.
Bad file descriptor),close()itselfraises, so
_connectedis never reset. The monitor restart path is guarded byif not self._connected: self._connect(), so with_connectedstuck atTrueitnever rebuilds the socket and keeps reading the dead descriptor — the client silently
stops receiving notifications until the whole
AtClientis recreated.Fix: wrap
close()and always clear_connectedin afinally.Tests:
test/disconnect_test.py— network-free; asserts_connectedbecomesFalseboth on a clean close and whenclose()raisesOSError(Bad file descriptor).This is the primary fix for the "monitor never recovers after a drop" behaviour;
see the related issue for the contributing factors (process-global monitor locks and a
non-daemon heartbeat thread that never exits).