Skip to content

Commit 355c798

Browse files
committed
native: Don't deadlock the runtime on spawn failure
Previously, the call to bookkeeping::increment() was never paired with a decrement when the spawn failed (due to unwinding). This fixes the problem by returning a "bomb" from increment() which will decrement on drop, and then moving the bomb into the child task's procedure which will be dropped naturally.
1 parent e156d00 commit 355c798

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/libnative/task.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn spawn_opts(opts: TaskOpts, f: proc():Send) {
7171
// Note that this increment must happen *before* the spawn in order to
7272
// guarantee that if this task exits it will always end up waiting for the
7373
// spawned task to exit.
74-
bookkeeping::increment();
74+
let token = bookkeeping::increment();
7575

7676
// Spawning a new OS thread guarantees that __morestack will never get
7777
// triggered, but we must manually set up the actual stack bounds once this
@@ -93,7 +93,7 @@ pub fn spawn_opts(opts: TaskOpts, f: proc():Send) {
9393
let mut task = task;
9494
task.put_runtime(ops);
9595
drop(task.run(|| { f.take_unwrap()() }).destroy());
96-
bookkeeping::decrement();
96+
drop(token);
9797
})
9898
}
9999

src/librustrt/bookkeeping.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,24 @@
1919
//! decrement() manually.
2020
2121
use core::atomics;
22+
use core::ops::Drop;
2223

2324
use mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
2425

2526
static mut TASK_COUNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
2627
static mut TASK_LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
2728

28-
pub fn increment() {
29+
pub struct Token(());
30+
31+
impl Drop for Token {
32+
fn drop(&mut self) { decrement() }
33+
}
34+
35+
/// Increment the number of live tasks, returning a token which will decrement
36+
/// the count when dropped.
37+
pub fn increment() -> Token {
2938
let _ = unsafe { TASK_COUNT.fetch_add(1, atomics::SeqCst) };
39+
Token(())
3040
}
3141

3242
pub fn decrement() {

0 commit comments

Comments
 (0)