Skip to content

Commit 323ccf1

Browse files
dist: report remote compile exit codes as exit codes, not signals
`From<ProcessOutput> for process::Output` had the same problem as the nvcc path: `ProcessOutput::code` is a real exit code (`status.code()` on the build server), but the client turned it back with `ExitStatus::from_raw`, which takes a wait status on Unix, so a remote compile failing with exit code 1 showed up as "killed by signal 1". The conversion now lives in one place, `util::exit_status_from_code`, used by both `dist` and `nvcc`, with tests for the helper and for the `ProcessOutput` round trip. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VszvDmRRPP1UcwozsUMY1L
1 parent 17503dd commit 323ccf1

4 files changed

Lines changed: 92 additions & 35 deletions

File tree

‎src/compiler/nvcc.rs‎

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use crate::compiler::{
2525
use crate::mock_command::{
2626
CommandChild, CommandCreator, CommandCreatorSync, ExitStatusValue, RunCommand, exit_status,
2727
};
28-
use crate::util::{OsStrExt, resolve_compiler_avoiding_wrapper, run_input_output};
28+
use crate::util::{
29+
OsStrExt, exit_status_from_code, resolve_compiler_avoiding_wrapper, run_input_output,
30+
};
2931
use crate::{counted_array, dist, protocol, server};
3032
use async_trait::async_trait;
3133
use fs::File;
@@ -1355,23 +1357,11 @@ fn aggregate_output(lhs: process::Output, rhs: process::Output) -> process::Outp
13551357
}
13561358
}
13571359

1358-
// `ExitStatus::from_raw` takes a wait status on Unix, not an exit code: passed directly, the
1359-
// server's fatal-error code -2 reads as "killed by signal 126" and an exit code of 1 as signal 1.
1360-
#[cfg(unix)]
1361-
fn exit_code_status(code: i32) -> process::ExitStatus {
1362-
exit_status((code & 0xff) << 8)
1363-
}
1364-
1365-
#[cfg(windows)]
1366-
fn exit_code_status(code: i32) -> process::ExitStatus {
1367-
exit_status(code as ExitStatusValue)
1368-
}
1369-
13701360
fn error_to_output(err: Error) -> process::Output {
13711361
match err.downcast::<ProcessError>() {
13721362
Ok(ProcessError(out)) => out,
13731363
Err(err) => process::Output {
1374-
status: exit_code_status(1),
1364+
status: exit_status_from_code(1),
13751365
stdout: vec![],
13761366
stderr: err.to_string().into_bytes(),
13771367
},
@@ -1396,7 +1386,7 @@ fn compile_result_to_output(exe: &Path, res: protocol::CompileFinished) -> proce
13961386
};
13971387
}
13981388
process::Output {
1399-
status: exit_code_status(res.retcode.unwrap_or(0)),
1389+
status: exit_status_from_code(res.retcode.unwrap_or(0)),
14001390
stdout: res.stdout,
14011391
stderr: res.stderr,
14021392
}
@@ -2389,19 +2379,22 @@ mod test {
23892379

23902380
#[test]
23912381
fn test_aggregate_output_keeps_first_failure() {
2392-
let ok = exit_code_status(0);
2382+
let ok = exit_status_from_code(0);
23932383

23942384
let all_ok = aggregate_output(output_with(ok, "a"), output_with(ok, "b"));
23952385
assert!(all_ok.status.success());
23962386
assert_eq!(all_ok.stdout, b"ab");
23972387
assert_eq!(all_ok.stderr, b"ab");
23982388

2399-
let failed = aggregate_output(output_with(ok, "a"), output_with(exit_code_status(2), "b"));
2389+
let failed = aggregate_output(
2390+
output_with(ok, "a"),
2391+
output_with(exit_status_from_code(2), "b"),
2392+
);
24002393
assert_eq!(failed.status.code(), Some(2));
24012394

24022395
let first = aggregate_output(
2403-
output_with(exit_code_status(3), "a"),
2404-
output_with(exit_code_status(5), "b"),
2396+
output_with(exit_status_from_code(3), "a"),
2397+
output_with(exit_status_from_code(5), "b"),
24052398
);
24062399
assert_eq!(first.status.code(), Some(3));
24072400
assert_eq!(first.stdout, b"ab");
@@ -2411,7 +2404,7 @@ mod test {
24112404
#[test]
24122405
fn test_aggregate_output_keeps_signal() {
24132406
let killed = aggregate_output(
2414-
output_with(exit_code_status(0), "a"),
2407+
output_with(exit_status_from_code(0), "a"),
24152408
output_with(exit_status(9), "b"),
24162409
);
24172410
assert_eq!(killed.status.signal(), Some(9));

‎src/dist/mod.rs‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -471,23 +471,13 @@ impl ProcessOutput {
471471
}
472472
#[cfg(unix)]
473473
use std::os::unix::process::ExitStatusExt;
474-
#[cfg(windows)]
475-
use std::os::windows::process::ExitStatusExt;
476-
#[cfg(unix)]
477-
fn exit_status(code: i32) -> process::ExitStatus {
478-
process::ExitStatus::from_raw(code)
479-
}
480-
#[cfg(windows)]
481-
fn exit_status(code: i32) -> process::ExitStatus {
482-
// TODO: this is probably a subideal conversion - it's not clear how Unix exit codes map to
483-
// Windows exit codes (other than 0 being a success)
484-
process::ExitStatus::from_raw(code as u32)
485-
}
486474
impl From<ProcessOutput> for process::Output {
487475
fn from(o: ProcessOutput) -> Self {
488476
// TODO: handle signals, i.e. None code
477+
// TODO: on Windows a Unix exit code is taken as is; it is not clear how Unix exit codes
478+
// map to Windows exit codes (other than 0 being a success)
489479
process::Output {
490-
status: exit_status(o.code),
480+
status: crate::util::exit_status_from_code(o.code),
491481
stdout: o.stdout,
492482
stderr: o.stderr,
493483
}

‎src/dist/test.rs‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::dist::Toolchain;
1+
use crate::dist::{ProcessOutput, Toolchain};
22
use crate::util::Digest;
33

44
fn roundtrip(archive_id: &str) -> bincode::Result<Toolchain> {
@@ -33,3 +33,34 @@ fn test_toolchain_archive_id_non_digest_rejected() {
3333
assert!(roundtrip(archive_id).is_err(), "accepted {archive_id:?}");
3434
}
3535
}
36+
37+
#[test]
38+
fn test_process_output_exit_code_is_kept() {
39+
let output: std::process::Output =
40+
ProcessOutput::fake_output(1, b"out".to_vec(), b"err".to_vec()).into();
41+
assert_eq!(output.status.code(), Some(1));
42+
assert_eq!(output.stdout, b"out");
43+
assert_eq!(output.stderr, b"err");
44+
#[cfg(unix)]
45+
{
46+
use std::os::unix::process::ExitStatusExt;
47+
assert_eq!(output.status.signal(), None);
48+
}
49+
}
50+
51+
// A remote compile that fails with an exit code reached the client as "killed by signal <code>".
52+
#[cfg(unix)]
53+
#[test]
54+
fn test_process_output_roundtrip_is_not_a_signal() {
55+
use std::os::unix::process::ExitStatusExt;
56+
for code in [0, 1, 2, 127] {
57+
let compiled = std::process::Output {
58+
status: crate::util::exit_status_from_code(code),
59+
stdout: vec![],
60+
stderr: vec![],
61+
};
62+
let received: std::process::Output = ProcessOutput::try_from(compiled).unwrap().into();
63+
assert_eq!(received.status.code(), Some(code), "exit code {code}");
64+
assert_eq!(received.status.signal(), None, "exit code {code}");
65+
}
66+
}

‎src/util.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,23 @@ where
594594
})
595595
}
596596

597+
/// The `ExitStatus` of a process that exited with `code`.
598+
///
599+
/// On Unix `ExitStatus::from_raw` takes a wait status, not an exit code: given an exit code
600+
/// directly, -2 reads as "killed by signal 126" and 1 as signal 1.
601+
#[cfg(unix)]
602+
pub fn exit_status_from_code(code: i32) -> process::ExitStatus {
603+
use std::os::unix::process::ExitStatusExt;
604+
process::ExitStatus::from_raw((code & 0xff) << 8)
605+
}
606+
607+
/// The `ExitStatus` of a process that exited with `code`.
608+
#[cfg(windows)]
609+
pub fn exit_status_from_code(code: i32) -> process::ExitStatus {
610+
use std::os::windows::process::ExitStatusExt;
611+
process::ExitStatus::from_raw(code as u32)
612+
}
613+
597614
/// Write `data` to `writer` with bincode serialization, prefixed by a `u32` length.
598615
pub fn write_length_prefixed_bincode<W, S>(mut writer: W, data: S) -> Result<()>
599616
where
@@ -1614,10 +1631,36 @@ pub fn resolve_compiler_avoiding_wrapper(
16141631

16151632
#[cfg(test)]
16161633
mod tests {
1617-
use super::{Digest, OsStrExt, TimeMacroFinder, resolve_compiler_avoiding_wrapper};
1634+
use super::{
1635+
Digest, OsStrExt, TimeMacroFinder, exit_status_from_code, resolve_compiler_avoiding_wrapper,
1636+
};
16181637
use std::ffi::{OsStr, OsString};
16191638
use std::path::Path;
16201639

1640+
#[test]
1641+
fn test_exit_status_from_code() {
1642+
assert!(exit_status_from_code(0).success());
1643+
assert_eq!(exit_status_from_code(1).code(), Some(1));
1644+
assert_eq!(exit_status_from_code(255).code(), Some(255));
1645+
}
1646+
1647+
#[cfg(unix)]
1648+
#[test]
1649+
fn test_exit_status_from_code_is_not_a_signal() {
1650+
use std::os::unix::process::ExitStatusExt;
1651+
for code in [1, 2, 126, 127, 255, -2] {
1652+
let status = exit_status_from_code(code);
1653+
assert_eq!(status.signal(), None, "exit code {code}");
1654+
assert_eq!(status.code(), Some(code & 0xff), "exit code {code}");
1655+
}
1656+
}
1657+
1658+
#[cfg(windows)]
1659+
#[test]
1660+
fn test_exit_status_from_code_keeps_the_code() {
1661+
assert_eq!(exit_status_from_code(-2).code(), Some(-2));
1662+
}
1663+
16211664
#[cfg(all(any_dist, target_os = "linux"))]
16221665
#[test]
16231666
#[serial_test::serial(reqwest_tls_env)]

0 commit comments

Comments
 (0)