Skip to content

handle WebSocket implementations missing bufferedAmount#3497

Open
ernest-okot wants to merge 1 commit into
libp2p:mainfrom
ernest-okot:patch-1
Open

handle WebSocket implementations missing bufferedAmount#3497
ernest-okot wants to merge 1 commit into
libp2p:mainfrom
ernest-okot:patch-1

Conversation

@ernest-okot
Copy link
Copy Markdown

@ernest-okot ernest-okot commented May 11, 2026

Title

Description

React Native's WebSocket implementation does not expose bufferedAmount (it's undefined). The current code in WebSocketMultiaddrConnection.sendData compares it directly with a number, which evaluates undefined < 4MB to false, marking every write as needing drain. The eventual 'close' event then makes pEvent reject with undefined, surfacing in Upgrader._encryptOutbound as the cryptic TypeError: Cannot read property 'message' of undefined.

Same guard applied to checkBufferedAmount, which otherwise starts a polling task that never terminates.

Reproducer: a React Native Expo project using @libp2p/websockets to dial a Node.js relay. Before the fix, every dial failed at mss.select with an undefined message error. After the fix, dial completes and noise handshake proceeds normally.

Refs

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

React Native's WebSocket implementation does not expose bufferedAmount (it's undefined). The current code in WebSocketMultiaddrConnection.sendData compares it directly with a number, which evaluates undefined < 4MB to false, marking every write as needing drain. The eventual 'close' event then makes pEvent reject with undefined, surfacing in Upgrader._encryptOutbound as the cryptic TypeError: Cannot read property 'message' of undefined.

Same guard applied to checkBufferedAmount, which otherwise starts a polling task that never terminates.

Testing

Reproducer: a React Native Expo project using @libp2p/websockets to dial a Node.js relay. Before the fix, every dial fails at mss.select with the undefined-message error. After the fix, dial completes and noise  handshake proceeds normally.
 
Refs
- React Native WebSocket source: https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/WebSocket/WebSocket.js — bufferedAmount is not implemented.

- MDN: bufferedAmount is required for browsers but not all WebSocket implementations conform.
@ernest-okot ernest-okot requested a review from a team as a code owner May 11, 2026 16:58
@tabcat
Copy link
Copy Markdown
Member

tabcat commented May 12, 2026

This would effectively remove all backpressure guards from the transport inside of React Native and any other environment where bufferedAmount is undefined. You're very likely to hit errors further down the road which are harder to debug.

Have you tried other transports for your usecase? I'm not able to find a good way to handle this atm.

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.

2 participants