Skip to content

Conversation

@mattsu2020
Copy link
Contributor

Adjust fd_soft_limit helper to expose the soft RLIMIT for reuse across the codebase.
Respect the soft limit when determining merge batch sizes so the external merge never exceeds the available file descriptors.
Fallback to a useful error message when retrieving the limit fails while parsing --batch-size.
Tests:

cargo build -p uu_sort
ulimit -n 7; ./target/debug/sort -n -m __test.* … (GNU sort-continue scenario)
ulimit -n 7; ./target/debug/sort -n -m __test.* - …

…mits

- Add `effective_merge_batch_size()` function to calculate batch size considering fd soft limit, with minimums and safety margins.
- Generalize fd limit handling from Linux-only to Unix systems using `fd_soft_limit()`.
- Update merge logic to use dynamic batch size instead of fixed `settings.merge_batch_size` to prevent fd exhaustion.
…dling

Replace direct call to get_rlimit()? with fd_soft_limit(), adding a check for None value to return a usage error if rlimit cannot be fetched. This improves robustness on Linux by ensuring proper error handling when retrieving the file descriptor soft limit.
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 1, 2025

CodSpeed Performance Report

Merging #9107 will not alter performance

Comparing mattsu2020:sort_-compatibility (10aea61) with main (f72130e)

Summary

✅ 127 untouched
⏩ 6 skipped1

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@github-actions
Copy link

github-actions bot commented Nov 1, 2025

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/overlay-headers (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/sort/sort-continue is no longer failing!

@mattsu2020 mattsu2020 changed the title Fix: sort-continue.sh test Fix: GNU sort-continue.sh test Nov 3, 2025
@mattsu2020 mattsu2020 changed the title Fix: GNU sort-continue.sh test fix(sort): GNU sort-continue.sh test Nov 3, 2025
@anastygnome
Copy link
Contributor

@mattsu2020 hey, thanks for the PR. to fix the docs related issues, you'll need to escape the brackets as in the error log. The rest of the CI fails are unrelated.

@github-actions
Copy link

github-actions bot commented Nov 3, 2025

GNU testsuite comparison:

Congrats! The gnu test tests/sort/sort-continue is no longer failing!

@mattsu2020
Copy link
Contributor Author

fix the docs related issues, you'll need to escape the brackets as in the error log.

This has been improved in another commit.

@anastygnome
Copy link
Contributor

@sylvestre can you restart the CI, this seems ready to merge :)

@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/sort/sort-continue is no longer failing!

@anastygnome
Copy link
Contributor

@mattsu2020 RLIM_INFINITY doesn't seem to be available there.

Update conditional compilation attributes from #[cfg(unix)] to #[cfg(target_os = "linux")]
for the nix::libc import and fd_soft_limit function implementations, ensuring these
features are only enabled on Linux systems to improve portability and avoid issues
on other Unix-like platforms.
@mattsu2020
Copy link
Contributor Author

@mattsu2020 RLIM_INFINITY doesn't seem to be available there.

Modified to restrict it to Linux

@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/sort/sort-continue is no longer failing!

Ok(())
}

fn effective_merge_batch_size(settings: &GlobalSettings) -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

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

it needs more comments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

0 => Ok(limit.rlim_cur as usize),
_ => Err(UUsageError::new(2, translate!("sort-failed-fetch-rlimit"))),

let result = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &raw mut limit) };
Copy link
Contributor

Choose a reason for hiding this comment

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

please use use nix::sys::resource::getrlimit;
to avoid the unsafe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it

mattsu2020 and others added 4 commits December 20, 2025 10:18
Replace unsafe libc::getrlimit calls in fd_soft_limit with safe nix crate usage.
Update Rayon thread configuration to use ThreadPoolBuilder instead of environment variables for better control.
Add documentation comment to effective_merge_batch_size function for clarity.
Extract the rlimit fetching logic into a separate `get_rlimit` function that returns `UResult<usize>` and properly handles errors with `UUsageError`, instead of silently returning `None` on failure or infinity. This provides better error reporting for resource limit issues on Linux platforms.
Reordered the nix::sys::resource imports to group constants first (RLIM_INFINITY), then types (Resource), and finally functions (getrlimit), improving code readability and adhering to import style guidelines.
@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/sort/sort-continue is no longer failing!

