Skip to content

io_uring_setup failure panics: Torrent::start unwraps the ring build #149

Closed

Description

@grenade

Torrent::start unwraps the io_uring ring build, so a client that runs one ring per torrent panics instead of getting an error.

bittorrent/src/torrent.rs:150-157 (master, 240e544):

// check ulimit
let ring: IoUring = IoUring::builder()
    .setup_single_issuer()
    .setup_clamp()
    .setup_cqsize(self.state.config.cq_size)
    .setup_defer_taskrun()
    .setup_coop_taskrun()
    .build(self.state.config.sq_size)
    .unwrap();

io_uring_setup fails with ENOMEM well before the machine is out of memory, because the kernel charges ring memory against RLIMIT_MEMLOCK — a byte budget shared by every process the user is running, not per-process.

Measurements

On Fedora 44, kernel 7.1.5, ulimit -l = 8192 KiB (soft and hard; the systemd default, and not raisable without privileges). Creating rings with exactly the builder above until failure, each figure stable across repeated rounds:

sq_size = cq_size rings created ≈ cost per ring
64 593 ~14 kB
256 197 ~42 kB
512 107 ~76 kB
1024 53 ~154 kB
4096 (default) 13 ~630 kB

Inverse-linear in ring size, which is what a byte-denominated budget looks like.

strace confirms it is io_uring_setup itself, not a later mmap:

io_uring_setup(4096, {flags=IORING_SETUP_CQSIZE|IORING_SETUP_CLAMP|IORING_SETUP_COOP_TASKRUN
                     |IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_DEFER_TASKRUN, …}) = 3
io_uring_setup(4096, {flags=…}) = -1 ENOMEM (Cannot allocate memory)

Why this bites

At the default cq_size/sq_size of 4096, 13 rings exhaust the entire per-user budget — for the whole login session, not just the client. A desktop with unrelated processes using io_uring (node, python tooling) leaves fewer.

I hit this in monsoon, which uses vortex-bittorrent as a library and creates one ring per torrent. With 12 torrents in its registry, every scope thread panicked at startup within ~600 ms:

thread '<unnamed>' panicked at vendor/vortex-bittorrent/src/torrent.rs:157:14:
called `Result::unwrap()` on an `Err` value:
  Os { code: 12, kind: OutOfMemory, message: "Cannot allocate memory" }

At the time the user's session held 135 io_uring fds across 42 processes. Killing the client restored capacity to exactly 13.

This is a resource condition a client can react to — back off, lower concurrency, reduce ring size — but only if it is given the chance. Torrent::start already returns Result<(), Error> and Error::Io(#[from] io::Error) already exists, so propagating is a one-character change with no signature or API impact.

Two things worth your view before I open a PR

Scope. cli/src/main.rs:446 does torrent.start(event_tx, cmd_rc, listener).unwrap() inside a spawned closure, so the library change alone leaves vortex-cli behaviour unchanged. Happy to keep the PR to the library and leave the CLI to you, or to handle the call site too — whichever you prefer.

Defaults. Separately from the panic: is cq_size/sq_size = 4096 the right default given it costs ~630 kB of an 8 MiB shared budget per ring? A per-torrent event loop's queue depth is bounded by peer count and outstanding subpiece requests. Dropping to 256 would give ~15x the headroom. That is an opinionated performance change though, so I have not touched it — worth its own issue if you think it has legs.

I have a patch ready for the propagation change and can open a PR against this issue.

Activity

  1. Nehliin commented on Aug 8, 2026

    @Nehliin
    Owner

    yeah the 4096 is a bit big perhaps, I want to add a check in the torrent start also to see that not only does it fit within the ulimit but that the ulimit is reasonable given the config (as you probably have noted you can change the cq/sq size via the config). That is, it gives a bit of headroom to other applications

  2. added a commit that references this issue on Oct 12, 2026
    bdd6953
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions