Skip to content

fix(transfer): flush zlib end-of-stream before daemon-sender close (UTS-9.REOPEN) - #5710

Merged
oferchen merged 1 commit into
masterfrom
fix/uts-9-reopen-daemon-gzip-download-goodbye
Jun 11, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/uts-9-reopen-daemon-gzip-download-goodbye

Conversation

@oferchen

Copy link
Copy Markdown
Owner

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`:

  • `server_writer_finalize_compression_emits_zlib_eos_for_receiver` drives a Compressed ServerWriter through write -> flush -> finalize_compression, decompresses the wire bytes via the production `CompressedReader`, and asserts `read_to_end` returns the full payload without truncation (the 612411-byte cutoff symptom).
  • `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.
  • `server_writer_finalize_compression_round_trips_with_subsequent_raw_write` verifies trailing post-goodbye frames still ride out cleanly through the downgraded multiplex writer.

Non-regression

  • 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.
  • Non-daemon zlib transfers retain identical behaviour: the codec finalisation only runs when the generator (daemon-sender) finishes its run; client-mode generators flush exactly as before.

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.

…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
oferchen force-pushed the fix/uts-9-reopen-daemon-gzip-download-goodbye branch from b264c17 to e054638 Compare June 11, 2026 20:12
@oferchen
oferchen merged commit 54a5b7f into master Jun 11, 2026
9 checks passed
@github-actions github-actions Bot added the bug Something isn't working label Jun 11, 2026
@oferchen
oferchen deleted the fix/uts-9-reopen-daemon-gzip-download-goodbye branch June 12, 2026 14:39
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.
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