Skip to content

Issue#151 - #172

Open
4m1z wants to merge 1 commit into
Nehliin:masterfrom
4m1z:issue#151
Open

4m1z wants to merge 1 commit into
Nehliin:masterfrom
4m1z:issue#151

Conversation

@4m1z

@4m1z 4m1z commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes #151.

Check descriptor capacity before startup, allowing for configured peers, the new ring, and 32 spare slots. Handle sparse descriptor tables and report actionable errors before opening the TUI.

Testing with NOFILE=64 reproduced failures with the default 128-peer configuration. MEMLOCK work is tracked separately in #175.

Verified: 193 tests passed, plus formatting, clippy, and CLI checks. The Transmission-backed test was skipped because its container was unavailable.

Implementation and tests were AI-assisted.

@4m1z

4m1z commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

I started with RLIMIT_NOFILE after reproducing Too many open files with a limit of 64. After reading #149 and your comment about queue sizes and headroom, I included RLIMIT_MEMLOCK too, to cover the memory side of the startup check.

Would you prefer to keep this PR focused on RLIMIT_NOFILE? I can move the RLIMIT_MEMLOCK checks and policy settings into a separate PR.

Comment thread .github/workflows/ci.yaml Outdated
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@nextest
- uses: taiki-e/install-action@just
- name: Install syscall fault-injection test dependency

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strace lets the tests force ENOMEM during ring setup and buffer registration
without exhausting memory on the CI runner.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice!

}
}

fn minimum_descriptor_limit(occupied: &[u32], additional: u128) -> Result<u128, Error> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing descriptors can remain open above a lowered soft limit. Counting every open descriptor would therefore give the wrong number of available slots. This walks the descriptor numbers and also accounts for occupied slots between the current limit and the higher limit suggested in the error message.

