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

Race condition in the async code when sender drops too quickly #120

Closed
inetic opened this issue Mar 8, 2023 · 1 comment
Closed

Race condition in the async code when sender drops too quickly #120

inetic opened this issue Mar 8, 2023 · 1 comment

Comments

@inetic
Copy link
Contributor

inetic commented Mar 8, 2023

When using the async code and the Sender sends and gets dropped at the wrong time, the Receiver won't receive the sent value. Here is how the race happens:

  1. Let's have a Sender tx and a Receiver rx.
  2. User calls rx.recv().await, the RecvFut::poll_inner func goes here. So far so good.
  3. The next time RecvFut::poll_inner is called, we get to line 377 where we check whether the sender sent something already. At this point the sender has not and thus we do not proceed to the if block (line 378).
  4. But now the sending thread is scheduled, the sender sends a value and tx is dropped.
  5. The receiving thread wakes up inside RecvFut::poll_inner, on line 379 it determines that the Sender has been destroyed and Poll::Ready(Err(RecvError::Disconnected)) is returned without ever returning the value from the previous step.

I have a solution which I tested and seems to be working fine: instead of testing if self.receiver.shared.is_disconnected() on line 379 we can just check whether the returned value from step #3 is equal to Err(TryRecvTimeoutError::Disconnected). I'll send a PR.

inetic added a commit to inetic/flume that referenced this issue Mar 8, 2023
The race condition was happening when the Sender sent a value and was
immediatelly dropped between the calls
`self.receiver.shared.recv_sync(None)` and
`self.receiver.shared.is_disconnected()`. More information on github
issue zesterer#120
@zesterer
Copy link
Owner

zesterer commented Apr 6, 2023

Closed by #121

@zesterer zesterer closed this as completed Apr 6, 2023
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

No branches or pull requests

2 participants