Repository navigation
timeout: do not escalate to KILL when non-TERM signal ends child - #16716
yunusarda-sudo wants to merge 2 commits into
Conversation
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
| } | ||
| // GNU timeout also kills on signals other than SIGTERM. | ||
| Ok(TimeoutRet::TimedOut | TimeoutRet::Interrupted(_)) => { | ||
| if let Some(status) = process.try_wait()? { |
There was a problem hiding this comment.
is this still needed with the ignore_signal change? it duplicates the Exited arm just above
There was a problem hiding this comment.
No, removed in ab7c168. With the sent signal ignored, the grace-period wait returns Exited on its own.
| /// | ||
| /// If there is a problem sending the `SIGKILL` signal or waiting for | ||
| /// the process after that signal is sent. | ||
| #[allow(clippy::too_many_arguments)] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()? { |
There was a problem hiding this comment.
why do we need this fast path? SIGCHLD already handles it
| match timer.timed_sigwait(remaining) { | ||
| Ok(None) => break Ok(TimeoutRet::TimedOut), | ||
| Ok(None) => { | ||
| if let Some(status) = self.try_wait()? { |
There was a problem hiding this comment.
same here, do we need this try_wait?
| } // otherwise waits again | ||
| } | ||
| Ok(Some(Signal::SIGTERM)) if ignore_term => {} // waits again | ||
| Ok(Some(signal)) if ignore_signal == Some(signal as usize) => {} // waits again |
There was a problem hiding this comment.
with -s INT, an external TERM during the grace period now escalates to KILL, before it was ignored. is that intended?
There was a problem hiding this comment.
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.
|
GNU testsuite comparison: |
Merging this PR will regress 3 benchmarks3 warnings, 1 note
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
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.
When
-k(kill-after grace period) is used with a non-TERM timeout signal (such as-s INT,-s HUP, or-s QUIT),timeoutsends that signal to the child's process group. Becausetimeoutis also in that process group, it receives the signal itself as a pending signal.During the kill-after grace period,
wait_or_kill_processinvokeswait_or_timeout. Previously,wait_or_timeoutonly ignoredSIGTERM(ignore_term: bool). As a result, receiving any non-TERM signal immediately causedwait_or_timeoutto returnTimeoutRet::Interrupted, whichwait_or_kill_processtreated as an immediate prompt to sendSIGKILL, overriding the child's normal termination and reporting command timed out / killed.This PR fixes this by:
ChildExt::wait_or_timeoutto acceptignore_signal: Option<usize>, ignoring the specific signal that was broadcast to the process group during the grace period.process.try_wait()?before escalating toSIGKILLinwait_or_kill_process.tests/by-util/test_timeout.rsverifying that non-TERM signals (INT, HUP, QUIT) with-kdo not escalate to KILL when the child exits cleanly.Fixes #16642