Repository navigation
Conversation
|
Hi @sidhujag! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
@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. |
Summary
Android stores a WebSocket handle when
onOpenfires. 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.0parser and formatting check passed on all three changed Kotlin files. Prettier 3.10.0 passed on both JavaScript files.git diff --checkpassed.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.