Skip to content

wc_preserves_native_failure_exit_code is racy, EPIPE when the child exits before the write #3739

Closed

Description

@vringar

When running wc_preserves_native_failure_exit_code repeatedly it sometimes fails due to a race condition in

child
.stdin
.take()
.expect("piped stdin")
.write_all(input)
.expect("write stdin");

thread 'wc_preserves_native_failure_exit_code' panicked at tests/pipeline_stdin_test.rs:18:10:
write stdin: Os { code: 32, kind: BrokenPipe, message: "Broken pipe" }

The test spawns wc --definitely-invalid-rtk-test-option, which prints usage and exits right away. If the process exits before the writer is able to write, it fires EPIPE. I measured 15 out of 2000 spawns on an idle 24 core box and 131 out of 2000 with every core busy, so it fails on build machines and stays hidden on idle CI.

Sleeping 50ms before the write makes it deterministic: 50 out of 50 for the invalid option, and 0 out of 50 for wc_reads_piped_stdin, which is unaffected because wc blocks on the read there.

Given that any command that actually reads stdin should have an assert that makes sure the output processed the given input, wc_reads_piped_stdin asserting "2" being the example, it seems safe to drop the error. In that case I propose the following pattern:

let mut stdin = child.stdin.take().expect("piped stdin");
let _ = stdin.write_all(input);
drop(stdin);
child.wait_with_output().expect("wait for command")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions