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: interleaving multiple calls to Handle::enter() does not work #5756

Closed
carllerche opened this issue Jun 3, 2023 · 2 comments · Fixed by #5772
Closed

rt: interleaving multiple calls to Handle::enter() does not work #5756

carllerche opened this issue Jun 3, 2023 · 2 comments · Fixed by #5772
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-runtime Module: tokio/runtime

Comments

@carllerche
Copy link
Member

When calling Handle::enter() multiple times and dropping guards in an unexpected order, the "current runtime" is set incorrectly.

This is because the guard stores the previous state and updates the current runtime on drop.

Repro:

#[test]
fn interleave_enter() {
    let rt1 = rt();
    let rt2 = rt();
    let rt3 = rt();

    let _enter1 = rt1.enter();
    let enter2 = rt2.enter();
    let enter3 = rt3.enter();

    drop(enter2);
    drop(enter3);

    drop(rt2);

    let handle = tokio::spawn(async {});
    rt1.block_on(handle).unwrap();
}
@carllerche carllerche added C-bug Category: This is a bug. A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime labels Jun 3, 2023
@carllerche
Copy link
Member Author

@Darksonn Why do we check that EnterGuard is Send here? It would not be good for the guard to move threads...

@carllerche
Copy link
Member Author

carllerche commented Jun 3, 2023

Unfortunately, we can't deprecate the method. As far as I can see, the options are:

  • panic if the guards are dropped out of order.
  • Maintain a linked list of "previous" states. This could be intrusive when the API is a closure block_on but enter() will need to allocate.

Either way, EnterGuard should not be Send (Sync is fine).

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 C-bug Category: This is a bug. M-runtime Module: tokio/runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant