Skip to content

fix(pipe): preserve websocket bytes after upgrade - #1288

Merged
FlorentinDUBOIS merged 3 commits into
mainfrom
fix/ws-upgrade-leftover-bytes
Jul 8, 2026
Merged

FlorentinDUBOIS merged 3 commits into
mainfrom
fix/ws-upgrade-leftover-bytes

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Jul 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Preserve backend bytes that arrive in the same read buffer as a WebSocket 101 Switching Protocols response.
  • Sync mux buffers before the upgrade handoff, then restore inherited readiness through Pipe::restore_readiness_events(...) so already-buffered bytes keep a writable event after HTTP/HTTPS handoff restores mux readiness.
  • Add e2e coverage for a backend write containing both the 101 response and the first WebSocket text frame.
  • Add pipe-level regression coverage for inherited buffered bytes after readiness-event restore.
  • Collapse two existing clippy-only nested conditionals required by the repository pre-push all-features gate.

Root Cause

The H1 mux returned SessionResult::Upgrade without syncing Kawa's remaining post-header bytes back into the Checkout buffer window. When the backend sent 101 Switching Protocols and the first WebSocket frame in the same TCP segment, the pipe inherited a buffer whose readable window did not expose the post-101 bytes.

The first fix also needed one follow-up: HTTP and HTTPS restore the pre-upgrade mux readiness events after constructing the Pipe, so any synthetic WRITABLE event set during Pipe::new() could be erased. Pipe::restore_readiness_events(...) now performs that restore and then re-arms writable readiness for inherited frontend/backend buffers.

User Impact

This fixes WebSocket backends such as Reverb, Soketi, and Pusher-compatible servers that send connection_established immediately after the upgrade response. The first frame is now forwarded even when it shares the same backend read buffer as the 101 response.

Review Follow-Up

Addressed the inline review on lib/src/protocol/pipe.rs by moving the inherited-event restore behind a Pipe helper that re-arms buffered writes after the restore. The new restore_readiness_events_rearms_inherited_buffered_writes regression was observed red before the re-arm and green after the fix.

Validation

  • cargo test -p sozu-lib restore_readiness_events_rearms_inherited_buffered_writes --locked
  • cargo test -p sozu-lib protocol::pipe --locked
  • cargo test -p sozu-e2e test_websocket_backend_frame_in_same_read_as_101 --locked
  • cargo test -p sozu-e2e websocket --locked
  • cargo +nightly fmt --all -- --check
  • cargo build --all-features --locked
  • cargo clippy --all-targets --locked -- -D warnings
  • cargo test --workspace --locked
  • cargo clippy --all-features --all-targets --locked -- -D warnings
  • git diff --check

When a backend sends a WebSocket 101 response and the first frame in the same TCP read, the H1 mux can parse the headers while leaving post-upgrade bytes in Kawa's remaining buffer window. Sync that window before handing the connection to Pipe, then arm writable readiness for any inherited direction buffers so edge-triggered epoll flushes them immediately.

Add an e2e regression for a same-buffer 101 plus WebSocket text frame.

Signed-off-by: Florentin Dubois <[email protected]>
Collapse two nested conditionals that Rust 1.91 clippy rejects when running the repository pre-push all-features gate.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS marked this pull request as ready for review July 8, 2026 10:21

@FlorentinDUBOIS FlorentinDUBOIS left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated cross-review (review / review-code / guidelines / simplify / security-review + Codex). One finding inline; comments batched into one review. GitHub does not allow the PR author account to request changes on its own PR, so this is submitted as a comment review.

Comment thread lib/src/protocol/pipe.rs Resolved
Restore the mux readiness events through a Pipe helper that re-arms writable readiness for buffers already inherited at upgrade time. This prevents the HTTP and HTTPS upgrade handoffs from erasing the synthetic writable event needed to flush bytes that were exposed by the mux buffer sync.

Add a pipe regression covering event restore with inherited frontend and backend buffered bytes.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 7fb58e4 into main Jul 8, 2026
22 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/ws-upgrade-leftover-bytes branch July 8, 2026 12:16
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