Skip to content

fix: disconnect() always clears _connected even if socket close fails#523

Merged
cpswan merged 2 commits into
trunkfrom
fix/disconnect-resets-connected
Jul 3, 2026
Merged

fix: disconnect() always clears _connected even if socket close fails#523
cpswan merged 2 commits into
trunkfrom
fix/disconnect-resets-connected

Conversation

@cconstab

@cconstab cconstab commented Jul 2, 2026

Copy link
Copy Markdown
Member

In at_client/connections/atconnection.py:

def disconnect(self):
    self._secure_root_socket.close()   # raises on an already-broken socket
    self._connected = False            # skipped when close() raises

When the socket is already broken (e.g. Bad file descriptor), close() itself
raises, so _connected is never reset. The monitor restart path is guarded by
if not self._connected: self._connect(), so with _connected stuck at True it
never rebuilds the socket and keeps reading the dead descriptor — the client silently
stops receiving notifications until the whole AtClient is recreated.

Fix: wrap close() and always clear _connected in a finally.

Tests: test/disconnect_test.py — network-free; asserts _connected becomes
False both on a clean close and when close() raises OSError(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).

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.
@cconstab cconstab changed the title disconnect() always clears _connected even if socket close fails fix: disconnect() always clears _connected even if socket close fails Jul 2, 2026
@cconstab cconstab assigned Xlin123 and unassigned Xlin123 Jul 3, 2026
@cconstab cconstab requested review from Xlin123 and cpswan July 3, 2026 02:54
@cpswan cpswan merged commit 09d89ed into trunk Jul 3, 2026
9 checks passed
@cpswan cpswan deleted the fix/disconnect-resets-connected branch July 3, 2026 08:25
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.

3 participants