Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory streams incorrectly raising cancelled when *_nowait() is called immediately after cancelling send()/receive() #729

Closed

Conversation

gschaffner
Copy link
Collaborator

@gschaffner gschaffner commented May 11, 2024

Changes

Fixes:

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
the you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) added which would fail without your patch
  • You've updated the documentation (in docs/, in case of behavior changes or new
    features)
  • You've added a new changelog entry (in docs/versionhistory.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

gschaffner and others added 2 commits May 11, 2024 00:00
…) when calling *_nowait() immediately after cancelling send()/receive()

* In the send_nowait() + receive() case, this bug could drop items.
* In the send() + receive_nowait() case, this bug could cause send() to
  raise even though it succeeded.
…alled immediately after cancelling send()/receive()

This partially reverts commit 6b0a1f3.

Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
@agronholm
Copy link
Owner

This was the first solution that came to my mind, but then I realized it would interact poorly with asyncio's native cancellation.

@gschaffner
Copy link
Collaborator Author

This was the first solution that came to my mind, but then I realized it would interact poorly with asyncio's native cancellation.

could you elaborate on this? i do see now that the branch of receive() that was added in a3af1da (which predates Python 3.11) should probably make an uncancel() call on asyncio >= 3.11, in order to support native cancellations. is this what you mean?

@gschaffner gschaffner force-pushed the fix-memory-stream-dropped-items branch from 10bcc86 to 06e38da Compare May 12, 2024 06:20
@gschaffner gschaffner changed the title Fix memory streams dropping items when calling send_nowait() immediately after cancelling receive() Fix memory streams incorrectly raising cancelled when caling *_nowait() immediately after cancelling send()/receive() May 12, 2024
@gschaffner
Copy link
Collaborator Author

gschaffner commented May 12, 2024

(i just updated this branch to also test for and fix the analogous bug that can occur when send() is cancelled.)

@gschaffner gschaffner force-pushed the fix-memory-stream-dropped-items branch from 06e38da to 2b09585 Compare May 12, 2024 06:47
@gschaffner gschaffner changed the title Fix memory streams incorrectly raising cancelled when caling *_nowait() immediately after cancelling send()/receive() Fix memory streams incorrectly raising cancelled when *_nowait() is called immediately after cancelling send()/receive() May 12, 2024
Comment on lines +240 to +244
# Ignore the immediate cancellation if we already sent the item, so as
# to not indicate failure despite success
if send_event in self._state.waiting_senders:
del self._state.waiting_senders[send_event]
raise
Copy link
Owner

Choose a reason for hiding this comment

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

This would be fine when the cancellation originates from a cancel scope, but not when a task was natively cancelled (Task.cancel(), because the originator of that cancellation expects the cancellation to be acted on and not ignored). Doing this will lead to hangs.

@agronholm
Copy link
Owner

Closing in favor of #735, as I've come to the conclusion that avoiding the item delivery to a task with a pending cancellation is the only viable solution.

@agronholm agronholm closed this May 25, 2024
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.

None yet

2 participants