@sylvestre sylvestre merged commit ccd4bbd into uutils:main Dec 20, 2025
127 checks passed
@mattsu2020 mattsu2020 deleted the sort_-compatibility branch December 20, 2025 10:20
oech3 pushed a commit to oech3/coreutils that referenced this pull request Dec 20, 2025
* feat: dynamically adjust merge batch size based on file descriptor limits

- Add `effective_merge_batch_size()` function to calculate batch size considering fd soft limit, with minimums and safety margins.
- Generalize fd limit handling from Linux-only to Unix systems using `fd_soft_limit()`.
- Update merge logic to use dynamic batch size instead of fixed `settings.merge_batch_size` to prevent fd exhaustion.

* fix(sort): update rlimit fetching to use fd_soft_limit with error handling

Replace direct call to get_rlimit()? with fd_soft_limit(), adding a check for None value to return a usage error if rlimit cannot be fetched. This improves robustness on Linux by ensuring proper error handling when retrieving the file descriptor soft limit.

* refactor(sort): restrict nix::libc and fd_soft_limit to Linux

Update conditional compilation attributes from #[cfg(unix)] to #[cfg(target_os = "linux")]
for the nix::libc import and fd_soft_limit function implementations, ensuring these
features are only enabled on Linux systems to improve portability and avoid issues
on other Unix-like platforms.

* refactor: improve thread management and replace unsafe libc calls

Replace unsafe libc::getrlimit calls in fd_soft_limit with safe nix crate usage.
Update Rayon thread configuration to use ThreadPoolBuilder instead of environment variables for better control.
Add documentation comment to effective_merge_batch_size function for clarity.

* refactor(linux): improve error handling in fd_soft_limit function

Extract the rlimit fetching logic into a separate `get_rlimit` function that returns `UResult<usize>` and properly handles errors with `UUsageError`, instead of silently returning `None` on failure or infinity. This provides better error reporting for resource limit issues on Linux platforms.

* refactor(sort): reorder imports in get_rlimit for consistency

Reordered the nix::sys::resource imports to group constants first (RLIM_INFINITY), then types (Resource), and finally functions (getrlimit), improving code readability and adhering to import style guidelines.
CrazyRoka pushed a commit to CrazyRoka/coreutils that referenced this pull request Dec 28, 2025
* feat: dynamically adjust merge batch size based on file descriptor limits

- Add `effective_merge_batch_size()` function to calculate batch size considering fd soft limit, with minimums and safety margins.
- Generalize fd limit handling from Linux-only to Unix systems using `fd_soft_limit()`.
- Update merge logic to use dynamic batch size instead of fixed `settings.merge_batch_size` to prevent fd exhaustion.

* fix(sort): update rlimit fetching to use fd_soft_limit with error handling

Replace direct call to get_rlimit()? with fd_soft_limit(), adding a check for None value to return a usage error if rlimit cannot be fetched. This improves robustness on Linux by ensuring proper error handling when retrieving the file descriptor soft limit.

* refactor(sort): restrict nix::libc and fd_soft_limit to Linux

Update conditional compilation attributes from #[cfg(unix)] to #[cfg(target_os = "linux")]
for the nix::libc import and fd_soft_limit function implementations, ensuring these
features are only enabled on Linux systems to improve portability and avoid issues
on other Unix-like platforms.

* refactor: improve thread management and replace unsafe libc calls

Replace unsafe libc::getrlimit calls in fd_soft_limit with safe nix crate usage.
Update Rayon thread configuration to use ThreadPoolBuilder instead of environment variables for better control.
Add documentation comment to effective_merge_batch_size function for clarity.

* refactor(linux): improve error handling in fd_soft_limit function

Extract the rlimit fetching logic into a separate `get_rlimit` function that returns `UResult<usize>` and properly handles errors with `UUsageError`, instead of silently returning `None` on failure or infinity. This provides better error reporting for resource limit issues on Linux platforms.

* refactor(sort): reorder imports in get_rlimit for consistency

Reordered the nix::sys::resource imports to group constants first (RLIM_INFINITY), then types (Resource), and finally functions (getrlimit), improving code readability and adhering to import style guidelines.
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.

3 participants