Executive Summary & Direct Answer

RUSTSEC-2026-0386 identifies an I/O safety flaw in Tokio’s safe AsyncFd constructors: they accept any AsRawFd, including a raw, closed, or replaceable descriptor. That can let safe code create an invalid BorrowedFd or cause the runtime to deregister a reused descriptor and free memory still referenced by an I/O event. Use patched Tokio and its unsafe registration API, and uphold the documented invariant: the descriptor stays valid, open, and unchanged for the entire AsyncFd lifetime.

The key production fix is not merely replacing a method name. The caller must establish that invariant—preferably by moving an owning descriptor such as OwnedFd into AsyncFd—and prevent it from being closed or replaced while registered. The unsafe boundary should be small, documented, and tested against the actual OS I/O driver.

Environment and baseline

This guide addresses Unix AsyncFd and the issue tracked as RUSTSEC-2026-0386, reported October 13, 2026, and issued October 23, 2026. The advisory lists AsyncFd::new and with_interest as affected from Tokio 0.3.2, and try_new and try_with_interest from Tokio 1.37.0. It lists Tokio <0.3.2 as unaffected and patched releases 1.51.6, 1.53.3, and 1.54.0 for their respective release lines.

Use Rust 1.82+ (Edition 2021) and a patched Tokio release. The example pins Tokio 1.54.0; review the advisory and your dependency resolution before selecting a version in another release line.

[dependencies]
tokio = { version = "=1.54.0", features = ["rt", "macros", "net"] }

AsyncFd is Unix-specific. Gate modules that use it with #[cfg(unix)]. Check the resolved version, not just the manifest’s requested range:

cargo tree -i tokio
cargo audit

For builds that permit compatible Tokio updates, use a patched minimum version appropriate to your release line and commit Cargo.lock for deployable binaries.

The unsafe assumption in the old constructors

The affected constructors accepted T: AsRawFd. That trait only exposes a raw integer descriptor; it does not prove that the descriptor is valid, owned, or kept open. RawFd itself implements AsRawFd, so a safe call could register an arbitrary integer:

use std::os::fd::RawFd;
use tokio::io::unix::AsyncFd;

// A safe constructor could accept a raw descriptor that is invalid,
// already closed, or later closed and reused.
let fd: RawFd = 42;
let registered = AsyncFd::new(fd);

This code illustrates the unsound API boundary; do not use it. The root cause is that a raw descriptor is a process-local number, not a stable ownership handle. Closing it and opening another resource can cause the operating system to reuse the same number.

The flaw has two related consequences:

  1. Invalid borrowed handle. Tokio’s AsyncFd<T> implements AsFd for T: AsRawFd by calling BorrowedFd::borrow_raw(self.as_raw_fd()). That operation requires the descriptor to remain valid for the borrow’s lifetime. Accepting an arbitrary or subsequently closed descriptor cannot uphold that guarantee.
  2. Stale driver registration and use-after-free. On Unix, Tokio’s I/O driver stores a pointer to an internal allocation in epoll/kqueue user data. If the registered descriptor is duplicated and then closed while AsyncFd remains alive, the OS can reuse its number for another resource on the same runtime. Dropping the first AsyncFd may then deregister the reused descriptor number and free the allocation while the original file description remains registered. A later readiness event can access freed memory.

The second failure is not just “readiness on the wrong socket.” It is a lifetime mismatch between the kernel’s saved user data and Tokio’s registration allocation. Descriptor-number reuse connects the stale registration to a use-after-free.

Production fix: own the descriptor and register it once

Tokio’s patched API deprecates the safe constructors in favor of unsafe AsyncFd::register and AsyncFd::register_with_interest. The caller must guarantee that the descriptor stays valid, open, and unchanged for the lifetime of the AsyncFd.

The most maintainable pattern is to move an owning descriptor into AsyncFd. OwnedFd closes its descriptor on drop, and moving it into the registration ties the descriptor’s ownership to the registered value:

#[cfg(unix)]
use std::os::fd::OwnedFd;
#[cfg(unix)]
use tokio::io::unix::AsyncFd;

#[cfg(unix)]
fn register_owned(fd: OwnedFd) -> std::io::Result<AsyncFd<OwnedFd>> {
    // SAFETY: `fd` owns the descriptor and is moved into `AsyncFd`.
    // The caller does not retain another owner that can close or replace
    // this descriptor number while the returned AsyncFd is alive.
    unsafe { AsyncFd::register(fd) }
}

If your application needs a specific readiness interest, use register_with_interest and apply the same safety argument. The exact interest should reflect how the resource is used; it does not weaken the descriptor-lifetime requirement.

The safety comment must describe the actual invariant, not merely say “the fd is valid.” In particular, audit whether any other code can close the descriptor through a raw handle, call dup2/dup3 to replace its number, or otherwise invalidate the registration while AsyncFd exists. OwnedFd helps establish ownership, but unsafe code elsewhere can still violate the invariant. Do not create multiple independently dropped OwnedFd values from one raw descriptor.

When a caller receives a raw descriptor from an external API, convert it to an owning type only if ownership is actually being transferred. If ownership remains elsewhere, coordinate lifetimes and closure explicitly; wrapping a borrowed descriptor as OwnedFd would create double-close or premature-close hazards. Prefer redesigning the API to transfer ownership rather than relying on an undocumented convention.

Keep the unsafe boundary small

A library can centralize registration in one constructor and expose only the safe operations its users need. For example, an internal wrapper should accept OwnedFd, not RawFd, and should not expose a method that lets callers close or replace the registered descriptor.

#[cfg(unix)]
pub struct RegisteredFd {
    inner: tokio::io::unix::AsyncFd<std::os::fd::OwnedFd>,
}

