Repository navigation
Fix stale packager connections on Android - #58971
Merged
Merged
Conversation
vonovak
marked this pull request as ready for review
October 12, 2026 21:38
huntie
self-requested a review
October 29, 2026 11:38
DevServerHelper opened and closed the packager connection in separate AsyncTasks on THREAD_POOL_EXECUTOR. A close could run before a pending open, and repeated opens could create two clients. Changing the bundle location from the dev menu also kept the old connection. In both cases, the old dev server could still reload the app. - Run openPackagerConnection and closePackagerConnection as tasks on one single-threaded executor. Tasks run in call order, off the UI thread, and only that thread uses packagerClient. The executor names its thread ReactPackagerConnection and stops it after 30 seconds without work. - Remember the host of the open client. If an open finds a client for a different host, close it and connect to the new host. - Check clientId on the caller's thread. A failed init() still crashes the app, as before; packagerClient stays null, so a later open can retry. - Change Bundle Location: close the packager connection before the reload, as setBundleSource does. Trim the input, then reject a host that OkHttp cannot parse in the connection URL and show a toast. Before, an invalid host was saved without a check. - ReconnectingWebSocket: change close state under the socket's monitor, so no message handler starts after closeQuietly() returns. Close a socket whose handshake finishes after closeQuietly() instead of reporting it as connected. - Add DevServerHelperTest, ReconnectingWebSocketTest and DevSupportHttpClientTest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
vonovak
force-pushed
the
codex/stale-packager-connection-test
branch
from
November 25, 2026 17:38
df64993 to
06951a0
Compare
This was referenced Nov 26, 2026
vonovak
added a commit
to vonovak/react-native
that referenced
this pull request
Nov 30, 2026
DevServerHelper opened and closed the packager connection in separate AsyncTasks on THREAD_POOL_EXECUTOR. A close could run before a pending open, and repeated opens could create two clients. Changing the bundle location from the dev menu also kept the old connection. In both cases, the old dev server could still reload the app. - Run openPackagerConnection and closePackagerConnection as tasks on one single-threaded executor. Tasks run in call order, off the UI thread, and only that thread uses packagerClient. The executor names its thread ReactPackagerConnection and stops it after 30 seconds without work. - Remember the host of the open client. If an open finds a client for a different host, close it and connect to the new host. - Check clientId on the caller's thread. A failed init() still crashes the app, as before; packagerClient stays null, so a later open can retry. - Change Bundle Location: close the packager connection before the reload, as setBundleSource does. Trim the input, then reject a host that OkHttp cannot parse in the connection URL and show a toast. Before, an invalid host was saved without a check. - ReconnectingWebSocket: change close state under the socket's monitor, so no message handler starts after closeQuietly() returns. Close a socket whose handshake finishes after closeQuietly() instead of reporting it as connected. - Add DevServerHelperTest and ReconnectingWebSocketTest. This re-lands react#58971, which was reverted in react#60318: - Validate the host with Request.Builder.url(String), which OkHttp 3 and 4 both have. HttpUrl.Companion.toHttpUrlOrNull exists only in OkHttp 4. - Use localhost in test fixtures, so the tests have no hardcoded IP addresses. - Drop DevSupportHttpClientTest. It mostly tested OkHttp's URL parser and needed a separate downstream test target. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
meta-codesync Bot
pushed a commit
that referenced
this pull request
Dec 2, 2026
## Summary: Re-lands #58971 (stale packager connections on Android), which #60318 reverted. Same connection fix, plus: - `isValidHost` uses `Request.Builder.url(String)`, which OkHttp 3 and 4 both have. `toHttpUrlOrNull` exists only in OkHttp 4. - Tests use `localhost`, so they have no hardcoded IP addresses. For the importer: `DevServerHelperTest` needs a dependency on the `DeveloperSettings` interface. ## Changelog: [ANDROID] [FIXED] - Close stale packager connections when the dev server changes ## Test Plan: - CI - on-device Differential Revision: D135356300 Pull Request resolved: #60403
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
On Android, with two Metro servers running, an app that switches from one to the other (via Change Bundle Location,
setBundleSource, or a dev client) can keep its packager connection to the first. Pressingrin the first Metro's terminal then still reloads the app. I hit this while working in several worktrees at once, with several emulators, killing and restarting Metro servers. There it depends on timing. Change Bundle Location in the dev menu should reproduce it every time, because it never closed the old connection.DevServerHelperran open and close in separateAsyncTasks, so a close could overtake an open, and repeated opens could leak a client. Change Bundle Location never closed the old connection.ReconnectingWebSocketalso kept a socket whose handshake finished aftercloseQuietly().Changes:
ReconnectingWebSocketcloses a socket that opens aftercloseQuietly().Notes: open and close are now asynchronous, and
customCommandHandlers()handlers must not block.pr-58971-before-after.mp4
Follow-up: #59012 builds on this PR to fix the inspector connection staying attached to the previous Metro server after a host change.
Changelog:
[ANDROID] [FIXED] - Fix the app staying connected to the previous Metro server after switching to another one
Test Plan:
New tests in
DevServerHelperTest,ReconnectingWebSocketTestandDevSupportHttpClientTest, plus the existingJSPackagerClientTest, all pass.