Skip to content

Commit

Permalink
current-thread: fix shutdown on idle (#763)
Browse files Browse the repository at this point in the history
When spawning using `Handle` while on the executor, tasks were being
double counted. This prevented the number of active tasks to reach zero,
thus preventing the executor from shutting down.

This changes `spawn` to check if being called from the executor
**before** incrementing the number of active tasks.

Fixes #760
  • Loading branch information
carllerche authored Nov 20, 2018
1 parent 9b1a45c commit ed3ece2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 7 additions & 7 deletions tokio-current-thread/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,13 @@ impl Handle {
where
F: Future<Item = (), Error = ()> + Send + 'static,
{
if thread::current().id() == self.thread {
let mut e = TaskExecutor::current();
if e.id() == Some(self.id) {
return e.spawn_local(Box::new(future));
}
}

if self.shut_down.get() {
return Err(SpawnError::shutdown());
}
Expand All @@ -669,13 +676,6 @@ impl Handle {
return Err(SpawnError::shutdown());
}

if thread::current().id() == self.thread {
let mut e = TaskExecutor::current();
if e.id() == Some(self.id) {
return e.spawn_local(Box::new(future));
}
}

self.sender.send(Box::new(future))
.expect("CurrentThread does not exist anymore");
// use 0 for the id, CurrentThread does not make use of it
Expand Down
19 changes: 19 additions & 0 deletions tokio-current-thread/tests/current_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,25 @@ fn spawn_from_other_thread_unpark() {
).unwrap();
}

#[test]
fn spawn_from_executor_with_handle() {
let mut current_thread = CurrentThread::new();
let handle = current_thread.handle();
let (tx, rx) = oneshot::channel();

current_thread.spawn(lazy(move || {
handle.spawn(lazy(move || {
tx.send(()).unwrap();
Ok(())
})).unwrap();
Ok::<_, ()>(())
}));

current_thread.run();

rx.wait().unwrap();
}

fn ok() -> future::FutureResult<(), ()> {
future::ok(())
}

0 comments on commit ed3ece2

Please sign in to comment.