Skip to content

Commit 62c2007

Browse files
authored
Merge pull request uutils#9435 from naoNao89/timeout-error-handling
timeout: Improve error handling with explicit expect messages
2 parents 7654075 + 22b63ee commit 62c2007

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/uu/timeout/src/timeout.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,14 @@ fn wait_or_kill_process(
270270
match process.wait_or_timeout(duration, None) {
271271
Ok(Some(status)) => {
272272
if preserve_status {
273-
Ok(status.code().unwrap_or_else(|| status.signal().unwrap()))
273+
let exit_code = status.code().unwrap_or_else(|| {
274+
status.signal().unwrap_or_else(|| {
275+
// Extremely rare: process exited but we have neither exit code nor signal.
276+
// This can happen on some platforms or in unusual termination scenarios.
277+
ExitStatus::TimeoutFailed.into()
278+
})
279+
});
280+
Ok(exit_code)
274281
} else {
275282
Ok(ExitStatus::TimeoutFailed.into())
276283
}
@@ -358,10 +365,14 @@ fn timeout(
358365
// structure of `wait_or_kill_process()`. They can probably be
359366
// refactored into some common function.
360367
match process.wait_or_timeout(duration, Some(&SIGNALED)) {
361-
Ok(Some(status)) => Err(status
362-
.code()
363-
.unwrap_or_else(|| preserve_signal_info(status.signal().unwrap()))
364-
.into()),
368+
Ok(Some(status)) => {
369+
let exit_code = status.code().unwrap_or_else(|| {
370+
status
371+
.signal()
372+
.map_or_else(|| ExitStatus::TimeoutFailed.into(), preserve_signal_info)
373+
});
374+
Err(exit_code.into())
375+
}
365376
Ok(None) => {
366377
report_if_verbose(signal, &cmd[0], verbose);
367378
send_signal(process, signal, foreground);

0 commit comments

Comments
 (0)