Skip to content

timeout: do not escalate to KILL when non-TERM signal ends child - #16716

Open
yunusarda-sudo wants to merge 3 commits into
uutils:mainfrom
yunusarda-sudo:fix/timeout-kill-after-signal
Open

yunusarda-sudo wants to merge 3 commits into
uutils:mainfrom
yunusarda-sudo:fix/timeout-kill-after-signal

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

When -k (kill-after grace period) is used with a non-TERM timeout signal (such as -s INT, -s HUP, or -s QUIT), timeout sends that signal to the child's process group. Because timeout is also in that process group, it receives the signal itself as a pending signal.

During the kill-after grace period, wait_or_kill_process invokes wait_or_timeout. Previously, wait_or_timeout only ignored SIGTERM (ignore_term: bool). As a result, receiving any non-TERM signal immediately caused wait_or_timeout to return TimeoutRet::Interrupted, which wait_or_kill_process treated as an immediate prompt to send SIGKILL, overriding the child's normal termination and reporting command timed out / killed.

This PR fixes this by:

  1. Updating ChildExt::wait_or_timeout to accept ignore_signal: Option<usize>, ignoring the specific signal that was broadcast to the process group during the grace period.
  2. Checking process.try_wait()? before escalating to SIGKILL in wait_or_kill_process.
  3. Adding a regression test in tests/by-util/test_timeout.rs verifying that non-TERM signals (INT, HUP, QUIT) with -k do not escalate to KILL when the child exits cleanly.

Fixes #16642

Comment thread src/uu/timeout/src/timeout.rs Outdated
}
// GNU timeout also kills on signals other than SIGTERM.
Ok(TimeoutRet::TimedOut | TimeoutRet::Interrupted(_)) => {
if let Some(status) = process.try_wait()? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this still needed with the ignore_signal change? it duplicates the Exited arm just above

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.

No, removed in ab7c168. With the sent signal ignored, the grace-period wait returns Exited on its own.

Comment thread src/uu/timeout/src/timeout.rs Outdated
///
/// If there is a problem sending the `SIGKILL` signal or waiting for
/// the process after that signal is sent.
#[allow(clippy::too_many_arguments)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please avoid the #[allow]. ignore_signal is always Some here, so it could just be the signal we sent, or the args could be grouped

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.

Removed in ab7c168. The delay and the sent signal are now grouped in a small KillAfter struct, so the function is back to 7 arguments.

drop(self.stdin.take());

// Fast path: if the child already terminated, return its exit status immediately.
if let Some(status) = self.try_wait()? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why do we need this fast path? SIGCHLD already handles it

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.

Not needed, removed in ab7c168.

match timer.timed_sigwait(remaining) {
Ok(None) => break Ok(TimeoutRet::TimedOut),
Ok(None) => {
if let Some(status) = self.try_wait()? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same here, do we need this try_wait?

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.

Removed too.

} // otherwise waits again
}
Ok(Some(Signal::SIGTERM)) if ignore_term => {} // waits again
Ok(Some(signal)) if ignore_signal == Some(signal as usize) => {} // waits again

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

with -s INT, an external TERM during the grace period now escalates to KILL, before it was ignored. is that intended?

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.

Not intended, thanks for catching it. In ab7c168, wait_or_timeout takes &[usize] of signals to ignore, and the grace period passes both TERM and the sent signal, so an external TERM is ignored as before. test_kill_after_ignores_external_term_after_non_term_signal covers it: it fails on the previous commit and passes now. I couldn't build the Windows side locally; that change is only the signature.

@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!

@codspeed

codspeed Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 123 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

✅ 415 untouched benchmarks
⏩ 70 skipped benchmarks1


Comparing yunusarda-sudo:fix/timeout-kill-after-signal (3ea2c2f) with main (9ed1dfc)

Open in CodSpeed

Footnotes

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

@xtqqczze

Copy link
Copy Markdown
Collaborator

needs to be rebased for codspeed

@yunusarda-sudo
yunusarda-sudo force-pushed the fix/timeout-kill-after-signal branch from ab7c168 to 478f582 Compare November 22, 2026 13:53
@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Rebased onto current main so CodSpeed can run against the latest baseline.

yunusarda-sudo and others added 2 commits November 23, 2026 16:27
When -k is specified, timeout enters wait_or_kill_process to wait through
the grace period. Because timeout broadcasts the timeout signal to the process
group, timeout receives the signal itself. Previously, wait_or_timeout only
ignored SIGTERM, causing other signals (such as SIGINT, SIGHUP, SIGQUIT) to
immediately return TimeoutRet::Interrupted and trigger an immediate SIGKILL
even if the child process was already terminated.

Fix this by passing the specific signal to wait_or_timeout to ignore, and
verifying whether the child has already exited before sending SIGKILL.

Fixes uutils#16642
Address review: wait_or_timeout now takes the signals to ignore, and the
grace period ignores both TERM and the signal that was sent, so an
external TERM no longer escalates to KILL with -s INT. Drop the extra
try_wait calls, which SIGCHLD already covers, and group the kill-after
delay and sent signal to avoid the clippy allow.
@sylvestre
sylvestre force-pushed the fix/timeout-kill-after-signal branch from 478f582 to cd2a79e Compare November 23, 2026 15:27

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.

timeout: -k sends KILL even though the first signal already ended the command

3 participants