mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
current-thread: fix shutdown on idle (#763)
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
This commit is contained in:
parent
9b1a45cc6a
commit
ed3ece266b
@ -653,6 +653,13 @@ impl Handle {
|
|||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + Send + 'static,
|
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() {
|
if self.shut_down.get() {
|
||||||
return Err(SpawnError::shutdown());
|
return Err(SpawnError::shutdown());
|
||||||
}
|
}
|
||||||
@ -669,13 +676,6 @@ impl Handle {
|
|||||||
return Err(SpawnError::shutdown());
|
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))
|
self.sender.send(Box::new(future))
|
||||||
.expect("CurrentThread does not exist anymore");
|
.expect("CurrentThread does not exist anymore");
|
||||||
// use 0 for the id, CurrentThread does not make use of it
|
// use 0 for the id, CurrentThread does not make use of it
|
||||||
|
@ -778,6 +778,25 @@ fn spawn_from_other_thread_unpark() {
|
|||||||
).unwrap();
|
).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<(), ()> {
|
fn ok() -> future::FutureResult<(), ()> {
|
||||||
future::ok(())
|
future::ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user