Skip to content

fix: cancel Android WebSockets while connecting - #59862

Open
sidhujag wants to merge 4 commits into
react:mainfrom
sidhujag:codex/fix-connecting-websocket-close
Open

sidhujag wants to merge 4 commits into
react:mainfrom
sidhujag:codex/fix-connecting-websocket-close

Conversation

@sidhujag

@sidhujag sidhujag commented Nov 8, 2026 •

Copy link
Copy Markdown

Summary

Android stores a WebSocket handle when onOpen fires. Closing a socket during its HTTP upgrade therefore leaves the pending OkHttp request running.

Keep a connection record before creating the request, retain its handle through the connecting state, and cancel the underlying call when it closes before upgrade. Match late callbacks to their connection record and publish each terminal callback once. Established sockets retain the close handshake during module invalidation or a late send. If OkHttp rejects the close frame, the underlying socket is cancelled so its terminal callback can complete. The shared JavaScript listener also ignores an already queued open event after close() has moved the socket to CLOSING.

For a peer-initiated empty Close frame, OkHttp reports the reserved local status 1005. The Android module replies with 1000 on the wire and preserves the peer’s 1005 in the terminal event. Valid peer codes and reasons pass through.

Changelog

[ANDROID] [FIXED] - Cancel pending WebSocket upgrades when closed and acknowledge peer Close frames with no status code.

Test Plan

The repository-pinned ktfmt@0.59.0 parser and formatting check passed on all three changed Kotlin files. Prettier 3.10.0 passed on both JavaScript files. git diff --check passed.

"$JAVA" -jar "$KTFMT_JAR" --do-not-remove-unused-imports --dry-run --set-exit-if-changed \
  packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.kt \
  packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketConnections.kt \
  packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/websocket/WebSocketConnectionsTest.kt

Fourteen JUnit regressions passed in an isolated JVM compilation using Kotlin 2.4.21, OkHttp 4.9.2, Okio 2.9.0 and JUnit 4.13.2. They cover close-before-attach, cancellation while connecting, normal connected close, open-close-attach ordering, late callbacks after identifier reuse, module invalidation, competing terminal callbacks and caller-reported failures before or after handle attachment. The additional cases cover a send during a graceful close, a rejected close frame, a send to an unknown socket, and both empty and ordinary peer Close frames.

An OkHttp 4.9.2 MockWebServer loopback accepted the upgrade request, then observed cancellation while its response was held. The same ID connected again, echoed text, and completed the normal 1000 close handshake despite late send and ping calls. A 124-byte local close reason also produced a terminal failure and released the ID. A real empty peer Close produced a 1000 wire reply, one 1005 terminal callback, and no failure. The same test with 1001 and the reason “leaving” retained both values.

Both changed Kotlin production files compiled together against the published React Native 0.89.0-rc.3 Android API and Android SDK 37 at PR head 85be688. The earlier revision also compiled against React Native 0.88.1. The four WebSocket JavaScript tests passed using an isolated Node host adapter. The new test failed against base 7a2d1d2, where a queued open event returned the socket to OPEN. These checks have not run as a full upstream Gradle/Fantom build or on an Android device.

"$JAVA" -cp "$COMPILER_CP" org.jetbrains.kotlin.cli.jvm.K2JVMCompiler \
  -no-stdlib -no-reflect -jvm-target 17 -classpath "$RUNTIME_CP" -d "$CLASSES" \
  packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketConnections.kt \
  packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/websocket/WebSocketConnectionsTest.kt
"$JAVA" -cp "$CLASSES:$RUNTIME_CP" org.junit.runner.JUnitCore \
  com.facebook.react.modules.websocket.WebSocketConnectionsTest

@meta-cla

meta-cla Bot commented Nov 8, 2026

Copy link
Copy Markdown

Hi @sidhujag!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 8, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 8, 2026
@sidhujag

sidhujag commented Nov 27, 2026 •

Copy link
Copy Markdown
Author

@javache @cortinico, could one of you review this Android WebSocket lifecycle fix or point me to the current reviewer?

On Android, closing a socket while the HTTP upgrade is pending leaves the OkHttp request running. This PR cancels that request and keeps the usual close handshake for an established socket.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant