Skip to content

Commit bbd1ede

Browse files
committedMay 20, 2024
scubainit: Restore the default SIGPIPE action
Rust pre-main code may change the SIGPIPE disposition to ignore: * rust-lang/rust#62569 * rust-lang/rust#97889 We could use the nightly compiler flag -Zon-broken-pipe=inherit to disable this behavior. Instead, we take the simpler route and restore the default disposition ourselves. Fixes #254
1 parent 325fb58 commit bbd1ede

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎scubainit/src/main.rs

+19
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ fn run_scubainit() -> Result<()> {
4545
setup_logging()?;
4646
info!("Looking Rusty!");
4747

48+
restore_sigpipe_default()?;
49+
4850
let ctx = process_envvars()?;
4951

5052
if let Some(ref user_info) = ctx.user_info {
@@ -368,3 +370,20 @@ fn setup_logging() -> Result<()> {
368370
.verbosity(verbosity)
369371
.init()?)
370372
}
373+
374+
// Restore the default SIGPIPE action.
375+
// Rust pre-main code may change the SIGPIPE disposition to ignore:
376+
// https://github.com/rust-lang/rust/issues/62569
377+
// https://github.com/rust-lang/rust/issues/97889
378+
fn restore_sigpipe_default() -> Result<()> {
379+
// Set the SIGPIPE disposition to default (terminate).
380+
// SAFETY: No signal handler is provided, only the default action,
381+
// so no additional consideration is needed.
382+
unsafe {
383+
let result = libc::signal(libc::SIGPIPE, libc::SIG_DFL);
384+
if result == libc::SIG_ERR {
385+
return Err(std::io::Error::last_os_error().into())
386+
}
387+
}
388+
Ok(())
389+
}

‎tests/test_main.py

+11
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ def test_redirect_stdin(self) -> None:
278278
assert_str_equalish(out, test_str)
279279

280280

281+
class TestMainSignals(MainTest):
282+
def test_sigpipe(self) -> None:
283+
"""Verify SIGPIPE is handled correctly"""
284+
# See https://github.com/JonathonReinhart/scuba/issues/254
285+
SCUBA_YML.write_text(f"image: {DOCKER_IMAGE}")
286+
287+
out, err = run_scuba(["sh", "-c", "yes | echo abcd"])
288+
assert_str_equalish(out, "abcd")
289+
assert_str_equalish(err, "")
290+
291+
281292
class TestMainUser(MainTest):
282293
def _test_user(
283294
self,

0 commit comments

Comments
 (0)