Skip to content

fix(asyncio): Avoid InvalidStateError on late callbacks after cancel#297

Open
goodvibs wants to merge 1 commit intoapache:mainfrom
goodvibs:fix/noop-callback-after-cancel
Open

fix(asyncio): Avoid InvalidStateError on late callbacks after cancel#297
goodvibs wants to merge 1 commit intoapache:mainfrom
goodvibs:fix/noop-callback-after-cancel

Conversation

@goodvibs
Copy link
Copy Markdown

Summary

Guard _set_future so scheduled completions are skipped when the asyncio Future is cancelled.

Problem

_set_future schedules complete() via call_soon_threadsafe. If the Future is cancelled, a late callback could still call set_result / set_exception, which raises asyncio.InvalidStateError and is logged as an exception in the event loop callback.

Solution

At the start of complete(), return early if future.done(). This matches common asyncio callback bridges (no-op when the future is no longer pending).

Testing

Added AsyncioSetFutureTest with two cases (no live broker): cancelled Future + late success callback, and already-resolved Future + late callback.

@goodvibs
Copy link
Copy Markdown
Author

Same as #296, but from a different branch (the other one was off my main branch, which I needed to add commits to)

@BewareMyPower BewareMyPower added this to the 3.12.0 milestone Apr 16, 2026
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