Skip to content

Commit c8ecfc8

Browse files
authored
sync: fix watch borrow_and_update (#3913)
1 parent 08ed41f commit c8ecfc8

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

tokio/src/sync/watch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<T> Receiver<T> {
247247
/// [`changed`]: Receiver::changed
248248
pub fn borrow_and_update(&mut self) -> Ref<'_, T> {
249249
let inner = self.shared.value.read().unwrap();
250-
self.version = self.shared.version.load(SeqCst);
250+
self.version = self.shared.version.load(SeqCst) & !CLOSED;
251251
Ref { inner }
252252
}
253253

tokio/tests/sync_watch.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,20 @@ fn poll_close() {
169169

170170
assert!(tx.send("two").is_err());
171171
}
172+
173+
#[test]
174+
fn borrow_and_update() {
175+
let (tx, mut rx) = watch::channel("one");
176+
177+
tx.send("two").unwrap();
178+
assert_ready!(spawn(rx.changed()).poll()).unwrap();
179+
assert_pending!(spawn(rx.changed()).poll());
180+
181+
tx.send("three").unwrap();
182+
assert_eq!(*rx.borrow_and_update(), "three");
183+
assert_pending!(spawn(rx.changed()).poll());
184+
185+
drop(tx);
186+
assert_eq!(*rx.borrow_and_update(), "three");
187+
assert_ready!(spawn(rx.changed()).poll()).unwrap_err();
188+
}

0 commit comments

Comments
 (0)