Skip to content

Revert "Fix stale packager connections on Android" - #60318

Merged
cipolleschi merged 1 commit into
mainfrom
cipolleschi/revert-stale-packager-connections
Nov 26, 2026
Merged

cipolleschi merged 1 commit into
mainfrom
cipolleschi/revert-stale-packager-connections

Conversation

@cipolleschi

Copy link
Copy Markdown
Contributor

Summary

Reverts 99a157bbfd4accecc72c42b79ecf0eeb25452487, merged in #58971 ("Fix stale packager connections on Android").

The original change addresses a real bug when switching between Metro servers, but it introduced failures in downstream Android builds:

  • The new host validator imports okhttp3.HttpUrl.Companion.toHttpUrlOrNull, an API available in OkHttp 4 but absent from OkHttp 3.14.9. Consumers whose dependency graph selects OkHttp 3.14.9 fail to compile React Native. The regression suites use OkHttp 4.10.0, so passing those tests did not cover this compatibility requirement.
  • The added tests require build-target dependencies and Kotlin friend access for the inspector helper, plus the DeveloperSettings interface dependency. Those were missing from the downstream test configuration. The deliberate loopback/emulator IP fixtures also trigger HardcodedIPAddressUse lint without targeted suppressions. These failures block importing the change and cause cascading build/E2E failures.

A compatibility follow-up was prepared in #60317, but the agreed approach is to revert the full change to restore build compatibility, then revisit it with the source compatibility and test/build integration fixes validated together. This revert supersedes that follow-up.

This restores the packager/WebSocket lifecycle and bundle-location behavior from before #58971. The original stale-connection bug can therefore recur until a revised fix lands; that fix should be validated with both OkHttp 3 and 4 consumers and the complete downstream build/test configuration.

Changelog

[ANDROID] [FIXED] - Revert the stale packager connection fix to restore Android build compatibility with OkHttp 3

Test Plan

  • git revert --no-commit 99a157bbfd4accecc72c42b79ecf0eeb25452487 applied cleanly to upstream main at 1f43abd0247a82284b31969312588035d4407e9d.
  • Verified that all eight affected files exactly match the reverted commit's parent, including removal of the three tests introduced by that commit. No other files changed.
  • git diff --cached --check passed.
  • Pinned ktfmt 0.59.0 passed for the four restored Kotlin source files using --do-not-remove-unused-imports --dry-run --set-exit-if-changed.
  • Parsed the restored Android string resource XML successfully.

Full Android builds, unit tests, and device UI testing were not run locally for this revert. The earlier local Gradle attempt for #60317 stopped before tests when codegen dependency installation hit a registry proxy HTTP 503. Fresh GitHub CI is required for the revert.

This reverts commit 99a157b.

Restore downstream Android build compatibility with OkHttp 3 while source compatibility and test/build integration fixes are validated together.
@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 26, 2026
@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 26, 2026
@cipolleschi

Copy link
Copy Markdown
Contributor Author

@vonovak we had to revert 99a157bbfd4accecc72c42b79ecf0eeb25452487, merged in #58971. See the reason in the summary

@cipolleschi
cipolleschi enabled auto-merge (squash) November 26, 2026 14:30
@cipolleschi
cipolleschi merged commit f0c7848 into main Nov 26, 2026
62 of 63 checks passed
@cipolleschi
cipolleschi deleted the cipolleschi/revert-stale-packager-connections branch November 26, 2026 14:49
@vonovak

vonovak commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

@cipolleschi thanks for the info

the agreed approach is to revert the full change to restore build compatibility, then revisit it with the source compatibility and test/build integration fixes validated together.

do I need to do anything to bring the change into the core or will someone take this over in meta?
Thank you!

@cortinico

Copy link
Copy Markdown
Contributor

@cipolleschi thanks for the info

the agreed approach is to revert the full change to restore build compatibility, then revisit it with the source compatibility and test/build integration fixes validated together.

do I need to do anything to bring the change into the core or will someone take this over in meta? Thank you!

@vonovak Could you resubmit the PR and ask your agent to make sure the code is source compatible with OkHTTP 3.x? We've added instructions in the AGENTS.md also about this

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
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. p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants