Skip to content

sort: keep sorting when no more threads can be created - #16415

Open
aozmekik wants to merge 3 commits into
uutils:mainfrom
aozmekik:sort-thread-spawn-fallback
Open

aozmekik wants to merge 3 commits into
uutils:mainfrom
aozmekik:sort-thread-spawn-fallback

Conversation

@aozmekik

Copy link
Copy Markdown

Ubuntu's openvpn-client@.service sets TasksMax=20, and update-resolv-conf pipes through sort, so on a 16-CPU machine sort panics with failed to spawn thread and openvpn fails to bring the connection up (LP: #2172214). GNU sort keeps working with fewer threads.

Two places needed a fallback:

  • the sorter thread in ext_sort: if it can't be spawned, the chunks go back to the reading thread and are sorted there.
  • rayon's global pool: if build_global fails, rayon can't create one later either, so chunks are sorted without it instead of panicking on the first one large enough for par_sort to use the pool.

I tested it on a 2-CPU runner with systemd-run --scope -p TasksMax=N. Main panics at TasksMax 1-3 for printf 'b\na\n' | sort, and at 2-3 for 300k shuffled numbers with -n. With this change both work at every limit and give the same output as GNU. The sort tests pass, and clippy and the wasi check are clean.

Not covered here: when the input spills to temporary files (or with -m/-c), the merge and check paths start their own reader thread and still panic under the same limits (-S 1M with TasksMax 2-3). That needs a bigger change to the merger, so I'd rather do it separately.

(#13806 adds a compile-time no-thread variant for WASI. This one is the runtime case, when the OS refuses a thread on Linux.)

@sylvestre

Copy link
Copy Markdown
Contributor

nice finding!
is there a way to add a test for this? thanks

@aozmekik

Copy link
Copy Markdown
Author

Added test_no_threads_available in f22d3bc. It runs sort with RLIMIT_NPROC set to 1, which stops an unprivileged process from creating any thread (the same effect as a cgroup pids limit), on a two-line input and on 100k lines so the parallel sort path is hit too. It fails on main and passes here. Root isn't limited by RLIMIT_NPROC, so the test is skipped there.

@codspeed

codspeed Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 29 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

✅ 58 untouched benchmarks
⏩ 426 skipped benchmarks1


Comparing aozmekik:sort-thread-spawn-fallback (354e856) with main (e1b90fa)2

Open in CodSpeed

Footnotes

  1. 426 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. ↩

  2. No successful run was found on main (a663b44) during the generation of this report, so e1b90fa was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@aozmekik

Copy link
Copy Markdown
Author

The CodSpeed regression was from my change: build_global() also fails when the pool is already initialized, which happens on every run after the first in the benchmarks, and I treated that as "no pool", so those runs sorted sequentially. ab83b45 only treats rayon's I/O error as "no threads" and decides once per process. With the divan benches on a GitHub runner, sort_ascii_only is 44.2 ms on main, 46.9 ms before this commit and 44.5 ms after; sort_mixed_data is 40.0 / 46.2 / 39.9 ms.

The same commit fixes the wasi lint (the field was unused there). The stty clippy and OpenBSD failures happen on main too.

@sylvestre

Copy link
Copy Markdown
Contributor

The CodSpeed regression was from my change:...

I am more interested in the fix :)

Under a cgroup pids limit (for example TasksMax in a systemd unit), the
sorter thread could not be spawned and sort panicked with "failed to
spawn thread".  Sort the chunks on the reading thread in that case.

If rayon's global pool cannot be created either, rayon never gets one,
so sort chunks without it instead of panicking on the first large one.
RLIMIT_NPROC set to 1 stops an unprivileged process from creating any
thread, like a cgroup pids limit.  Check a small input and one large
enough for the parallel sort.
build_global() also fails when the pool is already initialized, which
happens whenever sort runs again in the same process, as in the
benchmarks.  That was taken to mean there is no pool, so those runs
sorted sequentially.  Only an I/O error means no thread could be
created; decide once per process.

Also only define the parallel field where it is read, so the wasi
builds do not warn about a dead field.
@aozmekik

Copy link
Copy Markdown
Author

Rebased on main so the lint job picks up the stty clippy fix.

@aozmekik
aozmekik force-pushed the sort-thread-spawn-fallback branch from ab83b45 to 354e856 Compare November 19, 2026 12:11
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

This branch has not been deployed

No deployments
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.

2 participants