Comment thread bittorrent/src/io.rs Outdated
impl<Q: SubmissionQueue> Io<Q> {
pub fn new(sq: BackloggedSubmissionQueue<Q>, config: &Config) -> Self {
Self {
pub fn new(sq: BackloggedSubmissionQueue<Q>, config: &Config) -> io::Result<Self> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Io::new now returns read-buffer initialization errors instead of panicking. The added unwrap calls are in tests constructing valid fixtures; the production caller propagates the error with ?.
Also keep the compatibility note in the PR description: existing TOML files work through defaults, but exhaustive Rust Config initializers need the new fields.

@4m1z
4m1z marked this pull request as ready for review November 15, 2026 16:45
@Nehliin

Nehliin commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thank you for you contribution and clarifying comments! Really nice to add the fault injection via strace :) Overall it looks good but I would prefer to split this up to smaller pieces if that's alright. Makes it a bit easier to review as well.

I think one pr for the RLIMIT_NOFILE one for RLIMIT_MEMLOCK and one for the actual user facing cli code tying everything together could make sense but let me know if you want to split it in another way.

@4m1z

4m1z commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for you contribution and clarifying comments! Really nice to add the fault injection via strace :) Overall it looks good but I would prefer to split this up to smaller pieces if that's alright. Makes it a bit easier to review as well.

I think one pr for the RLIMIT_NOFILE one for RLIMIT_MEMLOCK and one for the actual user facing cli code tying everything together could make sense but let me know if you want to split it in another way.

yes, make sense. both PRs under issue#151 or I create another issue specifically for RLIMIT_MEMLOCK and a PR for that ?

@Nehliin

Nehliin commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Feel free to create a new issue!

Check available descriptor slots at startup, allowing for the new ring, configured peer connections and 32 spare slots. Use a safe Rust snapshot of /proc/thread-self/fd, including sparse tables and descriptors above a lowered soft limit. Compute recovery limits against the occupied descriptor numbers and respect the hard limit.

Keep limit reads in a checked libc helper. Preserve inspection errors, distinguish process EMFILE from system-wide ENFILE, and provide recovery guidance even when no descriptor remains for inspection. The snapshot does not reserve future capacity.

RLIMIT_NOFILE protects sockets and files independently of the io_uring memory discussed in Nehliin#149. A limit of 64 cannot support the default 128 peers; this was reproduced as repeated Too many open files errors during peer discovery.

Wait for the torrent worker startup result before starting DHT/UI and surface errors without panicking. Cover low and exhausted limits, sparse tables, higher inherited descriptors, hard/unlimited limits, syscall errors, concurrent descriptor changes, and CLI startup propagation.

Use an invalid submission queue for the ring-setup regression so it does not depend on kernel MEMLOCK accounting. Queue a stop command to avoid hanging if setup unexpectedly succeeds, and assert that the original EINVAL reaches the caller.

Implementation and tests were AI-assisted. Refs Nehliin#151.
@4m1z

4m1z commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Hi @Nehliin, I’ve moved the RLIMIT_MEMLOCK work into #176, tracked by #175. #172 now focuses on RLIMIT_NOFILE for #151, so the two checks can be reviewed separately.

@Nehliin Nehliin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I think we can do some simplifications but I think the most important thing is that this doesn't become a fatal error. As mentioned in another comment I think we want to let this check create 1 new error variant and then WARN in the library if this is hit.

Ideally I think we have a warning "pop up" in the cli ui as well but not let this become a fatal error. You could also add an option to make it a hard error in the config but have it as a warning by default (that's up to you). However both the UI pop up and the config change can be done in a follow up pr instead if you want to limit the scope of this pr.

Ok(required)
}

fn check_descriptor_budget(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so I think this takes into account more edge cases than we realistically need.

  1. We do not need u128 we won't overflow u64 in any real scenario rlim_t is u64. Use saturating add instead and don't worry about the overflow.
  2. The minimum_descriptor_limit catches an edge case I wasn't even aware of but since this is ran on startup I don't think we need the extra complexity of sort,dedup etc. It's fine to overestimate the number of open fds even if they won't count towards the soft limit.

Ok(required)
}

fn check_descriptor_budget(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so I think this takes into account more edge cases than we realistically need.

  1. We do not need u128 we won't overflow u64 in any real scenario rlim_t is u64. Use saturating add instead and don't worry about the overflow.
  2. The minimum_descriptor_limit catches an edge case I wasn't even aware of but since this is ran on startup I don't think we need the extra complexity of sort,dedup etc. It's fine to overestimate the number of open fds even if they won't count towards the soft limit.

Comment thread bittorrent/src/torrent.rs
)]
SystemFileDescriptorsExhausted(#[source] io::Error),
#[error("Could not read {resource}: {source}")]
LimitInspection {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this level of granularity in the errors, I'd prefer if we add a single new error variant for this feature (see my other comment)

Comment thread bittorrent/src/torrent.rs
"Too many open files for this process: {source}\n\
RLIMIT_NOFILE: soft {soft_limit}; hard {hard_limit}.\n{guidance}"
)]
FileDescriptorsExhausted {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep this but rename it to FileDescriptorHeadRoomExhausted or something simillar

}

#[test]
fn limit_read_failure_preserves_errno() {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be remvoed

@@ -1,69 +1,40 @@
//! `Torrent::start` must report a failed io_uring setup to the caller rather

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this test if you add another test?

if limit.rlim_cur == libc::RLIM_INFINITY {
return Ok(());
}
let entries = fs::read_dir("/proc/thread-self/fd")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this fails due to chroot or SELinux or what not we should not crash the app

Comment thread cli/src/main.rs
result: &Receiver<color_eyre::Result<()>>,
) -> color_eyre::Result<()> {
loop {
if matches!(events.peek(), Some(TorrentEvent::Running { .. })) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why peek instead of dequeue?

Comment thread cli/src/main.rs
}

#[test]
fn torrent_startup_result_reaches_the_cli() {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading this PR and think about the other MEMLOCK apporoach I think we want to switch the not-enough headroom from a hard error to a WARN instead in the log. OR we add config option like you did in the other pr to make it a fatal error but make this warn by default. We also don't take actual files needed into account here since we don't know how many files the resulting torrent will contain. So we will never be 100% sure the estimate is enough. It's at most a guidance for misconfiguration.

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

Successfully merging this pull request may close these issues.

Check if the system ulimit is reasonable in the torrent start method

2 participants