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

Documentation of Runtime::enter features unfortunate, slightly nondeterministic, and thus quite confusing example #6350

Closed
steffahn opened this issue Feb 16, 2024 · 1 comment · Fixed by #6351
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-runtime Module: tokio/runtime T-docs Topic: documentation

Comments

@steffahn
Copy link
Contributor

steffahn commented Feb 16, 2024

The bad user experience is demonstrated in this question thread on URLO. The underlying problem is that the user got the example code from Runtime::enter docs

use tokio::runtime::Runtime;

fn function_that_spawns(msg: String) {
    // Had we not used `rt.enter` below, this would panic.
    tokio::spawn(async move {
        println!("{}", msg);
    });
}

fn main() {
    let rt = Runtime::new().unwrap();

    let s = "Hello World!".to_string();

    // By entering the context, we tie `tokio::spawn` to this executor.
    let _guard = rt.enter();
    function_that_spawns(s);
}

and then tried out modifications to see how they behave. The problem is that this code turns out to print “Hello World” quite likely, but not necessarily, whereas other modified versions of it might have other random tendencies; before a user discovers the nondeterminism, this can cause lots of confusion.

In my opinion, the example at least needs updating that clearly calls out the non-determined nature of this example program’s behavior. Furthermore, I would appreciate a more natural example that doesn’t feature such randomness at all… though off the top of my head, I’m not sure what the most idiomatic use-case of Runtime::enter is in the first place, since avoiding premature cancellation of spawned tasks usually involves using Runtime::block_on and once you use Runtime::block_on, you don’t really need explicit Runtime::enter anymore…


I’m not sure if “bug” or “feature-request” is the correct category for documentation issues.

@steffahn steffahn added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Feb 16, 2024
@Darksonn Darksonn added T-docs Topic: documentation M-runtime Module: tokio/runtime labels Feb 16, 2024
@maminrayej maminrayej changed the title Documentation of Runtime::enter featues unfortunate, slightly nondeterministic, and thus quite confusing example Documentation of Runtime::enter features unfortunate, slightly nondeterministic, and thus quite confusing example Feb 16, 2024
@Darksonn
Copy link
Contributor

Thank you for reporting this. Documentation bugs are definitely bugs.

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 T-docs Topic: documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants