Skip to content

Fix dropped message when cancelling subscription messages iterator#804

Open
p7g wants to merge 4 commits intonats-io:mainfrom
p7g:main
Open

Fix dropped message when cancelling subscription messages iterator#804
p7g wants to merge 4 commits intonats-io:mainfrom
p7g:main

Conversation

@p7g
Copy link

@p7g p7g commented Dec 22, 2025

If you have an asyncio task that iterates over messages from a subscription and you cancel it while it's waiting for the next message, there are a couple of outcomes:

  1. The next message received will be dropped; for an example, see the test I added
  2. The get_task gets GCed before it consumes a message, which results in a warning from the event loop's exception handler like Task was destroyed but it is still pending!

I added a regression test and made sure it was failing, then fixed the bug by cancelling get_task if the current task is cancelled while waiting, then made sure that the new test was passing. An existing test was publishing an extra message to work around this issue; I had to remove the workaround to make it pass.

Copy link
Collaborator

@caspervonb caspervonb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much, dangling task is a no-no, lgtm!

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