Skip to content

Commit 6895908

Browse files
committed
fix: race condition in next_when_notified
tokio-rs#280
2 parents 8f1bc5d + 8c18630 commit 6895908

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/buf/fixed/pool.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,10 @@ impl<T: IoBufMut> FixedBufPool<T> {
274274
pin!(notified);
275275
loop {
276276
// In the single-threaded case, no buffers could get checked in
277-
// between us calling `try_next` and here, so we can't miss a wakeup.
278-
notified.as_mut().await;
277+
// between us calling `try_next` and here. However, we may still miss a wake-up,
278+
// as multiple check-ins can occur before any waking tasks are scheduled,
279+
// which would result in the loss of a permit
280+
notified.as_mut().enable();
279281

280282
if let Some(data) = self.inner.borrow_mut().try_next(cap) {
281283
// Safety: the validity of buffer data is ensured by
@@ -284,6 +286,9 @@ impl<T: IoBufMut> FixedBufPool<T> {
284286
return buf;
285287
}
286288

289+
// Await notify_one
290+
notified.as_mut().await;
291+
287292
// It's possible that the task did not get a buffer from `try_next`.
288293
// The `Notify` entries are created once for each requested capacity
289294
// and never removed, so this `Notify` could have been holding

0 commit comments

Comments
 (0)