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
rt: avoid early task shutdown #3870
Conversation
Tokio 1.7.0 introduced a change intended to eagerly shutdown newly spawned tasks if the runtime is in the process of shutting down. However, it introduced a bug where already spawned tasks could be shutdown too early, resulting in the potential introduction of deadlocks if tasks acquired mutexes in drop handlers. Fixes #3869
@@ -9,6 +9,7 @@ | |||
rust_2018_idioms, | |||
unreachable_pub | |||
)] | |||
#![deny(unused_must_use)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, any unhandled Result
values should result in a compilation failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not directly related, so if needed, I can split it off to another PR.
drop(p); | ||
task.shutdown(); | ||
return; | ||
return Err(task); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix is to push the task.shutdown()
call out to a point where we know if the task is newly spawned or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix looks good to me.
I'll publish a release later today once CI passes. |
fn drop(&mut self) { | ||
println!("drop {} start", self.put_waker); | ||
let mut lock = self.shared.lock().unwrap(); | ||
if !self.put_waker { | ||
lock.waker.take().unwrap().wake(); | ||
} | ||
drop(lock); | ||
println!("drop {} stop", self.put_waker); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reproduce this bug, it requires the task with put_waker==false
(f2
) to be dropped first to let it wakes up f1
.
On my laptop it seems that the last spawned will always be dropped first. Do we have a guarantee about this behavior or some documents describing it? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's any way to get any guarantees about the drop order.
Tokio 1.7.0 introduced a change intended to eagerly shutdown newly
spawned tasks if the runtime is in the process of shutting down.
However, it introduced a bug where already spawned tasks could be
shutdown too early, resulting in the potential introduction of deadlocks
if tasks acquired mutexes in drop handlers.
Fixes #3869
The PR is based on the
tokio-1.7.x
branch and will be released as 1.7.1.