Skip to content

Wait for a connection closure with timeout - #15

Merged
Pliner merged 2 commits into
Pliner:mainfrom
slawwan:close-with-timeout
Oct 25, 2026
Merged

Pliner merged 2 commits into
Pliner:mainfrom
slawwan:close-with-timeout

Conversation

@slawwan

@slawwan slawwan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #8: as suggested there, the background closure is dropped now that MagicStack/asyncpg#1361 is released in asyncpg 0.32.0.

The guard awaits connection.close(timeout=close_timeout) (5 s by default). Since 0.32.0, the timeout also covers the pending cancellation, and on timeout asyncpg aborts the socket. Before, the background close() on a silently dead connection could hang forever and keep the socket open.

Errors from close() are suppressed, so they never replace the original exception. Otherwise a TimeoutError from closing a dead connection swallows CancelledError, and the cancelled guard keeps running and reacquires the lock.

asyncpg 0.32.0 is required: with 0.31.0 the awaited close() hangs again, test_reacquire_lock_after_silent_disruption and the new tests fail.

Tests:

  • test_release_silently_dead_connection: the guard drops a silently dead connection; fails on main, passes with the fix;
  • test_stop_on_silently_dead_connection: the guard stops on cancellation while its connection is silently dead; passes on main, fails if close() errors are not suppressed.

@slawwan
slawwan requested a review from Pliner October 25, 2026 09:55
@slawwan
slawwan marked this pull request as ready for review October 25, 2026 10:19
@Pliner
Pliner merged commit de5c2bc into Pliner:main Oct 25, 2026
5 checks passed
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