Incomplete receive data is delivered as a complete WebSocket message

Open Beginner friendly
#68 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
swift
Domain
networking

Research direction

Start at NWWebSocket.listen(connection:generation:) and inspect the NWConnection.receiveMessage callback, including its data, context, isComplete, and error values. Add regression coverage for complete, incomplete, empty, and missing-context receives, then verify that only complete data reaches the delegate while transport errors still stop the receive loop.

Written by the indexing model from the issue text.

Description

Problem

NWWebSocket.listen(connection:generation:) ignores the isComplete value from NWConnection.receiveMessage.

The completion can contain non-empty data, isComplete == false, and a transport error. The current code sends that data to receiveMessage(data:context:) before it handles the error.

A text delegate can therefore receive an incomplete UTF-8 or JSON message as a complete WebSocket message.

Apple documents receiveMessage as a complete-message receive API and provides isComplete in the callback:
https://developer.apple.com/documentation/network/nwconnection/receivemessage(completion:)

Current behavior

connection?.receiveMessage { [weak self] (data, context, _, error) in
    // ...
    if let data = data, !data.isEmpty, let context = context {
        self.receiveMessage(data: data, context: context)
    }
    // The transport error is handled after data delivery.
}

Expected behavior

The library sends data to its delegate only when isComplete is true.

The existing error path continues to report the transport failure and stop the receive loop.

Proposed fix

  • Read the isComplete callback value.
  • Require isComplete before message delivery.
  • Add regression tests for complete, incomplete, empty, and missing-context receive values.

I searched the existing issues and pull requests for isComplete, receiveMessage, partial data, incomplete data, truncated data, and fragments. I found no report for this case.

Dominant language
Swift
Stars
161
Forks
36
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pusher/NWWebSocket

All issues in pusher/NWWebSocket

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.