Skip to content

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

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

yunusarda-sudo wants to merge 1 commit 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
}
// 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

///
/// 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

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

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?

} // 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?

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