Repository navigation
fix: cancel Android WebSockets while connecting #59862
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
Merged
meta-codesync
merged 6 commits into
react:main
from
sidhujag:codex/fix-connecting-websocket-close
Dec 2, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d42b68
fix: cancel Android WebSockets while connecting
sidhujag d7bfb21
Preserve WebSocket close ownership through late sends and open events
sidhujag 369de8f
Acknowledge a peer close when no status code was sent
sidhujag 04432e2
Keep WebSocket lifecycle test callbacks void
sidhujag cdda610
Report rejected Android WebSocket connections to JavaScript
sidhujag e0c8bed
Use the project-pinned Mockito assertion in the WebSocket module test
sidhujag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...e/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketConnections.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.modules.websocket | ||
|
|
||
| import okhttp3.WebSocket | ||
|
|
||
| /** A slot remains owned until the transport reports its terminal callback. */ | ||
| internal class WebSocketConnections { | ||
| internal class Connection(val id: Int) { | ||
| var socket: WebSocket? = null | ||
| var opened = false | ||
| var closing = false | ||
| var finished = false | ||
| } | ||
|
|
||
| private val connections = mutableMapOf<Int, Connection>() | ||
| private var retired = false | ||
|
|
||
| @Synchronized | ||
| fun create(id: Int): Connection { | ||
| check(!retired) { "WebSocket module has been invalidated" } | ||
| check(!connections.containsKey(id)) { "WebSocket ID $id is already registered" } | ||
| return Connection(id).also { connections[id] = it } | ||
| } | ||
|
|
||
| fun attach(connection: Connection, socket: WebSocket) { | ||
| val cancel = | ||
| synchronized(this) { | ||
| if ( | ||
| connections[connection.id] !== connection || | ||
| connection.finished || | ||
| (connection.closing && (!connection.opened || connection.socket !== socket)) | ||
| ) { | ||
| true | ||
| } else { | ||
| check(connection.socket == null || connection.socket === socket) | ||
| connection.socket = socket | ||
| false | ||
| } | ||
| } | ||
| if (cancel) socket.cancel() | ||
| } | ||
|
|
||
| fun opened(connection: Connection, socket: WebSocket): Boolean { | ||
| val accepted = | ||
| synchronized(this) { | ||
| if ( | ||
| connections[connection.id] !== connection || | ||
| connection.finished || | ||
| connection.closing || | ||
| (connection.socket != null && connection.socket !== socket) | ||
| ) { | ||
| false | ||
| } else { | ||
| connection.socket = socket | ||
| connection.opened = true | ||
| true | ||
| } | ||
| } | ||
| if (!accepted) socket.cancel() | ||
| return accepted | ||
| } | ||
|
|
||
| @Synchronized | ||
| fun readable(connection: Connection, socket: WebSocket): Boolean = | ||
| connections[connection.id] === connection && | ||
| connection.socket === socket && | ||
| connection.opened && | ||
| !connection.closing && | ||
| !connection.finished | ||
|
|
||
| fun peerClosing(connection: Connection, socket: WebSocket, code: Int, reason: String) { | ||
| synchronized(this) { | ||
| if (!readable(connection, socket)) return | ||
| connection.closing = true | ||
| } | ||
| // OkHttp reports 1005 for an empty peer Close frame. It is reserved on the wire. | ||
| socket.close(if (code == 1005) 1000 else code, reason) | ||
| } | ||
|
|
||
| fun socketForSend(id: Int, onMissing: (Int) -> Unit): WebSocket? { | ||
| val pendingSocket = | ||
| synchronized(this) { | ||
| val connection = connections[id] ?: return@synchronized null | ||
| if (connection.closing) return null | ||
| if (connection.opened) return connection.socket | ||
|
|
||
| // The caller reports the error. Retire the slot before cancelling its transport. | ||
| connections.remove(id) | ||
| connection.finished = true | ||
| connection.socket | ||
| } | ||
| pendingSocket?.cancel() | ||
| onMissing(id) | ||
| return null | ||
| } | ||
|
|
||
| fun close(id: Int, code: Int, reason: String) { | ||
| val action = | ||
| synchronized(this) { | ||
| val connection = connections[id] ?: return | ||
| if (connection.closing || connection.finished) return | ||
| connection.closing = true | ||
| Pair(connection.socket, connection.opened) | ||
| } | ||
| val socket = action.first ?: return // attach() cancels a handle that arrives later. | ||
| if (!action.second) { | ||
| socket.cancel() | ||
| return | ||
| } | ||
| try { | ||
| socket.close(code, reason) | ||
| } catch (error: IllegalArgumentException) { | ||
| // OkHttp rejected the close frame. Cancellation still delivers a terminal callback. | ||
| socket.cancel() | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| @Synchronized | ||
| fun finish(connection: Connection, socket: WebSocket?): Boolean { | ||
| if ( | ||
| connections[connection.id] !== connection || | ||
| connection.finished || | ||
| (connection.socket != null && socket != null && connection.socket !== socket) | ||
| ) | ||
| return false | ||
| connection.finished = true | ||
| connections.remove(connection.id) | ||
| return true | ||
| } | ||
|
|
||
| fun invalidate() { | ||
| val sockets = | ||
| synchronized(this) { | ||
| retired = true | ||
| connections.values | ||
| .mapNotNull { connection -> | ||
| connection.finished = true | ||
| connection.closing = true | ||
| connection.socket?.let { Pair(it, connection.opened) } | ||
| } | ||
| .also { connections.clear() } | ||
| } | ||
| sockets.forEach { (socket, opened) -> | ||
| if (opened) socket.close(1001, null) else socket.cancel() | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method can throw when the registry is invalidated or the ID is already registered. It currently runs outside
connect()'s try/catch, so the exception escapes the native method without awebsocketFailedevent.Please handle a rejected connection creation + add a regression test for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed both review points in e0c8bed. Rejected creation now emits
websocketFailedbefore starting a transport or replacing the existing inspector entry. The new module test reproduced the uncaught exception on the previous head and passes on this one. The registry tests also pass with OkHttp 3.14.9 and 4.9.2. @cortinico, could you re-import this revision for review?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup will do 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cortinico resolve if fixed