You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Change thread_worker library to propagate panics by default #817
We have a small thread_worker for spawning background threads. It's main purpose is to force clean thread shutdown: if you don't join the thread, it might be forcefully terminated when the process exists, skipping destructors. Leaving threads unjoined is also bad for reliable unit-testing, and in general bad for overall architecture of the system.
A slight problem with this library is that it turns a thread's panic into Result by forcing you to use the shutdown method. I now think this is a bad idea: it's usually best to escalate the panic instead of catching it, even on the thread boundary. We need to switch "panic, if child thread panicked" behavior.
To do this, we need the following changes:
introduce a wrapper around thread::spawn and thread::JoinHandler which joins the thread automatically in Drop, propagating the panic from the child thread if the parent thread is not panicking itself. We should use this wrapper in cases where we spawn bare threads, like here
Remove WorkerHandle struct (which exists solely to join the thread) and instead add our JoinHandler (what's a good name here? ScopedThread, ThreadGuard?) to the Worker
Change Worker's shutdown method to return a pair of (ScopedThread, Receiver)
update usages of thead_worker to the new API, paying close attention to possible deadlocks. Specifically, channel senders should be dropped beforeScopedThread, even in case of a panic.
cc @stjepang, this is very related to rust-lang/rust#48830 and shows an interesting interaction between channels and threads (drop sender prior to joining).
Activity
matklad commentedon Feb 13, 2019
cc @stjepang, this is very related to rust-lang/rust#48830 and shows an interesting interaction between channels and threads (drop sender prior to joining).
matklad commentedon Feb 13, 2019
Slightly tangential, but, if you are interested in threads and clean cancellation, there's an interesting discussion over here: https://trio.discourse.group/t/structured-concurrency-in-rust/73
matklad commentedon Feb 13, 2019
Hm, this seems fun indeed, looks like the pattern we want is
That's a super uncomfortable level of subtlety, can we do better?
DJMcNab commentedon Feb 13, 2019
We could just use std::mem::drop manually.
matklad commentedon Feb 13, 2019
That works for the happy case, but not for the panics, a panic will cause a deadlock
automatically wait for worker threads
automatically wait for worker threads
automatically wait for worker threads
Merge #833