Skip to content

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

Merged
BewareMyPower merged 1 commit intoapache:mainfrom
goodvibs:fix/noop-callback-after-cancel
May 6, 2026
Merged

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

Conversation

@goodvibs
Copy link
Copy Markdown
Contributor

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
Contributor 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
@BewareMyPower
Copy link
Copy Markdown
Contributor

It's weird that CI is not running, let me close and reopen this PR

@BewareMyPower BewareMyPower reopened this Apr 27, 2026
@BewareMyPower BewareMyPower merged commit 5d38ac9 into apache:main May 6, 2026
11 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