Skip to content

refactor(ssh): pass ssh stderr through like upstream - #8224

Merged
oferchen merged 4 commits into
masterfrom
refactor/ssh-stderr-inherit-like-upstream
Oct 20, 2026
Merged

oferchen merged 4 commits into
masterfrom
refactor/ssh-stderr-inherit-like-upstream

Conversation

@oferchen

@oferchen oferchen commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Inherit stderr: the remote shell now inherits oc-rsync's stderr, as upstream piped_child() arranges it (pipe.c:48-97 redirects only stdin and stdout). ssh's messages reach the terminal byte for byte, as soon as they are written, and exactly once.
  • Server stderr blocking: the server sets its stderr blocking after protocol setup, as upstream start_server() does (main.c:1356-1364).
  • Removed: the oc-only stderr capture, its drain threads and fallback warnings, the unused socketpair_stderr module, the async stderr drain, the ssh-socketpair-stderr build feature and the captured-stderr accessors. Supersedes feat(ssh): build the socketpair stderr channel by default #8219.

Details

Upstream vs oc before this change

Stream Upstream 3.5.1 oc before oc after
ssh stdin/stdout fd_pair() socketpairs (pipe.c:57, util1.c) socketpairs (wire.rs) unchanged
ssh stderr inherited captured on a third socketpair, re-printed line by line, 64 KiB tail kept inherited
server fd 2 after setup set_blocking(STDERR_FILENO) (main.c:1364) untouched (only fds 0 and 1) set blocking

Defects the capture caused, all fixed by inheriting

  • Bytes rewritten: non-UTF-8 output from ssh was rewritten to U+FFFD.
  • Output held back: output without a trailing newline (a prompt, a progress fragment) was held until the next newline or EOF.
  • Duplicate error: a failed ssh's error was printed a second time when the connection was dropped without wait().
  • Not a tty: ssh's stderr was not a terminal.
  • Fragile shutdown: the drain had a 50 ms shutdown bound for helpers that keep the write end open.

Removed API

  • SshConnection::stderr_output and SshConnection::wait_with_stderr.
  • SshChildHandle::stderr_output and SshChildHandle::wait_with_stderr.
  • AsyncSshTransport::stderr_capture.
  • rsync_io::ssh::socketpair_stderr.
  • AsyncStderrDrain, RingBuffer and ASYNC_STDERR_BUFFER_CAP.

Nothing in core, cli, daemon or transfer called them; only rsync_io's own tests did. rsync_io no longer depends on tracing.

Kept unchanged

  • SshChildHandle reaping and map_child_exit_status.
  • The stdin/stdout socketpair wire.
  • The documented blocking parent-end deviation (wire.rs:66).
  • The embedded russh path, which has no child process. It forwards remote extended data to stderr verbatim, matching the subprocess path; it needs a separate review.

Upstream oracle

Run on the physical Linux host against /usr/bin/rsync 3.5.1-g04355d27. Every run used a fake remote shell; -e and --rsync-path pointed at the binary under test.

Case upstream 3.5.1 this branch master
rsh prints Connection refused, exits 255 exit 255, line printed once exit 255, line printed once exit 255, line printed once
rsh prints banner:\377\376-no-newline then runs the server exit 0, bytes verbatim exit 0, stderr byte-identical to upstream (cmp) exit 0, \377\376 rewritten to U+FFFD
same with -vvv exit 0, bytes verbatim exit 0, bytes verbatim exit 0, bytes rewritten

Perf gate

Quiet host (load 0.47-1.95), release builds, ssh localhost. Each case ran 9 interleaved rounds of master, branch and a master A/A copy, plus three strace -c -f totals per binary.

Case master median branch median A/A median branch vs master A/A vs master syscalls master / branch / A/A (mean of 3)
push, 2000 x 4 KiB 0.346 s 0.338 s 0.352 s -2.3% +1.9% 6117 / 6106 / 6132
pull, 2000 x 4 KiB 0.349 s 0.353 s 0.349 s +1.1% -0.2% 30824 / 30873 / 30991
push, 256 MiB 0.514 s 0.524 s 0.514 s +1.9% 0.0% 53566 / 53536 / 53569
push -vvv 0.361 s 0.346 s 0.351 s -4.2% -2.8% 43899 / 43339 / 43238

Every wall delta is under 3%, and every syscall delta is within the A/A spread.

Test plan

  • CI: fmt + clippy, nextest (stable), Windows, macOS, Linux musl.
  • cargo nextest run -p rsync_io --all-features -E 'binary(ssh_stderr_child_exit)' covers four tests:
    • stderr bytes, including non-UTF-8 and output with no trailing newline, arrive unmodified while the shell is still running;
    • a failing shell's error appears exactly once on the wait() path;
    • the error also appears exactly once on the drop path;
    • a re-exec helper. Each test re-executes the test binary as the client and reads its real stderr.
  • cargo nextest run --all-features -E 'binary(server_stderr_blocking)': a pull through a local remote shell, with the client's stderr pipe set non-blocking beforehand, leaves the shared stderr blocking.
  • cargo nextest run -p fast_io --all-features -E 'test(force_blocking)': force_blocking_stderr clears O_NONBLOCK on fd 2, in a forked child.
  • Mutation evidence: the new test files were run against master's capture implementation.
    • The byte and unbuffered test fails there (timeout: the fragment arrives only at EOF).
    • The drop-path test fails (the error is printed twice).
    • The server test fails (stderr is left non-blocking).
    • All pass on this branch.
  • cargo nextest run -p rsync_io -p fast_io -p core -p cli --all-features: 9611 passed.
  • cargo nextest run -p transfer -p daemon --all-features: 5388 passed.
  • Root SSH and diagnostics tests (ssh_transport, server_diagnostics_reach_client, server_stderr_blocking, /ssh/): 31 passed.
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings, the same with default features, and -p rsync_io --no-default-features.
  • cargo check --target x86_64-pc-windows-gnu -p rsync_io -p fast_io -p transfer -p core --all-targets --all-features.
  • cargo fmt --all -- --check and python3 tools/ci/check_rustfmt_all.py.
  • python3 tools/ci/unsafe_owner_deps.py, cargo run -p xtask -- no-placeholders and cargo run -p xtask -- citations.

Coordination

Conventional commit prefix

refactor:

Security considerations

  • Threat surface affected: the remote-shell transport's stderr handling and the server's fd 2 flags. No wire bytes change, and nothing reads or parses ssh's stderr any more, which removes a parser and two threads from the transport.
  • Mitigations / tests added:
    • ssh_stderr_child_exit.rs pins byte-exact, unbuffered, exactly-once passthrough.
    • server_stderr_blocking.rs pins the post-setup blocking reset.
    • The deadlock regression test in rsync_io/src/ssh/tests.rs still guards against an undrained stderr pipe.
  • Cross-references:
    • docs/ssh-transport.md (SSH stderr);
    • docs/design/socketpair-stderr-channel.md, marked superseded;
    • upstream pipe.c:48-97 and main.c:1356-1364.

Pre-merge checklist

  • Conventional commit prefix on PR title matches the change category.
  • cargo fmt --all run locally.
  • CHANGELOG.md updated if user-visible. Links use #8224, to be replaced with this PR's number.
  • SECURITY.md updated if security-relevant. Not changed: no CVE or threat-model entry depends on the removed capture.
  • No references to internal tooling or non-human authoring aids; hyphens, not em-dashes.

Follow-up: one write per stderr line (a7834cc)

CI (Linux musl (nightly)) caught rsh_peer_eof_whine reading : connection unexpectedly closed ... with its oc-rsync prefix torn off. writeln! on the unbuffered Stderr issues one write per formatted piece: strace showed the line leaving as four writes (oc-rsync, : , the message, \n). With the ssh child now sharing fd 2, the remote side's output landed between them.

cli::run now wraps stderr in logging_sink::LineAtomicWriter. It emits each completed line, ending in \n or \r, with a single write_all, as upstream rwrite() does (log.c:269-427).

Check Result
strace, failing rsh 8 writes became 2 (one per line)
rsh_peer_eof_whine, 50 runs under load 42/50 without the fix, 50/50 with it
-avvv --msgs2stderr, 200 files, write syscalls 631 became 408; totals within noise
Mutation: writer passes pieces through 3 of 4 unit tests red
Mutation: cli::run without the wrap both cli/tests/stderr_whole_line_writes.rs tests red

upstream main.c:1356-1364 start_server() calls set_blocking(STDERR) once
setup_protocol() is done, because ssh may have left fd 2 non-blocking and
that loses print output when stderr is a clone of stdout. The server now
does the same through fast_io::force_blocking_stderr.
upstream pipe.c:48-97 piped_child() redirects only stdin and stdout, so
the remote shell writes straight to rsync's own stderr. oc captured it on
a third socketpair and re-printed it line by line, which rewrote non-UTF-8
bytes to U+FFFD, held back output without a trailing newline, and printed
a failed ssh's error a second time on the drop path. The child now
inherits stderr. The capture channel, its drain threads and fallback
warnings, the socketpair_stderr module, the async stderr drain, the
ssh-socketpair-stderr feature and the captured-stderr accessors are
removed.
write!/writeln! issue one write per formatted piece, and Stderr is
unbuffered, so a single line such as the connection-closed message
reached fd 2 as four writes. Now that the ssh child inherits the same
descriptor, the remote side's output could land between them and tear
the line. cli::run now wraps stderr in a line-assembling writer that
emits each completed line (ending in a newline or carriage return) with
one write_all, as upstream rwrite() does (log.c:269-427).
@oferchen
oferchen merged commit 334dd29 into master Oct 20, 2026
151 of 153 checks passed
@oferchen
oferchen deleted the refactor/ssh-stderr-inherit-like-upstream branch October 20, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant