Skip to content

Commit

Permalink
watch::Receiver::wait_for(): Prevent poisoning of the lock
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Sep 21, 2023
1 parent 8229aac commit ee13691
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,23 @@ impl<T> Receiver<T> {
let has_changed = self.version != new_version;
self.version = new_version;

if (!closed || has_changed) && f(&inner) {
return Ok(Ref { inner, has_changed });
if !closed || has_changed {
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| f(&inner)));
match result {
Ok(true) => {
return Ok(Ref { inner, has_changed });
}
Ok(false) => {
// Skip the value.
}
Err(panicked) => {
// Drop the read-lock to avoid poisoning it.
drop(inner);
// Forward the panic to the caller.
panic::resume_unwind(panicked);
// Unreachable
}
};
}
}

Expand Down

0 comments on commit ee13691

Please sign in to comment.