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

fix bug where join_timeout would kill the subprocess on first timeout #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ impl<T: Serialize + DeserializeOwned> PooledHandle<T> {
match self.waiter_rx.recv_timeout(timeout) {
Ok(Ok(rv)) => Ok(rv),
Ok(Err(err)) => Err(err),
Err(mpsc::RecvTimeoutError::Timeout) => {
self.kill().ok();
Err(SpawnError::new_timeout())
}
Err(mpsc::RecvTimeoutError::Timeout) => Err(SpawnError::new_timeout()),
Err(mpsc::RecvTimeoutError::Disconnected) => Err(SpawnError::new_remote_close()),
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,15 @@ impl<T: Serialize + DeserializeOwned> JoinHandle<T> {
/// Wait for the child process to return a result.
///
/// If the join handle was created from a pool the join is virtualized.
pub fn join(self) -> Result<T, SpawnError> {
match self.inner {
Ok(JoinHandleInner::Process(mut handle)) => handle.join(),
Ok(JoinHandleInner::Pooled(mut handle)) => handle.join(),
Err(err) => Err(err),
pub fn join(mut self) -> Result<T, SpawnError> {
match &mut self.inner {
Ok(JoinHandleInner::Process(ref mut handle)) => handle.join(),
Ok(JoinHandleInner::Pooled(ref mut handle)) => handle.join(),
Err(err) => {
let mut rv_err = SpawnError::new_consumed();
mem::swap(&mut rv_err, err);
Err(rv_err)
}
}
}

Expand Down Expand Up @@ -551,6 +555,12 @@ impl<T: Serialize + DeserializeOwned> JoinHandle<T> {
}
}

impl<T> Drop for JoinHandle<T> {
fn drop(&mut self) {
self.kill().ok();
}
}

/// Spawn a new process to run a function with some payload.
///
/// ```rust,no_run
Expand Down
19 changes: 19 additions & 0 deletions tests/test_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ fn test_timeout() {
let val = handle.join_timeout(Duration::from_secs(2)).unwrap();
assert_eq!(val, 42);
}

#[test]
fn test_timeout_twice() {
let pool = Pool::new(2).unwrap();

let mut handle = pool.spawn((), |()| {
thread::sleep(Duration::from_secs(5));
42
});

let err = handle.join_timeout(Duration::from_millis(100)).unwrap_err();
assert!(err.is_timeout());

let err = handle.join_timeout(Duration::from_millis(100)).unwrap_err();
assert!(err.is_timeout());

let val = handle.join_timeout(Duration::from_secs(6)).unwrap();
assert_eq!(val, 42);
}