Skip to content
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

Merged
merged 2 commits into from Jun 18, 2021
Merged

rt: avoid early task shutdown #3870

merged 2 commits into from Jun 18, 2021

Conversation

carllerche
Copy link
Member

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.

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
@carllerche carllerche changed the title rt: avoid double task shutdown rt: avoid early task shutdown Jun 18, 2021
@carllerche carllerche requested a review from Darksonn June 18, 2021 21:34
@@ -9,6 +9,7 @@
rust_2018_idioms,
unreachable_pub
)]
#![deny(unused_must_use)]
Copy link
Member Author

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.

Copy link
Member Author

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);
Copy link
Member Author

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.

Copy link
Contributor

@Darksonn Darksonn left a 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.

@carllerche
Copy link
Member Author

I'll publish a release later today once CI passes.

@carllerche carllerche merged commit 7601dc6 into tokio-1.7.x Jun 18, 2021
@carllerche carllerche deleted the fix-3869 branch June 18, 2021 23:32
Comment on lines +451 to +459
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);
}

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? 🤔

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants