openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

fix(cli): restore SIGPIPE default on Unix

Open YonganZhang opened this issue 5 months ago β€’ 1 comments

Summary

  • restore the default SIGPIPE disposition at process startup on Unix
  • prevents Rust print macros from panicking on normal closed-pipe CLI usage like openhuman-core --help | head
  • add a Unix integration test that closes the stdout pipe and verifies stderr does not contain a broken-pipe panic

Closes #3535

Tests

  • cargo fmt --check
  • git diff --check

Attempted but blocked locally:

  • env CC=/usr/bin/gcc CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/gcc cargo test --test cli_sigpipe -- --nocapture
    • blocked by missing system pkg-config deps: alsa.pc and xi.pc

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a crash that occurred when the CLI's output pipe was closed before completion (e.g., when piping to commands like head).

YonganZhang avatar Jun 13 '26 22:06 YonganZhang

Review Change Stack

[!NOTE] Currently processing new changes in this PR. This may take a few minutes, please wait...

βš™οΈ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d185a8d4-5385-4884-8501-863963eb85fb

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 5a540d2dbe2c2dc7cc4a7c105267b1f0cfc255f4 and 1a1dbc8782df893f48bbea3016071db939f662f8.

πŸ“’ Files selected for processing (1)
  • tests/cli_sigpipe.rs
 _____________________________________________________________________________
< Rabbit check: I hopped through the diff and found carrots... and landmines. >
 -----------------------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
πŸ“ Walkthrough

Walkthrough

Adds libc = "0.2" as a direct dependency, introduces a restore_default_sigpipe function that calls libc::signal(SIGPIPE, SIG_DFL) on Unix (no-op on non-Unix), invokes it at the start of main, and adds an integration test verifying the CLI does not panic when its stdout pipe is closed early.

Changes

SIGPIPE default handler and integration test

Layer / File(s) Summary
libc dependency and restore_default_sigpipe implementation
Cargo.toml, src/main.rs
Pins libc = "0.2" as a direct dependency. Adds restore_default_sigpipe with a #[cfg(unix)] body calling libc::signal(SIGPIPE, SIG_DFL) and a #[cfg(not(unix))] no-op stub. Calls the function at the very start of main before dotenv loading and Sentry initialization.
Broken-pipe integration test
tests/cli_sigpipe.rs
Adds #[cfg(unix)] test help_output_closed_pipe_does_not_panic that spawns the CLI binary with --help, immediately drops the captured stdout handle, waits for the process, and asserts stderr contains neither "Broken pipe" nor "panicked".

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

πŸ‡ A pipe snapped shut, the stream ran dry, No panic now, the writes just die. SIG_DFL set before the start, A tiny fix, a steady heart. Broken pipes shall quietly fade β€” hooray! ✨

πŸš₯ Pre-merge checks | βœ… 5
βœ… Passed checks (5 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title clearly describes the main change: restoring the SIGPIPE default on Unix for the CLI.
Linked Issues check βœ… Passed The PR restores SIGPIPE on Unix and adds a regression test covering closed-pipe output, matching the issue goals.
Out of Scope Changes check βœ… Passed The changes are limited to the SIGPIPE fix, its libc dependency, and the regression test, with no unrelated additions.
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jun 13 '26 22:06 coderabbitai[bot]

I checked the failing Rust Core Coverage (cargo-llvm-cov) job. The tests completed and lcov-core.info was generated; the failure happened in post-job cleanup/rust-cache while running cargo metadata --all-features, which failed to unpack windows v0.57.0 because the runner hit No space left on device.

I tried gh run rerun 27481248697 --failed, but GitHub rejected it because I do not have admin rights. This looks like an infra/cache disk-space failure rather than a code failure; a maintainer rerun should be the next step.

Felyx-Fu avatar Jun 17 '26 11:06 Felyx-Fu

I addressed the remaining CodeRabbit test nit in a helper PR because direct push to the source branch returned 403: https://github.com/YonganZhang/openhuman/pull/4

What changed:

  • The closed-pipe CLI regression test now asserts that the child process either exits successfully or is terminated by SIGPIPE.
  • The existing stderr checks for broken-pipe/panic text are preserved.

Validation:

  • cargo fmt --manifest-path Cargo.toml --check
  • git diff --check -- tests/cli_sigpipe.rs
  • cargo test --manifest-path Cargo.toml --test cli_sigpipe was attempted on this Windows environment, but compilation did not finish before a 5 minute timeout. The test body is Unix-only via #[cfg(unix)], so Linux CI is the authoritative execution path.

The existing Rust Core Coverage failure still appears to be the runner disk/cache cleanup issue noted above rather than a code failure.

Felyx-Fu avatar Jun 17 '26 16:06 Felyx-Fu

Review: fix is correct, but the regression test is likely a false-green

The production fix is sound βœ… β€” restoring SIGPIPE β†’ SIG_DFL as the first statement in main() (before the banner and sentry::init) is the idiomatic ripgrep/fd approach and covers both Sentry groups (OPENHUMAN-CORE-C stdout/help and OPENHUMAN-CORE-1Q stderr/banner) since it's a global reset. The #[cfg(unix)] guard + non-unix no-op keeps Windows unaffected. No concerns there.

The regression test (tests/cli_sigpipe.rs) doesn't reliably exercise the bug, though ⚠️

  1. Pipe-buffer race β†’ possible false-green. --help output is a few KB, well under the 64 KB kernel pipe buffer. After drop(child.stdout.take()), the child can write its entire output into the buffer in a single write() that succeeds immediately, then exit cleanly β€” never hitting EPIPE, with or without the fix. So this test can pass even if the bug were reintroduced. To actually force the broken-pipe path, mirror the issue's repro (--help | head -n 1): read one line, then close, so the next write fails β€” or emit output larger than the pipe buffer.

  2. No exit-status assertion. Acceptance criterion #1 asks for a clean exit status; the test only greps stderr for "Broken pipe"/"panicked" and ignores output.status. CodeRabbit's nit (assert status.success() || status.signal() == Some(SIGPIPE)) addresses this, but that change currently lives in a helper PR against the fork (YonganZhang/openhuman#4) and is not in this branch β€” the diff here still has the weaker test.

Suggested before merge: fold the exit-status assertion and a repro that truly triggers EPIPE (read-one-line-then-close, or oversized output) directly into this branch, so "regression safety" is met substantively rather than nominally.

YellowSnnowmann avatar Jun 18 '26 07:06 YellowSnnowmann

[!CAUTION] Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted.

Error details
{}

coderabbitai[bot] avatar Jun 19 '26 06:06 coderabbitai[bot]