Repository navigation
fs: skip io_uring path gracefully when IO driver is disabled - #8377
philphauler wants to merge 3 commits into
Conversation
When a Tokio runtime is built without `enable_io()`, the io_uring filesystem probe in `tokio::fs` operations panics because `driver().io()` calls `.expect()` on a `None` IO handle. Add `Handle::try_io()` which returns `Option<&Handle>` instead of panicking, and update all seven fs callsites to use it. When the IO driver is absent, the io_uring path is skipped and the existing `spawn_blocking` fallback runs as expected. Affected: `fs::read`, `fs::write`, `fs::rename`, `fs::try_exists`, `fs::OpenOptions::open`, `File::poll_read_inner`, and `File::lazy_init_read`. Fixes tokio-rs#8164
try_io() is only called from io_uring fs paths, so it should be gated behind cfg_io_uring! rather than cfg_io_driver! to avoid a dead_code error when clippy runs without tokio_unstable + io-uring.
Daksh14
left a comment
There was a problem hiding this comment.
Thanks for catching this! Also some tests would be great, these are some great candidates.
- Test with io disabled + io_uring and tokio_unstable
- Test with io enabled + io_uring and tokio_unstable
- Test with io enabled + io_uring enabled/disabled and tokio_unstable
| Kind::Uring(opts) => { | ||
| let handle = crate::runtime::Handle::current(); | ||
| let driver_handle = handle.inner.driver().io(); | ||
| let use_uring = match handle.inner.driver().try_io() { |
There was a problem hiding this comment.
I would rename this to has_io as try is for Result resulting functions
There was a problem hiding this comment.
Done in f0fa514 — renamed to has_io, returns bool.
|
@Daksh14 Onnit g 🫡 |
|
Hi Daksh14 The As noted in the PR body, #8165 by @fallintoplace addresses the same panic. This PR covers the same sites plus Tests are in |
|
Damn! U right there g, imma tweak 🫡⚡️ |
| let driver = handle.inner.driver(); | ||
| if driver.has_io() | ||
| && driver | ||
| .io() | ||
| .check_and_init(io_uring::opcode::Read::CODE) | ||
| .await? |
There was a problem hiding this comment.
You are repeating logic like this in several places. Could we have a method on driver() called check_io_uring that takes an opcode, and internally performs the has_io() check followed by the check_and_init() check? That way we can just call that combined method in the places where we need to check whether there is io-uring and it has a certain opcode.
|
Please make your PR description less verbose. |
|
I'd like to close this PR in favor of @fallintoplace 's PR #8165 hopefully its okay with you? They are the same change very well needed with new io_uring semantics. Thanks a lot for your contribution anyways! |
|
Absolutely, that makes sense — #8165 covers the same ground. Happy to close this in favour of @fallintoplace's PR. The io_uring path was a real gap and glad it's getting fixed either way. If any of the test coverage from this PR (the Thanks for the review, @Daksh14! |
Motivation
When a Tokio runtime is built without
enable_io()and the crate is compiledwith
tokio_unstable+io-uring, filesystem operations liketokio::fs::readpanic before reaching the existing
spawn_blockingfallback:The seven
tokio::fscallsites that probe the io_uring driver all calldriver().io(), which panics via.expect()on a disabled IO handle.Fixes #8164
Related: #8165 fixes the same panic for
read,write,OpenOptions::openand
Filereads and was approved in May, but has been conflicted since July.This PR also covers
renameandtry_exists.Solution
Add
driver::Handle::has_io() -> boolnext to the panickingio()accessorand guard each io_uring probe with it, so a runtime without an IO driver falls
through to the existing blocking path.
Changes
runtime/driver.rs: addhas_io()(gated bycfg_io_uring!)fs/read.rs,fs/write.rs,fs/rename.rs,fs/try_exists.rs,fs/open_options.rs,fs/file.rs(poll_read_innerandlazy_init_read):probe io_uring only when
has_io()tests/fs_io_driver.rs:io_disabled,io_enabled,io_uring_enabledrun
write/read/OpenOptions::open/File::from_stdread /rename/try_existson both schedulers. Not gated on theio-uringfeature, so CIruns it with and without io_uring compiled in.
io_uring_enabledalsochecks the uring path is still taken (no blocking thread spawned).
All source changes are inside the existing
#[cfg(all(tokio_unstable, feature = "io-uring", ...))]blocks; thespawn_blockingfallback is unchanged.Testing
--cfg tokio_unstable --features full,test-util,io-uring, allfs_*tests: pass (3/3 infs_io_driver)io-uringfeature; withouttokio_unstable; on Windows both ways; under miri (--features full): 2/2 eachkernel.io_uring_disabled=2(EPERM): 3/3 pass (io_uring_available returns false, assertion skipped)io-uring+tokio_unstable:io_disabledfails with the panic above attokio/src/fs/write.rs:39:51;io_enabled/io_uring_enabledpassio-uring: passcargo fmt --checkclean;cargo clippy --all-features -Dwarningsclean