Repository navigation
fix(client): close() waits for async hooks before it closes the client - #122
Merged
kalaspuff merged 2 commits intoOct 31, 2026
Conversation
close() set the closed flag and closed the core before it drained the async hooks, so a hook that used the client (an operation.failed hook that writes an audit item) got ShutdownError. drain() gathered every hook task: a hook that called close() waited for itself and never finished, and a hook pending on another, closed event loop made close() raise "The future belongs to a different loop" after the core was closed. close() now delivers the buffered events and waits for the hooks of the current event loop, except the calling task, until none is left, and only then closes the core. events.pending() lists those tasks, and drain() waits for them with asyncio.wait, which does not cancel hooks when drain() is cancelled. Fixes #88 Co-authored-by: Carl Oscar Aaro <[email protected]>
This comment has been minimized.
This comment has been minimized.
Hooks of other event loops add and discard tasks from their own threads, and the GIL can switch while the comprehension runs, so iterating the live set could raise 'Set changed size during iteration'. Co-authored-by: Carl Oscar Aaro <[email protected]>
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 and why
close()set the closed flag and closed the core before it waited for the async hooks still running, so a hook could not use the client while the client "waited for it": an asyncoperation.failedhook that awaited something and then wrote an audit item gotShutdownError(reported ashook.failed), and the item was never written, although the observability guide says the client waits for async hooks atclose().events.drain()also gathered every hook task: an async hook that calledawait client.close()waited for itself and never finished (at shutdownasyncio.runraisedRecursionErrorcancelling it), and with a client used from two event loops, a hook still pending on the closed second loop madeclose()raiseValueError: The future belongs to a different loopafter the core was closed.close()now first delivers the events the core buffered and waits for the async hooks of the current event loop, except the calling task, until none is left (hooks those hooks start included), and only then stops the pump and closes the core. Calls made meanwhile still run. Aclose()from a hook does not wait for that hook, and a secondclose()while the first one waits for hooks returns at once.events.pending()lists those hook tasks, anddrain()waits for them withasyncio.wait, so cancellingdrain()no longer cancels the hooks.Verified with unit tests on the in-memory backend, which fail or hang without the fix: an
operation.failedhook writes an audit item duringclose()(read back through a second client sharing the store, with nohook.failed), a hook closes the client, andclose()finishes with a hook stuck on another, closed event loop. They pass 20 runs in a row and on Python 3.12.make checkpasses.Fixes #88
Checklist
make checkpasses locally (lint, types, clippy, consistency, tests)Worth confirming
close()waits for hooks, the client still accepts calls from any task, not only from hooks, since it cannot tell a hook's call from another; it refuses calls once the hooks have finished. Hooks that keep starting calls whose events start more hooks keepclose()waiting.fctl session
Note
Medium risk. It changes the order in which
Client.close()waits for async hooks and closes the core, and whatevents.drain()waits for, which every client shutdown goes through.What it does.
close()now delivers the events the core buffered and waits for the async hooks of the current event loop, leaving out the calling task, and only then closes the core, so a hook such as anoperation.failedaudit writer can still use the client. A hook that callsclose()no longer waits for itself, and hooks stuck on another, closed event loop no longer makeclose()raise.events.pending()is new and copies the hook task set before filtering it, since other event loops change that set from their threads, anddrain()waits withasyncio.wait, so cancelling it leaves the hooks running.Concurrent close(). A second
close()called while the first one is still waiting for hooks returns at once, before the client is closed, and whileclose()waits the client still accepts calls.fctl review's first look at
ee6e9e9· 5 files, +114 −8 · Review agent settings