Skip to content

Commit 5a31d17

Browse files
authored
Fix race condition in bounded queue (ArroyoSystems#1017)
1 parent 84589c1 commit 5a31d17

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

crates/arroyo-operator/src/context.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,19 @@ impl BatchSender {
138138
}
139139
}
140140
} else {
141-
// not enough room in the queue, wait to be notified that the receiver has
142-
// consumed
143-
self.notify.notified().await;
141+
// first register the notify listener
142+
let notified = self.notify.notified();
143+
144+
// then recheck -- there may now be space since we checked
145+
let cur = self.queued_messages.load(Ordering::Acquire);
146+
if cur as usize + count as usize <= self.size as usize {
147+
// space is now available, retry immediately
148+
continue;
149+
}
150+
151+
// if not, we're now guaranteed to receive the notification if space is made
152+
// available
153+
notified.await;
144154
}
145155
}
146156
}

0 commit comments

Comments
 (0)