Skip to content

Fix WebSocket buffered read and add support for fragmented messages #5255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andriydruk
Copy link
Contributor

This PR fixes a bug in Foundation.URLSessionWebSocketTask where incoming WebSocket frames were incorrectly assumed to arrive in a single TCP read. Since TCP can arbitrarily split data, this led to dropped or malformed messages.

Fixes:

  • Accumulates socket data until the entire WebSocket frame is received
  • Adds support for fragmented WebSocket messages (multiple frames using opcode 0x0)

The WebSocket unit tests were extended to cover:

  • Buffered sending – simulates server-side buffering by sending a message in small TCP chunks
  • Fragmented message – splits a single message across multiple WebSocket frames

Add support for fragmented messages

Buffered socket reads could result in incomplete frame parsing due to incorrect assumptions about TCP delivery. This patch introduces proper accumulation of partial reads.

Also adds handling for fragmented WebSocket messages split across multiple frames.
@parkera
Copy link
Contributor

parkera commented Aug 4, 2025

@swift-ci test

@guoye-zhang
Copy link
Contributor

guoye-zhang commented Aug 4, 2025

+@pseligman for review

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.

3 participants