Skip to content

fix(transfer): half-close stdout after receiver goodbye to break SSH/lsh.sh shutdown deadlock - #5781

Merged
oferchen merged 1 commit into
masterfrom
fix/uts-v3-half-close-server-drain
Jun 14, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/uts-v3-half-close-server-drain

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

The --server receiver under SSH/lsh.sh transports completes its NDX_DONE handshake then enters a drain-stdin loop, but never closes its own write side. The upstream sender, meanwhile, is in noop_io_until_death() reading-until-EOF on the receiver's stdout. Both sides wait for the other to close their write end; neither does.

This is the root cause of cluster-A upstream-testsuite hangs (00-hello, alt-dest, files-from, ssh-basic, symlink-dirlink-basis, hardlinks): the transfer completes, both sides emit goodbye, then 300s timeout.

Root-cause evidence

Reproduced on Linux host, captured kernel stacks of the deadlock:

  • Upstream sender (rsync 3.4.3, the SSH/lsh.sh client):

    hrtimer_nanosleep -> common_nsleep -> __arm64_sys_clock_nanosleep
    

    This is msleep(20) inside upstream's noop_io_until_death() poll loop (io.c:943-963).

  • oc-rsync receiver (--server under lsh.sh):

    unix_stream_data_wait -> unix_stream_read_generic -> ksys_read -> __arm64_sys_read
    

    Blocked in read(fd=0) on the inherited stdin Unix socket.

  • Socket queues: both Send-Q and Recv-Q = 0; no bytes in flight.

Both sides are reading-until-EOF on each other; neither closes its write side first.

The fix

After flushing the final goodbye envelope in the receiver-role --server exit path, issue shutdown(STDOUT_FILENO, SHUT_WR). On the SSH / lsh.sh transport stdout is a stream socket, so the half-close sends FIN to the peer. The peer's read_final_goodbye() then sees EOF, completes exit_cleanup, and closes its own write side. Our drain loop then sees EOF deterministically.

A new fast_io::shutdown_stdio_write() helper wraps the unsafe syscall under SAFETY comments, keeping unsafe code consolidated in fast_io (per the unsafe-code policy).

On non-socket stdout (regular pipe) the syscall returns ENOTSOCK; the caller treats it as best-effort. Windows builds are a no-op since the inherited stdio handles are not sockets there.

Upstream reference

  • target/interop/upstream-src/rsync-3.4.1/io.c:943-963 noop_io_until_death()
  • target/interop/upstream-src/rsync-3.4.1/cleanup.c:254 call in _exit_cleanup

Test plan

  • 00-hello upstream test (was hanging, should pass)
  • alt-dest upstream test (was hanging, should pass)
  • files-from upstream test (was hanging, should pass)
  • ssh-basic upstream test (was hanging, should pass)
  • symlink-dirlink-basis upstream test (was hanging, should pass)
  • hardlinks upstream test (was hanging, should pass)
  • Existing nextest suite remains green (3 new unit tests in stdio_shutdown.rs exercise FIN propagation on a socket pair, ENOTSOCK on a pipe, and BufWriter flush + half-close ordering)
  • No regression to local-copy or daemon-mode transfers (path only applies to --server receiver under SSH/lsh.sh)

@github-actions github-actions Bot added the bug Something isn't working label Jun 14, 2026
…down deadlock

The `--server` receiver under SSH/lsh.sh transports completed its
NDX_DONE handshake then entered a drain-stdin loop, but never closed
its own write side. The upstream sender, meanwhile, was in
`noop_io_until_death()` reading-until-EOF on the receiver's stdout.
Both sides waited for the other to close their write end; neither
did. Kernel stacks under gdb confirmed the deadlock:

  - sender: hrtimer_nanosleep (msleep loop in noop_io_until_death)
  - receiver: unix_stream_data_wait inside read(STDIN_FILENO)

The fix issues `shutdown(STDOUT_FILENO, SHUT_WR)` after flushing the
final goodbye envelope. On the SSH / lsh.sh transport stdout is a
stream socket, so the half-close sends FIN to the peer and unblocks
its `read_final_goodbye`. The peer then exits, closes its own write
side, and the receiver's drain loop sees EOF deterministically.

