Repository navigation
fix(transfer): flush zlib end-of-stream before daemon-sender close (UTS-9.REOPEN) - #5710
Merged
Merged
Conversation
…TS-9.REOPEN) Under `-zz` daemon pull the receiver hangs and reports "connection unexpectedly closed (612411 bytes received so far)" mid-goodbye even though every file payload, every flist segment and both phase=2/3 transitions completed. recv_files finishes, the receiver enters its read_final_goodbye, and the daemon-sender's last bytes never reach the wire before FIN. Root cause: the daemon-sender writes its goodbye NDX_DONE through the CompressedWriter and then only calls `writer.flush()` (Z_SYNC_FLUSH). Z_SYNC_FLUSH lets the receiver's CompressedReader decompress all data emitted so far, but the deflate stream stays open. When the socket closes immediately afterwards the receiver-side CompressedReader is still parked on the next deflate block and surfaces the close as a mid-stream truncation. UTS-15.c covered the goodbye flush ordering for the multiplex layer but not the codec layer. Fix: add `ServerWriter::finalize_compression()` which downgrades Compressed mode back to Multiplex by consuming the CompressedWriter and calling its `finish()`. `finish_into_inner()` runs `deflateEnd()` so the zlib end-of-stream trailer rides out before FIN, matching upstream `token.c:send_deflated_token()`. The orchestrator now calls `finalize_compression()` immediately after `handle_goodbye` in place of the bare `writer.flush()` defense-in-depth, so the codec is finalised before the socket closes for every protocol-31+ daemon-sender shutdown. The downgrade preserves multiplex mode so any trailing diagnostic MSG_INFO frame still rides out. Regression coverage in crates/transfer/src/writer/tests.rs: * server_writer_finalize_compression_emits_zlib_eos_for_receiver drives a Compressed ServerWriter through write -> flush -> finalize_compression, decompresses the resulting wire bytes with the production CompressedReader, and asserts `read_to_end` returns the full payload without truncation. Without the fix the receiver-side decoder would observe EOF inside the open deflate block. * server_writer_finalize_compression_is_idempotent_for_plain_and_multiplex asserts the helper is a flush no-op outside Compressed mode and preserves the writer's variant for repeat-call safety. * server_writer_finalize_compression_round_trips_with_subsequent_raw_write verifies trailing post-goodbye frames still ride out cleanly through the downgraded multiplex writer. UTS-10 (daemon-gzip-upload) exercises the daemon-receiver side and is untouched: only the daemon-sender's CompressedWriter is finalised, and only when ServerWriter::Compressed is the active variant. Plain and Multiplex writers fall through to the existing flush path.
oferchen
force-pushed
the
fix/uts-9-reopen-daemon-gzip-download-goodbye
branch
from
June 11, 2026 20:12
b264c17 to
e054638
Compare
This was referenced Jun 11, 2026
oferchen
added a commit
that referenced
this pull request
Aug 24, 2026
…TS-9.REOPEN) (#5710) Under `-zz` daemon pull the receiver hangs and reports "connection unexpectedly closed (612411 bytes received so far)" mid-goodbye even though every file payload, every flist segment and both phase=2/3 transitions completed. recv_files finishes, the receiver enters its read_final_goodbye, and the daemon-sender's last bytes never reach the wire before FIN. Root cause: the daemon-sender writes its goodbye NDX_DONE through the CompressedWriter and then only calls `writer.flush()` (Z_SYNC_FLUSH). Z_SYNC_FLUSH lets the receiver's CompressedReader decompress all data emitted so far, but the deflate stream stays open. When the socket closes immediately afterwards the receiver-side CompressedReader is still parked on the next deflate block and surfaces the close as a mid-stream truncation. UTS-15.c covered the goodbye flush ordering for the multiplex layer but not the codec layer. Fix: add `ServerWriter::finalize_compression()` which downgrades Compressed mode back to Multiplex by consuming the CompressedWriter and calling its `finish()`. `finish_into_inner()` runs `deflateEnd()` so the zlib end-of-stream trailer rides out before FIN, matching upstream `token.c:send_deflated_token()`. The orchestrator now calls `finalize_compression()` immediately after `handle_goodbye` in place of the bare `writer.flush()` defense-in-depth, so the codec is finalised before the socket closes for every protocol-31+ daemon-sender shutdown. The downgrade preserves multiplex mode so any trailing diagnostic MSG_INFO frame still rides out. Regression coverage in crates/transfer/src/writer/tests.rs: * server_writer_finalize_compression_emits_zlib_eos_for_receiver drives a Compressed ServerWriter through write -> flush -> finalize_compression, decompresses the resulting wire bytes with the production CompressedReader, and asserts `read_to_end` returns the full payload without truncation. Without the fix the receiver-side decoder would observe EOF inside the open deflate block. * server_writer_finalize_compression_is_idempotent_for_plain_and_multiplex asserts the helper is a flush no-op outside Compressed mode and preserves the writer's variant for repeat-call safety. * server_writer_finalize_compression_round_trips_with_subsequent_raw_write verifies trailing post-goodbye frames still ride out cleanly through the downgraded multiplex writer. UTS-10 (daemon-gzip-upload) exercises the daemon-receiver side and is untouched: only the daemon-sender's CompressedWriter is finalised, and only when ServerWriter::Compressed is the active variant. Plain and Multiplex writers fall through to the existing flush path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes UTS-9.REOPEN. Under `-zz` daemon pull the receiver hangs and reports "connection unexpectedly closed (612411 bytes received so far)" mid-goodbye even though every file payload, every flist segment and both phase=2/3 transitions completed.
User-reported evidence (2026-06-11 19:42 UTC, sudo runtests.py on 192.168.21.226)
```
test cmd: rsync -avvvvzz rsync://localhost/test-from/ /home/ofer/rsync/testtmp/daemon-gzip-download/to/
[recv] file ./empty, filelist, nolf, text, dir/text, dir/subdir/subsubdir/etc-ltr-list, dir/subdir/subsubdir2/bin-lt-list all received OK
recv_files phase=1
recv_files phase=2
generate_files phase=2
generate_files phase=3
recv_files finished
rsync: connection unexpectedly closed (612411 bytes received so far) [receiver]
[receiver] _exit_cleanup(code=12, file=io.c, line=232): entered
rsync error: error in rsync protocol data stream (code 12) at io.c(232) [receiver=3.4.3-93-g0d0399bb]
```
Root cause
The daemon-sender writes its goodbye NDX_DONE through the `CompressedWriter` and then only calls `writer.flush()`, which under `-zz` resolves to `Z_SYNC_FLUSH`. Z_SYNC_FLUSH lets the receiver's `CompressedReader` decompress everything emitted so far, but the deflate stream stays open. When the daemon-sender's socket closes immediately afterwards the receiver-side `CompressedReader` is still parked on the next deflate block and surfaces the close as a mid-stream truncation, hence the `612411 bytes received so far` count.
UTS-15.c (PR #5674) closed the goodbye flush ordering on the multiplex layer but the codec layer never received an end-of-stream finalisation.
Fix
Add `ServerWriter::finalize_compression()` which downgrades `Compressed` mode back to `Multiplex` by consuming the `CompressedWriter` and calling its `finish()`. `finish_into_inner()` runs `deflateEnd()` so the zlib end-of-stream trailer rides out before FIN, matching upstream `token.c:send_deflated_token()`.
The orchestrator now calls `finalize_compression()` immediately after `handle_goodbye` in place of the bare `writer.flush()` defense-in-depth, so the codec is finalised before the socket closes for every protocol-31+ daemon-sender shutdown. The downgrade preserves multiplex mode so any trailing diagnostic `MSG_INFO` frame still rides out.
Test plan
Regression coverage in `crates/transfer/src/writer/tests.rs`:
Non-regression
Site of the flush gap
The compressor's `finish()` (which calls `deflateEnd()`) was never invoked at the daemon-sender's pre-close sequence in `crates/transfer/src/generator/transfer/orchestrator.rs`. The codec was only `Z_SYNC_FLUSH`ed via `CompressedWriter::flush_compressed`, leaving the deflate stream open across the socket close. The fix replaces the trailing `writer.flush()` with `writer.finalize_compression()`, which downgrades `Compressed` -> `Multiplex` after `CompressedWriter::finish()` emits the end-of-stream trailer.