{"record":{"id":"94bc45e41fe7e162","repo":"tokio-rs/tokio","slug":"background-task-failed-94bc45","errorCode":null,"errorMessage":"background task failed","messagePattern":"background task failed","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"tokio/src/fs/mod.rs","lineNumber":327,"sourceCode":"\n    pub(crate) use self::open_options::UringOpenOptions;\n}\n\nuse std::io;\n\n#[cfg(not(test))]\nuse crate::blocking::spawn_blocking;\n#[cfg(test)]\nuse mocks::spawn_blocking;\n\npub(crate) async fn asyncify<F, T>(f: F) -> io::Result<T>\nwhere\n    F: FnOnce() -> io::Result<T> + Send + 'static,\n    T: Send + 'static,\n{\n    match spawn_blocking(f).await {\n        Ok(res) => res,\n        Err(_) => Err(io::Error::new(\n            io::ErrorKind::Other,\n            \"background task failed\",\n        )),\n    }\n}\n","sourceCodeStart":309,"sourceCodeEnd":333,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio/src/fs/mod.rs#L309-L333","documentation":"Runtime error from `tokio::fs::asyncify` (fs/mod.rs:327), which backs `File::open`, `create`, `metadata`, and other blocking-syscall wrappers. If `spawn_blocking(f).await` yields a `JoinError` (the task panicked, was cancelled, or the runtime is shutting down), it is converted to `io::ErrorKind::Other` with this message.","triggerScenarios":"The blocking task panicked; the runtime cancelled the blocking task during shutdown; calling `asyncify` (directly or via `tokio::fs::*`) outside/after the runtime lifetime.","commonSituations":"Runtime shut down while a `tokio::fs` operation was in flight; a blocking syscall closure panicked; using `tokio::fs::*` from a thread that is not inside a Tokio runtime; runtime driver/runtime dropped mid-await.","solutions":["Keep the runtime alive until all `tokio::fs` operations resolve.","Do not panic inside closures that run via `asyncify`/`spawn_blocking`.","Run all `tokio::fs` calls within the runtime context (`#[tokio::main]` or a runtime you drive).","Handle `io::ErrorKind::Other` from `asyncify`-backed calls as a possible shutdown/panic signal and propagate."],"exampleFix":"// before: fs call after the runtime is gone\nlet f = tokio::fs::File::open(\"p\").await?; // 'background task failed' if runtime dropped\n\n// after: keep fs calls inside the live runtime and handle shutdown\nasync fn run() -> io::Result<()> {\n    let _f = tokio::fs::File::open(\"p\").await.map_err(|e| {\n        if e.kind() == io::ErrorKind::Other {\n            io::Error::new(e.kind(), \"fs operation failed (runtime shutdown?)\")\n        } else { e }\n    })?;\n    Ok(())\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"fn is_bg_task_failed(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::Other && e.to_string() == \"background task failed\"\n}","tryCatchPattern":"let f = tokio::fs::File::open(\"p\").await.map_err(|e| {\n    if is_bg_task_failed(&e) { anyhow!(\"fs operation failed: runtime shutdown or blocking task panic\") } else { e.into() }\n})?;","preventionTips":["Drive the runtime until every `tokio::fs` operation resolves.","Do not panic inside closures passed to `asyncify`/`spawn_blocking`.","Run all `tokio::fs` calls within a Tokio runtime context."],"tags":["rust","tokio","fs","runtime","shutdown","blocking","runtime"],"backgroundTag":null,"analyzedSha":"625954f365727668cb02d04172b34f1149637728","analyzedAt":"2026-08-11T17:46:45.378Z","contentChangedAt":"2026-08-11T17:46:45.378Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}