Skip to content

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

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

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

Conversation

@yunusarda-sudo

Copy link
Copy Markdown

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

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
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
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
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
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
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
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 regress 3 benchmarks

3 warnings, 1 note

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

⚠️ 5 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

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

⚡ 2 improved benchmarks
❌ 3 regressed benchmarks
✅ 408 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation cksum_crc 33.1 ms 34.5 ms -3.95%
❌ CPU Simulation cksum_raw_output 33.3 ms 34.7 ms -3.92%
❌ CPU Simulation cksum_multiple_files 49.5 ms 51.5 ms -3.87%
⚡ Memory cp_recursive_deep_tree[(120, 4)] 699.2 KB 581.2 KB +20.31%
⚡ CPU Simulation df_deep_directory 930.1 µs 892.1 µs +4.27%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing yunusarda-sudo:fix/timeout-kill-after-signal (ab7c168) with main (2775292)

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

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.

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

2 participants