anyio icon indicating copy to clipboard operation
anyio copied to clipboard

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

Open gschaffner opened this issue 9 months ago • 3 comments

Changes

Fixes:

  • #728 (in sense (1) as defined in https://github.com/agronholm/anyio/issues/728#issuecomment-2105664591). cancellation of receive() could drop items, meaning that neither the attempted sender nor the attempted receiver think they are responsible for the item.

  • the analogous issue about cancellation of send(). cancellation of send() could result in send() raising cancelled despite succeeding, meaning that both the attempted sender and the attempted receiver think they are responsible for the item.

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):

  • [x] 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)
  • [x] 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 avatar May 11 '24 10:05 gschaffner

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

agronholm avatar May 11 '24 11:05 agronholm

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 a3af1da23c15af016af2656ebae79bd1580cbee3 (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 avatar May 12 '24 05:05 gschaffner

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

gschaffner avatar May 12 '24 06:05 gschaffner

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 avatar May 25 '24 22:05 agronholm