Skip to content

Make ending commands and waiting callers sleep instead of spinning - #193

Merged
holzschu merged 3 commits into
holzschu:masterfrom
demrom:fix/cleanup-wait
Nov 1, 2026
Merged

holzschu merged 3 commits into
holzschu:masterfrom
demrom:fix/cleanup-wait

Conversation

@demrom

@demrom demrom commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Three waits spun on a variable and kept the CPU busy, competing with the commands that would end them:

  • ios_nextAvailablePid, chdir, fchdir and fchdir_nolock looped on cleanup_counter > 0 until no command was ending.
  • cleanup_function looped on pthread_mutex_trylock(&pid_mtx) while a caller was creating a pid.
  • A command ending in the middle of a pipeline looped on current_command_root_thread, calling fflush each time, until the first command of the pipeline had ended.

The counter now changes under a mutex and waiters sleep on a condition variable (ios_cleanupBegin, ios_cleanupEnd, ios_cleanupWait in libc_replacement.c). The trylock loop becomes a lock and unlock. The pipeline wait sleeps on a condition signalled when current_command_root_thread is cleared, and wakes every 50 ms to check whether the command was cancelled, since cancellation has no condition to signal. ios_cleanupWait disables cancellation while it waits: pthread_cond_wait is a cancellation point, and a command cancelled there (Ctrl-C in a pipeline, ios_kill) would wake holding cleanup_mutex and block on it again in cleanup_function, so every later chdir, ios_fork and command end would hang. The cancel arrives at the next cancellation point instead. cleanup_counter is now static, since nothing outside libc_replacement.c uses it any more.

Test

tests/cleanup-wait.m runs hold | mid | skip with joinMainThread = false. mid ends after 300 ms while hold (2 s) is still the root of the pipeline, and the test measures the process CPU time over 3 seconds. On the iOS 27 Simulator, master (fa2af59) fails 4 of 4 runs with FAIL, cpu=1.68s in 3 s, the ending command spun (1.68 s to 1.71 s); with this change 10 of 10 runs print cpu=0.01s in 3 s PASS.

tests/cleanup-cancel.m pretends a command is ending (ios_cleanupBegin), cancels a command blocked in chdir, ends the pretend command and runs one more command. Without the cancellation guard it prints FAIL, no end after 20 s and exits 124 (5 of 5 runs); with it, 10 of 10 runs print PASS.

Merged with the other four changes of the series, concurrent-fork, concurrent-getopt, getopt-legacy and pid-admission pass 5 of 5 runs each. On an iPhone, in a-Shell mini 2.2.3 (592) built from a-shell master with that merged ios_system, sleep 2 | sleep 1 | true from dash returned in 2 of 2 runs, after 3 s by date +%s (whole seconds).

This applies to master on its own. Once #192 and the changes stacked on it land, I rerun the merged tests on master.

Callers that start a command or change the directory looped on cleanup_counter, a command that ends looped on pthread_mutex_trylock(&pid_mtx), and a command that ends in the middle of a pipeline looped on current_command_root_thread with an fflush each time.

The counter now changes under a mutex and the callers sleep on a condition variable. The trylock loop is a pthread_mutex_lock. The pipeline wait sleeps on a condition that a command signals when it clears current_command_root_thread, and wakes every 50 ms to see whether the command was cancelled.

tests/cleanup-wait.m measures the CPU of a pipeline whose middle command waits for the first one.

ios_cleanupWait disables cancellation while it waits, so a command cancelled there does not keep cleanup_mutex. tests/cleanup-cancel.m cancels a thread blocked in chdir while a command ends.
@demrom
demrom marked this pull request as ready for review October 24, 2026 04:22
@holzschu

holzschu commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for that one as well.

@holzschu
holzschu merged commit 9f2b5a8 into holzschu:master Nov 1, 2026
2 checks passed
@demrom
demrom deleted the fix/cleanup-wait branch November 2, 2026 08:49
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