Skip to content

WebSocket dispatches message events after close() has been called #59289

Closed

Description

@davidliu

Description

After WebSocket.close() has been called, React Native's WebSocket still fires onmessage for frames that arrive while the socket is closing (readyState === 2).

The WebSockets spec says these messages must be ignored. When a WebSocket message has been received: "If ready state is not OPEN (1), then return." Browsers follow this, and so do spec-compliant implementations such as Node's built-in WebSocket.

In packages/react-native/Libraries/WebSocket/WebSocket.js, the websocketMessage listener checks only the socket id before dispatching. It never checks readyState.

Steps to reproduce

See reproducer here: #59287

  1. Connect to a server that greets the websocket (reproducer uses echo.websocket.org)
  2. Close the websocket immediately before any messages arrive.
  3. Observe the onmessage events still being emitted.

React Native Version

0.88.0

Affected Platforms

Runtime - Android, Runtime - iOS

Output of npx @react-native-community/cli info

System:
  OS: macOS 26.3
  CPU: (10) arm64 Apple M1 Pro
  Memory: 181.05 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.23.3
    path: /Users/davidliu/.nvm/versions/node/v22.23.3/bin/node
  Yarn:
    version: 1.22.22
    path: /Users/davidliu/.nvm/versions/node/v22.23.3/bin/yarn
  npm:
    version: 10.9.9
    path: /Users/davidliu/.nvm/versions/node/v22.23.3/bin/npm
  Watchman:
    version: 2026.03.30.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/davidliu/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 25.5
      - iOS 26.5
      - macOS 26.5
      - tvOS 26.5
      - visionOS 26.5
      - watchOS 26.5
  Android SDK:
    API Levels:
      - "24"
      - "27"
      - "28"
      - "29"
      - "30"
      - "31"
      - "33"
      - "34"
      - "35"
      - "36"
      - "37"
    Build Tools:
      - 28.0.3
      - 29.0.0
      - 29.0.2
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 32.0.0
      - 33.0.0
      - 33.0.1
      - 33.0.2
      - 34.0.0
      - 35.0.0
      - 35.0.0
      - 35.0.0
      - 36.0.0
      - 37.0.0
    System Images:
      - android-36 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2025.2 AI-252.27397.103.2522.14514259
  Xcode:
    version: 26.6/17F113
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.14
    path: /opt/homebrew/opt/sdkman-cli/libexec/candidates/java/current/bin/javac
  Ruby:
    version: 3.3.5
    path: /Users/davidliu/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 19.3.0
    wanted: 19.3.0
  react-native:
    installed: 0.88.0
    wanted: 0.88.0
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Stacktrace or Logs

Expected:

close() called, readyState=2
onclose code=...


Actual:

close() called, readyState=2
message after close(): "Request served by ...", readyState=2
message after close(): "hello", readyState=2
onclose code=...

MANDATORY Reproducer

#59287

Screenshots and Videos

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions