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

sync: add watch::Sender::send_modify method #4310

Merged
merged 11 commits into from
Feb 22, 2022
11 changes: 9 additions & 2 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use crate::loom::sync::atomic::Ordering::Relaxed;
use crate::loom::sync::{Arc, RwLock, RwLockReadGuard};
use std::mem;
use std::ops;
use std::panic;

/// Receives values from the associated [`Sender`](struct@Sender).
///
Expand Down Expand Up @@ -461,8 +462,14 @@ impl<T> Sender<T> {
{
// Acquire the write lock and update the value.
let mut lock = self.shared.value.write().unwrap();
// Update the value.
func(&mut lock);
// Update the value and catch possible panic inside func.
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
func(&mut lock);
}));
// If the func panicked return the panic to the caller.
if let Err(error) = result {
panic::resume_unwind(error);
}

self.shared.state.increment_version();

Expand Down