#[cfg(unix)]
impl RegisteredFd {
    pub fn new(fd: std::os::fd::OwnedFd) -> std::io::Result<Self> {
        // SAFETY: ownership of the descriptor moves into AsyncFd. This
        // wrapper does not expose a way to close or replace it while live.
        let inner = unsafe { tokio::io::unix::AsyncFd::register(fd)? };
        Ok(Self { inner })
    }

    pub fn get_ref(&self) -> &std::os::fd::OwnedFd {
        self.inner.get_ref()
    }
}

Returning &OwnedFd does not transfer ownership, but consider whether exposing even that reference is necessary. In particular, do not add an API returning the raw descriptor together with instructions that consumers must “not close it.” That convention can be violated by safe callers. The design goal is to make descriptor ownership and the registration lifetime coincide.

This ownership-oriented design fits systems that already separate transport lifetimes from request handling, including services built with Hyper. It is also useful when integrating custom Unix resources into a larger async service; the I/O driver’s registration lifetime must remain aligned with the resource owner, not merely with a task’s local variable.

Version and API trade-offs

The table distinguishes the advisory’s affected API surface from the recommended ownership-based approach. The guarantees column is about the descriptor contract; an unsafe constructor cannot enforce that contract on its own.

ApproachVersion/statusDescriptor guaranteeOperational consequence
Safe new / with_interestAffected from Tokio 0.3.2Accepts AsRawFd; ownership and stability are not establishedDo not use on affected releases
Safe try_new / try_with_interestAffected from Tokio 1.37.0Fallible registration does not make the descriptor lifetime safeDo not treat Result as an I/O safety check
Unsafe register with OwnedFdPatched API in Tokio 1.51.6, 1.53.3, and 1.54.0 release linesCaller establishes validity, openness, and identity for the full lifetimeOne explicit unsafe boundary; ownership is tied to registration
Unsafe register with raw/non-owning handlePatched APISafe only if external synchronization guarantees the complete lifetime invariantMore coordination and a larger audit burden

These are qualitative engineering trade-offs, not benchmark timings: the issue is memory safety, not a throughput regression. Registration still uses the runtime’s I/O driver; the main cost of the fix is making ownership explicit and auditing unsafe call sites. A fallible constructor can report registration errors, but it cannot prove that another part of the program will not close the descriptor afterward.

Production pitfalls and edge cases

  • Do not infer ownership from AsRawFd. It is an accessor trait. A type implementing it may borrow a descriptor, share it, or return a descriptor that can change.
  • Do not close the registered descriptor through a duplicate-number operation. A duplicated descriptor has a different descriptor number, but closing or replacing the number Tokio registered can still invalidate the registration. Keep the exact registered number stable.
  • Do not confuse file-description identity with descriptor identity. dup creates another descriptor referring to the same open file description. The kernel registration and Tokio bookkeeping are associated with the registered descriptor number and its lifetime behavior.
  • Do not assume registration failure cures invalid ownership. An Ok result establishes that registration succeeded at that moment; it does not ensure the descriptor remains open afterward.
  • Do not rely on Drop ordering across unrelated owners. Ensure the AsyncFd is dropped before any external component can close or replace the descriptor it uses. Prefer a single owner rather than a documented ordering convention.
  • Do not assume Miri proves this OS interaction safe. Miri can detect many Rust-level undefined behaviors, but it does not reproduce the kernel’s epoll/kqueue event delivery and descriptor reuse behavior.

If this descriptor feeds a custom protocol implementation, test lifecycle behavior at the integration boundary rather than only testing a wrapper’s type signatures. The same discipline applies when services route requests through Axum: framework-level tests do not replace tests for the underlying registered resource.

Differential verification workflow

First ensure the build resolves to a fixed Tokio release and inspect all call sites for deprecated constructors:

cargo tree -i tokio
rg 'AsyncFd::(new|with_interest|try_new|try_with_interest)' src tests
cargo clippy --all-targets --all-features -- -D warnings
cargo test --all-targets

Then run a Unix integration test that exercises the actual runtime and descriptor lifecycle. A useful test creates a pipe or socket pair, transfers the registered end into OwnedFd, registers it, drives readiness on the Tokio runtime, and verifies that dropping the AsyncFd closes only the owned descriptor. Add a stress test that repeatedly opens and closes unrelated descriptors while the registration is alive; this is useful for exercising descriptor-number reuse, but a passing stress test is not proof of safety.

For a before/after regression check, compile the test against the affected dependency in an isolated worktree and the patched dependency in the main worktree. The old API may compile because its unsafe assumption was hidden; compilation success is not evidence of soundness. On the patched build, assert that all registration sites go through the unsafe API with a reviewed invariant, and verify readiness and drop behavior on each supported Unix target. cargo miri test can supplement Rust-level checks, but it does not substitute for this OS-level integration test.

Frequently Asked Questions (FAQ)

Which Tokio versions are affected by RUSTSEC-2026-0386?

The advisory lists AsyncFd::new and with_interest as affected from Tokio 0.3.2, and try_new and try_with_interest from Tokio 1.37.0. It lists patched releases 1.51.6, 1.53.3, and 1.54.0 for their respective release lines; check the advisory for the status of the specific version you deploy.

Why does AsyncFd::try_new not make an invalid descriptor safe?

Fallibility can report a registration error, but it cannot prove that the descriptor will remain open and unchanged after registration. The unsoundness concerns the descriptor’s lifetime and identity as well as whether registration initially succeeds.

Is passing OwnedFd to AsyncFd::register sufficient?

It provides a strong ownership model because the descriptor owner moves into AsyncFd, but the caller must still uphold the unsafe API’s lifetime and identity requirements. Avoid exposing ways to close or replace the registered descriptor, and do not construct duplicate owning handles for the same descriptor number.

Learn more with useful resources