A new `fast_io::shutdown_stdio_write()` helper wraps the syscall under
SAFETY-commented unsafe (consolidating unsafe into the permitted
crate). On non-socket stdout the syscall returns ENOTSOCK; the caller
treats it as best-effort. Windows builds are a no-op since the
inherited stdio handles are not sockets there.

upstream: io.c:943-963 `noop_io_until_death()`,
          cleanup.c:254 call in `_exit_cleanup`
@oferchen
oferchen force-pushed the fix/uts-v3-half-close-server-drain branch from 9a69e95 to 960a97c Compare June 14, 2026 16:25
@oferchen
oferchen merged commit 30b9ba4 into master Jun 14, 2026
25 of 69 checks passed
oferchen added a commit that referenced this pull request Jun 14, 2026
…ocal-rsh deadlock

The unbounded EOF drain added in PR #5771 to the receiver-role server
exit path deadlocks on the SSH / `--rsh=fake_rsh --rsync-path=oc-rsync`
transport used by the standalone interop harness. The upstream sender's
`read_final_goodbye()` keeps the connection open until our stdout
BufWriter is flushed and closed by process exit; the drain blocks on
its EOF, and process exit cannot run until the drain returns. Each side
waits on the other.

This was visible on master HEAD as two `interop /
interop with upstream rsync` standalone failures, both terminated by
the harness `timeout 30s`:

- `test_iconv_local_ssh_interop` direction-b
  (tools/ci/run_interop.sh:3530)
- `test_compress_ssh_interop` direction-b
  (tools/ci/run_interop.sh:3682)

Bound the drain to 250 ms via a background worker plus
`recv_timeout`. Upstream's `noop_io_until_death()` (io.c:943-963) arms
a 60 s `io_timeout` before its own read loop and treats EOF inside that
window as a clean exit (`kluge_around_eof = 2` -> `exit_cleanup(0)` at
io.c:217-232) - the same behaviour we now adopt with a tighter bound.
250 ms is comfortably above loopback round-trip and well below the
10 s upstream `--timeout` configured by the interop harness.

The wire-protocol goodbye envelope itself is unchanged: only the
post-envelope no-op drain becomes bounded. The cluster A regression
PR #5771 was added to fix is still covered: the peer's trailing bytes
arrive within microseconds of the goodbye and complete the drain
naturally before the 250 ms ceiling.

PR #5780 closed a 200 ms-bound proposal in favour of the deterministic
half-close approach in PR #5781, but CI run 27503163030 confirmed the
half-close alone still hits the same two timeouts. A bounded drain is
required because the peer waits for our stdout FIN, which only fires
on process exit.

upstream: io.c:943-963 `noop_io_until_death()`; io.c:217-232
`whine_about_eof()` clean exit when `kluge_around_eof > 0`; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
oferchen added a commit that referenced this pull request Jun 14, 2026
…ak SSH/local-rsh deadlock

PR #5781 made the receiver-role `--server` exit path call
`shutdown(STDOUT_FILENO, SHUT_WR)` after the goodbye flush so the
upstream peer's `read_final_goodbye()` (io.c:943-963) sees EOF and
its `exit_cleanup` runs. That works for the SSH `SOCK_STREAM`
transport but is a no-op on the `--rsh=fake_rsh --rsync-path=oc-rsync`
interop transport: upstream rsync's remote-shell launcher uses
`popen2`-style pipes, not socketpairs, so `shutdown(SHUT_WR)`
returns ENOTSOCK and the receiver's drain-stdin loop deadlocks
waiting on a peer FIN that never arrives. Two standalone interop
tests on master HEAD hit this deadlock and get killed by the
harness `timeout 30s`:

  - test_iconv_local_ssh_interop direction-b
    (tools/ci/run_interop.sh:3530)
  - test_compress_ssh_interop direction-b
    (tools/ci/run_interop.sh:3682)

`interop / interop with upstream rsync` reports
"Standalone: 69/72 passed, 1 known, 2 unexpected".

Extend `fast_io::shutdown_stdio_write` to fall back to
`dup2(/dev/null, STDOUT_FILENO)` when `shutdown(SHUT_WR)` returns
ENOTSOCK. The dup2 atomically closes the original pipe write end
(the peer's read returns 0 deterministically) while keeping FD 1 a
valid writable sink so any late stray write to stdout does not
error with EBADF. The SSH `SOCK_STREAM` half-close path is
unchanged; only the pipe fallback is new.

Update the pipe regression test to assert EOF observability via the
new fallback path, and add a direct test of `close_write_side` on a
pipe FD without touching STDOUT_FILENO so it runs safely under
parallel test threads.

The wire-protocol goodbye envelope itself is unchanged: `flush()`
still pushes the buffered NDX_DONE down before the close, and the
peer observes the same bytes followed by EOF on both transports.

upstream: io.c:943-963 `noop_io_until_death()` reads until EOF;
io.c:217-232 `whine_about_eof()` treats EOF inside the
`kluge_around_eof` window as a clean exit; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
@oferchen
oferchen deleted the fix/uts-v3-half-close-server-drain branch June 14, 2026 17:17
oferchen added a commit that referenced this pull request Jun 14, 2026
…ak SSH/local-rsh deadlock

PR #5781 made the receiver-role `--server` exit path call
`shutdown(STDOUT_FILENO, SHUT_WR)` after the goodbye flush so the
upstream peer's `read_final_goodbye()` (io.c:943-963) sees EOF and
its `exit_cleanup` runs. That works for the SSH `SOCK_STREAM`
transport but is a no-op on the `--rsh=fake_rsh --rsync-path=oc-rsync`
interop transport: upstream rsync's remote-shell launcher uses
`popen2`-style pipes, not socketpairs, so `shutdown(SHUT_WR)`
returns ENOTSOCK and the receiver's drain-stdin loop deadlocks
waiting on a peer FIN that never arrives. Two standalone interop
tests on master HEAD hit this deadlock and get killed by the
harness `timeout 30s`:

  - test_iconv_local_ssh_interop direction-b
    (tools/ci/run_interop.sh:3530)
  - test_compress_ssh_interop direction-b
    (tools/ci/run_interop.sh:3682)

`interop / interop with upstream rsync` reports
"Standalone: 69/72 passed, 1 known, 2 unexpected".

Extend `fast_io::shutdown_stdio_write` to fall back to
`dup2(/dev/null, STDOUT_FILENO)` when `shutdown(SHUT_WR)` returns
ENOTSOCK. The dup2 atomically closes the original pipe write end
(the peer's read returns 0 deterministically) while keeping FD 1 a
valid writable sink so any late stray write to stdout does not
error with EBADF. The SSH `SOCK_STREAM` half-close path is
unchanged; only the pipe fallback is new.

Update the pipe regression test to assert EOF observability via the
new fallback path, and add a direct test of `close_write_side` on a
pipe FD without touching STDOUT_FILENO so it runs safely under
parallel test threads.

The wire-protocol goodbye envelope itself is unchanged: `flush()`
still pushes the buffered NDX_DONE down before the close, and the
peer observes the same bytes followed by EOF on both transports.

upstream: io.c:943-963 `noop_io_until_death()` reads until EOF;
io.c:217-232 `whine_about_eof()` treats EOF inside the
`kluge_around_eof` window as a clean exit; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
oferchen added a commit that referenced this pull request Jun 14, 2026
…ak SSH/local-rsh deadlock

PR #5781 made the receiver-role `--server` exit path call
`shutdown(STDOUT_FILENO, SHUT_WR)` after the goodbye flush so the
upstream peer's `read_final_goodbye()` (io.c:943-963) sees EOF and
its `exit_cleanup` runs. That works for the SSH `SOCK_STREAM`
transport but is a no-op on the `--rsh=fake_rsh --rsync-path=oc-rsync`
interop transport: upstream rsync's remote-shell launcher uses
`popen2`-style pipes, not socketpairs, so `shutdown(SHUT_WR)`
returns ENOTSOCK and the receiver's drain-stdin loop deadlocks
waiting on a peer FIN that never arrives. Two standalone interop
tests on master HEAD hit this deadlock and get killed by the
harness `timeout 30s`:

  - test_iconv_local_ssh_interop direction-b
    (tools/ci/run_interop.sh:3530)
  - test_compress_ssh_interop direction-b
    (tools/ci/run_interop.sh:3682)

`interop / interop with upstream rsync` reports
"Standalone: 69/72 passed, 1 known, 2 unexpected".

Extend `fast_io::shutdown_stdio_write` to fall back to
`dup2(/dev/null, STDOUT_FILENO)` when `shutdown(SHUT_WR)` returns
ENOTSOCK. The dup2 atomically closes the original pipe write end
(the peer's read returns 0 deterministically) while keeping FD 1 a
valid writable sink so any late stray write to stdout does not
error with EBADF. The SSH `SOCK_STREAM` half-close path is
unchanged; only the pipe fallback is new.

Update the pipe regression test to assert EOF observability via the
new fallback path, and add a direct test of `close_write_side` on a
pipe FD without touching STDOUT_FILENO so it runs safely under
parallel test threads.

The wire-protocol goodbye envelope itself is unchanged: `flush()`
still pushes the buffered NDX_DONE down before the close, and the
peer observes the same bytes followed by EOF on both transports.

upstream: io.c:943-963 `noop_io_until_death()` reads until EOF;
io.c:217-232 `whine_about_eof()` treats EOF inside the
`kluge_around_eof` window as a clean exit; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
oferchen added a commit that referenced this pull request Jun 14, 2026
…ak SSH/local-rsh deadlock

PR #5781 made the receiver-role `--server` exit path call
`shutdown(STDOUT_FILENO, SHUT_WR)` after the goodbye flush so the
upstream peer's `read_final_goodbye()` (io.c:943-963) sees EOF and
its `exit_cleanup` runs. That works for the SSH `SOCK_STREAM`
transport but is a no-op on the `--rsh=fake_rsh --rsync-path=oc-rsync`
interop transport: upstream rsync's remote-shell launcher uses
`popen2`-style pipes, not socketpairs, so `shutdown(SHUT_WR)`
returns ENOTSOCK and the receiver's drain-stdin loop deadlocks
waiting on a peer FIN that never arrives. Two standalone interop
tests on master HEAD hit this deadlock and get killed by the
harness `timeout 30s`:

  - test_iconv_local_ssh_interop direction-b
    (tools/ci/run_interop.sh:3530)
  - test_compress_ssh_interop direction-b
    (tools/ci/run_interop.sh:3682)

`interop / interop with upstream rsync` reports
"Standalone: 69/72 passed, 1 known, 2 unexpected".

Extend `fast_io::shutdown_stdio_write` to fall back to
`dup2(/dev/null, STDOUT_FILENO)` when `shutdown(SHUT_WR)` returns
ENOTSOCK. The dup2 atomically closes the original pipe write end
(the peer's read returns 0 deterministically) while keeping FD 1 a
valid writable sink so any late stray write to stdout does not
error with EBADF. The SSH `SOCK_STREAM` half-close path is
unchanged; only the pipe fallback is new.

Update the pipe regression test to assert EOF observability via the
new fallback path, and add a direct test of `close_write_side` on a
pipe FD without touching STDOUT_FILENO so it runs safely under
parallel test threads.

The wire-protocol goodbye envelope itself is unchanged: `flush()`
still pushes the buffered NDX_DONE down before the close, and the
peer observes the same bytes followed by EOF on both transports.

upstream: io.c:943-963 `noop_io_until_death()` reads until EOF;
io.c:217-232 `whine_about_eof()` treats EOF inside the
`kluge_around_eof` window as a clean exit; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
oferchen added a commit that referenced this pull request Jun 14, 2026
…ak SSH/local-rsh deadlock

PR #5781 made the receiver-role `--server` exit path call
`shutdown(STDOUT_FILENO, SHUT_WR)` after the goodbye flush so the
upstream peer's `read_final_goodbye()` (io.c:943-963) sees EOF and
its `exit_cleanup` runs. That works for the SSH `SOCK_STREAM`
transport but is a no-op on the `--rsh=fake_rsh --rsync-path=oc-rsync`
interop transport: upstream rsync's remote-shell launcher uses
`popen2`-style pipes, not socketpairs, so `shutdown(SHUT_WR)`
returns ENOTSOCK and the receiver's drain-stdin loop deadlocks
waiting on a peer FIN that never arrives. Two standalone interop
tests on master HEAD hit this deadlock and get killed by the
harness `timeout 30s`:

  - test_iconv_local_ssh_interop direction-b
    (tools/ci/run_interop.sh:3530)
  - test_compress_ssh_interop direction-b
    (tools/ci/run_interop.sh:3682)

`interop / interop with upstream rsync` reports
"Standalone: 69/72 passed, 1 known, 2 unexpected".

Extend `fast_io::shutdown_stdio_write` to fall back to
`dup2(/dev/null, STDOUT_FILENO)` when `shutdown(SHUT_WR)` returns
ENOTSOCK. The dup2 atomically closes the original pipe write end
(the peer's read returns 0 deterministically) while keeping FD 1 a
valid writable sink so any late stray write to stdout does not
error with EBADF. The SSH `SOCK_STREAM` half-close path is
unchanged; only the pipe fallback is new.

Update the pipe regression test to assert EOF observability via the
new fallback path, and add a direct test of `close_write_side` on a
pipe FD without touching STDOUT_FILENO so it runs safely under
parallel test threads.

The wire-protocol goodbye envelope itself is unchanged: `flush()`
still pushes the buffered NDX_DONE down before the close, and the
peer observes the same bytes followed by EOF on both transports.

upstream: io.c:943-963 `noop_io_until_death()` reads until EOF;
io.c:217-232 `whine_about_eof()` treats EOF inside the
`kluge_around_eof` window as a clean exit; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
oferchen added a commit that referenced this pull request Jun 14, 2026
…ak SSH/local-rsh deadlock (#5786)

PR #5781 made the receiver-role `--server` exit path call
`shutdown(STDOUT_FILENO, SHUT_WR)` after the goodbye flush so the
upstream peer's `read_final_goodbye()` (io.c:943-963) sees EOF and
its `exit_cleanup` runs. That works for the SSH `SOCK_STREAM`
transport but is a no-op on the `--rsh=fake_rsh --rsync-path=oc-rsync`
interop transport: upstream rsync's remote-shell launcher uses
`popen2`-style pipes, not socketpairs, so `shutdown(SHUT_WR)`
returns ENOTSOCK and the receiver's drain-stdin loop deadlocks
waiting on a peer FIN that never arrives. Two standalone interop
tests on master HEAD hit this deadlock and get killed by the
harness `timeout 30s`:

  - test_iconv_local_ssh_interop direction-b
    (tools/ci/run_interop.sh:3530)
  - test_compress_ssh_interop direction-b
    (tools/ci/run_interop.sh:3682)

`interop / interop with upstream rsync` reports
"Standalone: 69/72 passed, 1 known, 2 unexpected".

Extend `fast_io::shutdown_stdio_write` to fall back to
`dup2(/dev/null, STDOUT_FILENO)` when `shutdown(SHUT_WR)` returns
ENOTSOCK. The dup2 atomically closes the original pipe write end
(the peer's read returns 0 deterministically) while keeping FD 1 a
valid writable sink so any late stray write to stdout does not
error with EBADF. The SSH `SOCK_STREAM` half-close path is
unchanged; only the pipe fallback is new.

Update the pipe regression test to assert EOF observability via the
new fallback path, and add a direct test of `close_write_side` on a
pipe FD without touching STDOUT_FILENO so it runs safely under
parallel test threads.

The wire-protocol goodbye envelope itself is unchanged: `flush()`
still pushes the buffered NDX_DONE down before the close, and the
peer observes the same bytes followed by EOF on both transports.

upstream: io.c:943-963 `noop_io_until_death()` reads until EOF;
io.c:217-232 `whine_about_eof()` treats EOF inside the
`kluge_around_eof` window as a clean exit; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
oferchen added a commit that referenced this pull request Aug 24, 2026
…down deadlock (#5781)

The `--server` receiver under SSH/lsh.sh transports completed its
NDX_DONE handshake then entered a drain-stdin loop, but never closed
its own write side. The upstream sender, meanwhile, was in
`noop_io_until_death()` reading-until-EOF on the receiver's stdout.
Both sides waited for the other to close their write end; neither
did. Kernel stacks under gdb confirmed the deadlock:

  - sender: hrtimer_nanosleep (msleep loop in noop_io_until_death)
  - receiver: unix_stream_data_wait inside read(STDIN_FILENO)

The fix issues `shutdown(STDOUT_FILENO, SHUT_WR)` after flushing the
final goodbye envelope. On the SSH / lsh.sh transport stdout is a
stream socket, so the half-close sends FIN to the peer and unblocks
its `read_final_goodbye`. The peer then exits, closes its own write
side, and the receiver's drain loop sees EOF deterministically.

A new `fast_io::shutdown_stdio_write()` helper wraps the syscall under
SAFETY-commented unsafe (consolidating unsafe into the permitted
crate). On non-socket stdout the syscall returns ENOTSOCK; the caller
treats it as best-effort. Windows builds are a no-op since the
inherited stdio handles are not sockets there.

upstream: io.c:943-963 `noop_io_until_death()`,
          cleanup.c:254 call in `_exit_cleanup`
oferchen added a commit that referenced this pull request Aug 24, 2026
…ak SSH/local-rsh deadlock (#5786)

PR #5781 made the receiver-role `--server` exit path call
`shutdown(STDOUT_FILENO, SHUT_WR)` after the goodbye flush so the
upstream peer's `read_final_goodbye()` (io.c:943-963) sees EOF and
its `exit_cleanup` runs. That works for the SSH `SOCK_STREAM`
transport but is a no-op on the `--rsh=fake_rsh --rsync-path=oc-rsync`
interop transport: upstream rsync's remote-shell launcher uses
`popen2`-style pipes, not socketpairs, so `shutdown(SHUT_WR)`
returns ENOTSOCK and the receiver's drain-stdin loop deadlocks
waiting on a peer FIN that never arrives. Two standalone interop
tests on master HEAD hit this deadlock and get killed by the
harness `timeout 30s`:

  - test_iconv_local_ssh_interop direction-b
    (tools/ci/run_interop.sh:3530)
  - test_compress_ssh_interop direction-b
    (tools/ci/run_interop.sh:3682)

`interop / interop with upstream rsync` reports
"Standalone: 69/72 passed, 1 known, 2 unexpected".

Extend `fast_io::shutdown_stdio_write` to fall back to
`dup2(/dev/null, STDOUT_FILENO)` when `shutdown(SHUT_WR)` returns
ENOTSOCK. The dup2 atomically closes the original pipe write end
(the peer's read returns 0 deterministically) while keeping FD 1 a
valid writable sink so any late stray write to stdout does not
error with EBADF. The SSH `SOCK_STREAM` half-close path is
unchanged; only the pipe fallback is new.

Update the pipe regression test to assert EOF observability via the
new fallback path, and add a direct test of `close_write_side` on a
pipe FD without touching STDOUT_FILENO so it runs safely under
parallel test threads.

The wire-protocol goodbye envelope itself is unchanged: `flush()`
still pushes the buffered NDX_DONE down before the close, and the
peer observes the same bytes followed by EOF on both transports.

upstream: io.c:943-963 `noop_io_until_death()` reads until EOF;
io.c:217-232 `whine_about_eof()` treats EOF inside the
`kluge_around_eof` window as a clean exit; cleanup.c:254
`noop_io_until_death()` call in `_exit_cleanup`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant