Skip to content

fix(client): close() waits for async hooks before it closes the client - #122

Merged
kalaspuff merged 2 commits into
issues-stack/7-retry-policy-valuesfrom
issues-stack/8-close-waits-for-hooks
Oct 31, 2026
Merged

kalaspuff merged 2 commits into
issues-stack/7-retry-policy-valuesfrom
issues-stack/8-close-waits-for-hooks

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

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 async operation.failed hook that awaited something and then wrote an audit item got ShutdownError (reported as hook.failed), and the item was never written, although the observability guide says the client waits for async hooks at close(). events.drain() also gathered every hook task: an async hook that called await client.close() waited for itself and never finished (at shutdown asyncio.run raised RecursionError cancelling it), and with a client used from two event loops, a hook still pending on the closed second loop made close() raise ValueError: The future belongs to a different loop after 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. A close() from a hook does not wait for that hook, and a second close() while the first one waits for hooks returns at once. events.pending() lists those hook tasks, and drain() waits for them with asyncio.wait, so cancelling drain() no longer cancels the hooks.

Verified with unit tests on the in-memory backend, which fail or hang without the fix: an operation.failed hook writes an audit item during close() (read back through a second client sharing the store, with no hook.failed), a hook closes the client, and close() finishes with a hook stuck on another, closed event loop. They pass 20 runs in a row and on Python 3.12. make check passes.

Fixes #88

Checklist

  • Tests cover the change (in-memory backend)
  • Docs updated (observability guide, docstrings, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No

Worth confirming

  • Decision: while 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 keep close() waiting.

fctl session


Note

Medium risk. It changes the order in which Client.close() waits for async hooks and closes the core, and what events.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 an operation.failed audit writer can still use the client. A hook that calls close() no longer waits for itself, and hooks stuck on another, closed event loop no longer make close() raise. events.pending() is new and copies the hook task set before filtering it, since other event loops change that set from their threads, and drain() waits with asyncio.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 while close() waits the client still accepts calls.

fctl review's first look at ee6e9e9 · 5 files, +114 −8 · Review agent settings

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]>
@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/events.py Resolved
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]>
@kalaspuff
kalaspuff added this pull request to stack #124 October 31, 2026 01:30
@kalaspuff
kalaspuff merged commit 779546c into main Oct 31, 2026
22 checks passed
@kalaspuff
kalaspuff deleted the issues-stack/8-close-waits-for-hooks branch October 31, 2026 01:34
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.

close() closes the core before async hooks finish, and drain() can wait on its own task

1 participant