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

tokio::fs::File::open errors with "blocking annotated I/O must be called from the context of the Tokio runtime." #1356

Closed
repnop opened this issue Jul 25, 2019 · 9 comments

Comments

@repnop
Copy link

repnop commented Jul 25, 2019

Version

Git master branch

Platform

64-bit Windows 10

Subcrates

tokio-fs

Description

Minimal reproduction:

use tokio::fs::File;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    File::open("Cargo.toml").await?;
    Ok(())
}

This will error with

Error: Custom { kind: Other, error: "`blocking` annotated I/O must be called from the context of the Tokio runtime." }

even when using the default multithreaded runtime. Manually starting the runtime doesn't seem to work either.

@carllerche
Copy link
Member

Could you include the list of crates you got pulled in (including deps). Odds are you are mixing git deps & crates.io deps.

@repnop
Copy link
Author

repnop commented Jul 25, 2019

Cargo.toml:

[dependencies]
tokio = { git = "https://github.com/tokio-rs/tokio" }

From my Cargo.lock:

[[package]]
name = "tokio"
version = "0.2.0"
source = "git+https://github.com/tokio-rs/tokio#298be802492068d28b972069c5f654de124fd510"
dependencies = [
 "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
 "futures-core-preview 0.3.0-alpha.17 (registry+https://github.com/rust-lang/crates.io-index)",
 "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "tokio-codec 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-current-thread 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-executor 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-fs 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-io 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-macros 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-reactor 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-sync 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-tcp 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-threadpool 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-timer 0.3.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-udp 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-uds 0.3.0 (git+https://github.com/tokio-rs/tokio)",
 "tracing-core 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tokio-fs"
version = "0.2.0"
source = "git+https://github.com/tokio-rs/tokio#298be802492068d28b972069c5f654de124fd510"
dependencies = [
 "futures-core-preview 0.3.0-alpha.17 (registry+https://github.com/rust-lang/crates.io-index)",
 "tokio-io 0.2.0 (git+https://github.com/tokio-rs/tokio)",
 "tokio-threadpool 0.2.0 (git+https://github.com/tokio-rs/tokio)",
]

@Proximyst
Copy link

Would just like to note that the issue doesn't seem to be platform-dependent.

0 proximyst@proximyst-arch:~/tokio_test|HEAD⚡?
λ cargo run
   Compiling tokio_test v0.1.0 (/home/proximyst/tokio_test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.82s
     Running `target/debug/tokio_test`
Error: Custom { kind: Other, error: "`blocking` annotated I/O must be called from the context of the Tokio runtime." }
1 proximyst@proximyst-arch:~/tokio_test|HEAD⚡?
λ uname -a
Linux proximyst-arch 5.1.16-arch1-1-ARCH #1 SMP PREEMPT Wed Jul 3 20:23:07 UTC 2019 x86_64 GNU/Linux

The code is simply the minimal reproduction example.

@carllerche
Copy link
Member

Ah! Ok, I see the problem... This is due to #1329...

I'm not sure what the best short term fix... the longer term fix is to finish a bunch of planned work...

@madadam
Copy link
Contributor

madadam commented Aug 2, 2019

I hit this same issue, but with #[tokio::test]. My workaround is to do something like this:

fn main() ->  Result<(), Box<dyn std::error::Error>>  {
    run(async {
        File::open("Cargo.toml").await?;
        Ok(())    
    })
}

fn run<F>(f: F) -> F::Output
where
    F: Future + Send + 'static,
    F::Output: Send + Debug,
{
    let (tx, rx) = tokio::sync::oneshot::channel();
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.spawn(async move {
        tx.send(f.await).unwrap();
    });
    rt.block_on(rx).unwrap()
}

@eminence
Copy link

eminence commented Aug 9, 2019

The tokio::io::Stdin type is impacted by this as well:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut stdin = tokio::io::stdin();
    let mut s = String::new();
    stdin.read_to_string(&mut s).await?;

    Ok(())
}

jebrosen added a commit to rwf2/Rocket that referenced this issue Aug 20, 2019
… operations.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
@carllerche
Copy link
Member

Fixed by #1329.

@jebrosen
Copy link
Contributor

Was that supposed to be #1495? I thought #1329 had introduced this error.

@carllerche
Copy link
Member

Whoops you are right. Copy / paste fail

jebrosen added a commit to rwf2/Rocket that referenced this issue Sep 7, 2019
… operations.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
jebrosen added a commit to rwf2/Rocket that referenced this issue Sep 21, 2019
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
jebrosen added a commit to jebrosen/Rocket that referenced this issue Dec 11, 2019
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
SergioBenitez pushed a commit to rwf2/Rocket that referenced this issue Jul 10, 2020
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
SergioBenitez pushed a commit to rwf2/Rocket that referenced this issue Jul 10, 2020
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
SergioBenitez pushed a commit to rwf2/Rocket that referenced this issue Jul 11, 2020
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
SergioBenitez pushed a commit to rwf2/Rocket that referenced this issue Jul 11, 2020
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
SergioBenitez pushed a commit to rwf2/Rocket that referenced this issue Jul 11, 2020
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
SergioBenitez pushed a commit to rwf2/Rocket that referenced this issue Jul 11, 2020
of futures-rs executor.

Despite this change, using body_bytes_wait on (for example) a File will
still fail due to tokio-rs/tokio#1356.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants