Skip to content

fix: exit 12 when the remote shell dies before the version exchange, as upstream - #8283

Merged
oferchen merged 2 commits into
masterfrom
fix/rsh-handshake-epipe-exit-12
Oct 30, 2026
Merged

oferchen merged 2 commits into
masterfrom
fix/rsh-handshake-epipe-exit-12

Conversation

@oferchen

@oferchen oferchen commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • When the remote-shell child dies before the protocol handshake, oc exited 5 (RERR_STARTCLIENT) whenever its version write hit EPIPE before its read saw EOF. That is what made exit_code_12_stream_io::remote_shell_killed_by_signal_before_handshake_returns_stream_io flaky (fix(batch): mirror upstream batch writing, replay and option checks #8250, musl nightly job 123007367229).
  • Upstream 3.5.1 exits 12 on both paths. setup_protocol() (compat.c:606-607) runs before buffered I/O starts, so write_int/read_int go through io.c safe_write()/safe_read(). Both exit RERR_STREAMIO on any failure (io.c:401-405, io.c:356-358), and whine_about_eof() does the same on EOF (io.c:299-303). RERR_STARTCLIENT is only main.c:1981, where start_client() returns -1 before any I/O.
  • The fix has two halves:
    • perform_handshake_with_max now tags a failed version write or read with its step (transfer::VersionExchangeError). The error keeps its io::ErrorKind, and rerr_for_io_error grades the tag as RERR_STREAMIO, not the connection-class RERR_SOCKETIO.
    • One classifier, client::remote::version_exchange::version_exchange_failure, builds the error with upstream's line for each failure: the rsyserr [sender] safe_write failed to write 4 bytes to socket: Broken pipe (32), the safe_read line, or the EOF whine. It still keeps a worse child exit code. The rsh driver, embedded-ssh and async-ssh all use it; the last two used to hard-code 5.

Oracle evidence

I forced the write-first ordering with strace -e inject=write:error=EPIPE:when=1 against the same stub, sh -c 'exec 1>&-; read -r _':

rsync: [sender] safe_write failed to write 4 bytes to socket: Broken pipe (32)
rsync error: error in rsync protocol data stream (code 12) at io.c(405) [sender=3.5.1]   (tarball build)
oc-rsync: [sender] safe_write failed to write 4 bytes to socket: Broken pipe (32)
oc-rsync error: error in rsync protocol data stream (code 12) at error.rs(309) [sender=0.6.4]

The upstream lines come from the 3.5.1 tarball build (/home/ofer/up351/rsync-3.5.1/rsync, "rsync version 3.5.1"), the build CI's run_interop.sh matches.

Runs of the race stub sh -c 'exec 0<&- 1>&-':

client runs rc=12 EOF line safe_write line
upstream 3.5.1 tarball 50 50 49 1
oc (this branch) 100 100 85 15

Upstream itself takes both orderings, so the race belongs to the scenario, not to oc. That is why no e2e test pins the text of a racing stub. Each e2e arm is built so only one ordering is possible.

Before this change, oc exited 5 on the safe_write ordering.

Test plan

  • Unit tests, deterministic with in-memory peers:
    • The handshake tags write EPIPE and read EOF with their step. A tagged write failure grades 12.
    • The classifier gives 12 plus the safe_write line for EPIPE (unix, errno 32), 12 for any write failure, 12 plus the safe_read line for a read error, and 12 plus the whine for EOF.
    • A worse child code wins the exit code but keeps the line.
    • A bad peer version is still 2.
  • tests/exit_codes.rs:
    • remote_shell_killed_by_signal_before_handshake_returns_stream_io now uses a stub that closes its stdout and keeps reading stdin until the client lets go, then kills itself with SIGTERM. The client's write always lands and its read always sees EOF, so there is one ordering and no race. It asserts exit 12 and the EOF line.
    • New remote_shell_closing_stdout_before_handshake_prints_upstream_eof_whine is the strict EOF arm on push. Its stub closes stdout and drains stdin, so the write lands and the read sees EOF. It asserts exactly two stderr lines, byte-compared with the tarball 3.5.1 for the same stub, program name and trailer source location aside: connection unexpectedly closed (0 bytes received so far) [sender] and the error in rsync protocol data stream (code 12) at ... [sender=<version>] trailer.
    • The write-EPIPE arm has no e2e. oc writes its version right after spawning the shell, so no stub can close stdin first without timing. The in-memory unit tests above pin that text strictly.
  • Mutations, one per arm, each killed:
    • untagged write
    • rerr arm off
    • classifier back to RERR_STARTCLIENT
    • the safe_write/safe_read line dropped
    • the rsh driver bypassing the classifier: both e2e tests fail (re-run after the strict EOF arm replaced the either-ordering test)
  • Gates: fmt, workspace clippy -D warnings, the windows-gnu check (-p core -p transfer --tests), and targeted nextest.

Known limits

  • Pull spelling, task 268: on a pull, 3.5.1 labels the pre-fork client receiver [Receiver] in both the EOF line and the trailer ([Receiver=3.5.1]). oc prints [receiver] (exit 12 either way). Task 268 adds that role, with its own strict pull EOF arm. This PR deliberately adds no pull e2e, so no test pins the wrong spelling.

Conventional commit prefix

  • fix:

Pre-merge checklist

  • Conventional commit prefix on PR title matches the change category.
  • cargo fmt --all run locally.
  • CHANGELOG.md updated (#8283 to be replaced after gh pr create).
  • No references to internal tooling or non-human authoring aids; hyphens, not em-dashes.

upstream setup_protocol() writes and reads the protocol int through
io.c safe_write()/safe_read(), which exit RERR_STREAMIO on any failure
(io.c:401-405, io.c:356-358), and whine_about_eof() does the same on
EOF. RERR_STARTCLIENT is only main.c:1981. oc mapped a version-write
EPIPE to 5, so a remote shell that died first exited 5 or 12 depending
on which side of the race the write landed.

The handshake now tags a failed write or read with its step, and one
classifier turns the tag into 12 with upstream's safe_write/safe_read
or EOF line for the rsh, embedded-ssh and async-ssh transports.
@oferchen oferchen closed this Oct 29, 2026
@oferchen oferchen reopened this Oct 29, 2026
@oferchen
oferchen merged commit 5bfb60a into master Oct 30, 2026
76 of 124 checks passed
@oferchen
oferchen deleted the fix/rsh-handshake-epipe-exit-12 branch October 30, 2026 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant