Repository navigation
fix(metro): reload the app's own dev server instead of assuming port 8081 - #5098
Conversation
…8081 A flagless metro reload resolved localhost:8081 whenever the session had bound no dev server, which reloaded whichever project owned that port, or failed as a bare fetch error. It now resolves in order: the session or explicit binding, the address the app itself stored (RCT_jsLocation in an iOS simulator's preferences), then a local dev server whose X-React-Native-Project-Root matches this directory. When nothing resolves, it fails with details.reason metro_dev_server_unresolved naming the address the fallback would have used. Only a session whose app runs on a local simulator or emulator runs the lookups; a remote or physical device keeps the previous default. Sessions that bind no address now record the app they opened so the reload has something to look that app's address up from.
Size Report
Startup median (7 runs, lower is better):
|
There was a problem hiding this comment.
All reported issues were addressed across 12 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on 21ef0b3. The verdict and the full list are in the summary comment.
|
At 21ef0b3, flagless A provider-hosted iOS simulator or Android emulator driven by a local daemon still reports kind The reload route changed, but the live run in the description uses only one dev server on 8096. The rule that another project's server is never reloaded is covered only by unit tests with injected probes. Before merge, a live transcript is required with project B's Metro on 8081, the session app's project A on 8096, and a session opened without Metro flags. On an iOS simulator with no stored Not blocking: these can be taken or left: use one loopback predicate because the local list and Could the reload keep These open threads still apply: stale loopback address and reused session app, which both break the rule above and need a fix, plus port candidate limit, empty Metro port, loose port parsing, missing lsof fallback, Windows path separators and docs failure claim. Two lower-priority threads also still apply. This thread does not apply and can be resolved: the local client path always passes the host, and the route without a host is the deliberate remote-daemon exception in the description. The Coverage check fails, likely because of this change: no test exercises the new branches in Not verified here: the tests and the live route were not run, and the live run in the description has no transcript. A full provider-hosted open was not traced end to end to confirm that its response carries that device kind and the app id. It was also not checked whether every remote configuration sets a daemon URL before the client is created; if neither the option nor the environment variable is set, the route would be treated as local. Before merge, the provider-hosted case, the two threads that break the rule, and the Coverage failure need fixes, and the live two-server run is required. |
…ed server Splits the pure reads out of dev-server discovery — lsof and netstat port tables, the app's stored preferences, the project-root header, host:port, and root overlap — so each branch is tested without a process or a socket, and the I/O wrappers stay thin. Gates local lookups on the app being recorded as hosted here, refuses an app-reported server whose root names another project, batches port probes instead of dropping ports past the 32nd, falls back to netstat where lsof is missing, treats a blank metroPort as no binding, and replaces the recorded app on every open. Co-authored-by: Apex <noreply@callstack.com>
… host reads Records where a session's device runs when open resolves it, instead of second-guessing the device kind: the trait is true when the daemon answering is local and it named the device from its own inventory, false for a provider device, and left out when the answer says nothing, so a later open keeps what an earlier one proved for the same device. A physical device on this machine is now in scope and a provider device on a simulator is not, which is what the host actually is. Shares one loopback test with the daemon's own and drops the half-baked URL check that read 127.1.2.3 as remote. A live Mac answers lsof in over 1.5s with simulators running — measured, not guessed — so the port listing went empty on a real reload. The host-read budget now covers the slow machine, because a timeout there is indistinguishable from "this machine listens on nothing". Co-authored-by: Apex <noreply@callstack.com>
…corder Un-exports the four parsers no caller outside the module reads — the status literal, the netstat address port, host:port, and the JSON-object read — which the tests already cover through the parsers that call them, and splits what `open` records into the binding path and the app path, so neither branch carries the other's conditions. Renames the two unit tests that were named for no module beside them: the reload walkthrough is a package-entry test, the host reads belong to the module whose I/O wrappers they pin. Co-authored-by: Apex <noreply@callstack.com>
Live two-server check:
|
| Port | X-React-Native-Project-Root |
Log |
|---|---|---|
| 8083 | /tmp/metro-fake/projA |
a.log |
| 8082 | /tmp/metro-fake/projB |
b.log |
CLI built from this branch (0.21.43-dev), its own AGENT_DEVICE_STATE_DIR, and no --metro-host/--metro-port/--bundle-url on any reload call — the working directory is the only thing that changes between calls.
iOS Simulator — iPhone Air, iOS 27.0 (1B89356B-961C-4993-B0BE-4C0F3917D995)
$ cd /tmp/metro-fake/projA && agent-device open com.callstack.agentdevicelab --platform ios --udid 1B89…D995 --session twoserver
Opened: com.callstack.agentdevicelab
Session state: /tmp/metro-fake/state/sessions/twoserver
$ cat /tmp/metro-fake/state/metro-sessions/twoserver.json # what open recorded
{
"app": {
"platform": "ios",
"deviceId": "1B89356B-961C-4993-B0BE-4C0F3917D995",
"deviceKind": "simulator",
"appId": "com.callstack.agentdevicelab",
"hostedLocally": true
}
}
$ cd /tmp/metro-fake/projA && agent-device --platform ios --udid 1B89…D995 --session twoserver metro reload
Reloaded React Native apps via http://127.0.0.1:8083/reload
-> a.log: RELOAD project=/tmp/metro-fake/projA at=2026-11-25T10:52:17.202Z
-> b.log: (nothing)
$ cd /tmp/metro-fake/projB && agent-device --platform ios --udid 1B89…D995 --session twoserver metro reload
Reloaded React Native apps via http://127.0.0.1:8082/reload
-> a.log: (unchanged)
-> b.log: RELOAD project=/tmp/metro-fake/projB at=2026-11-25T10:52:20.611Z
Android emulator — focus-order-probe-api37, Android 17.0 (emulator-5560, started for this check)
Same two servers, same two directories, same flagless calls. Local discovery works on Android, so this platform is not a docs exception:
$ cd /tmp/metro-fake/projA && agent-device open com.callstack.agentdevicelab --platform android --serial emulator-5560 --session twoserver-android
Opened: com.callstack.agentdevicelab
$ cat …/metro-sessions/twoserver-android.json
{ "app": { "platform": "android", "deviceId": "emulator-5560", "deviceKind": "emulator",
"appId": "com.callstack.agentdevicelab", "hostedLocally": true } }
$ cd /tmp/metro-fake/projA && agent-device --platform android --serial emulator-5560 --session twoserver-android metro reload
Reloaded React Native apps via http://127.0.0.1:8083/reload
-> a.log: RELOAD project=/tmp/metro-fake/projA at=2026-11-25T10:52:23.789Z
$ cd /tmp/metro-fake/projB && agent-device --platform android --serial emulator-5560 --session twoserver-android metro reload
Reloaded React Native apps via http://127.0.0.1:8082/reload
-> b.log: RELOAD project=/tmp/metro-fake/projB at=2026-11-25T10:52:26.601Z
What is iOS-only is the app-reported address step (RCT_jsLocation read from the app's preferences file): an Android app's stored address is not readable this way, so Android goes straight to the port sweep. CLI help and website/docs/docs/commands.md name an iOS simulator for that step only, and put no platform limit on the port sweep.
Both logs, after all four calls
$ cat a.log # port 8083, project A
LISTENING 8083 for /tmp/metro-fake/projA
RELOAD project=/tmp/metro-fake/projA at=2026-11-25T10:52:17.202Z # iOS, from projA
RELOAD project=/tmp/metro-fake/projA at=2026-11-25T10:52:23.789Z # android, from projA
$ cat b.log # port 8082, project B
LISTENING 8082 for /tmp/metro-fake/projB
RELOAD project=/tmp/metro-fake/projB at=2026-11-25T10:52:20.611Z # iOS, from projB
RELOAD project=/tmp/metro-fake/projB at=2026-11-25T10:52:26.601Z # android, from projB
Each server was called exactly once per platform, by the project it serves. No call reached localhost:8081, and no call reloaded a server reporting an unrelated project root.
The two guards, observed on the same pair of servers
A project no server serves fails typed instead of calling the default port. From a third project directory, with both servers running and the session's app recorded as local:
$ cd /tmp/metro-fake/projC && agent-device --platform ios --udid 1B89…D995 --session twoserver metro reload
Error (COMMAND_FAILED): React Native dev server is not bound to this session and none was found
for its app, so nothing was reloaded. The address the fallback would have used is
http://localhost:8081. Next: Pass --metro-port <port> …
Neither log gained a RELOAD line.
An app-reported address is refused when its server serves another project. RCT_jsLocation in the app's container was set to localhost:8082 — projB's live server — while reload ran from projA:
$ plutil -replace RCT_jsLocation -string "localhost:8082" …/com.callstack.agentdevicelab.plist
$ cd /tmp/metro-fake/projA && agent-device --platform ios --udid 1B89…D995 --session twoserver metro reload
Reloaded React Native apps via http://127.0.0.1:8083/reload # projA's server, by port sweep
-> b.log: (no new line — the app's own address was probed and refused, not reloaded)
$ cd /tmp/metro-fake/projB && agent-device --platform ios --udid 1B89…D995 --session twoserver metro reload
Reloaded React Native apps via http://localhost:8082/reload # same address, now accepted
-> b.log: RELOAD project=/tmp/metro-fake/projB at=2026-11-25T10:52:49.246Z
One address, two working directories, two verdicts — the project root the server reports decides it, not the address itself. (The simulator's preference was removed again afterwards.)
One thing the unit tests could not tell me
The first two live runs failed with metro_dev_server_unresolved while the right server was listening. lsof -nP -iTCP -sTCP:LISTEN -Fn needs over 1.5 s on this Mac with simulators running (/usr/bin/time: 1.54 s, 1.26 s, 1.17 s), and the port-listing budget was exactly 1.5 s — so the host answered "this machine listens on nothing". Every transcript line above exists only after HOST_READ_TIMEOUT_MS went to 4 s in 07a0b60. A timeout on that read is indistinguishable from an empty port list, so no port-table test could have caught it: the mock always answers instantly.
The stand-in server used for this check
// answers /status like the RN CLI's statusPageMiddleware; logs every /reload
import { createServer } from 'node:http';
import fs from 'node:fs';
const [port, projectRoot, logPath] = process.argv.slice(2);
createServer((req, res) => {
const url = new URL(req.url, `http://127.0.0.1:${port}`);
if (url.pathname === '/status') {
res.setHeader('X-React-Native-Project-Root', projectRoot);
res.end('packager-status:running');
return;
}
if (url.pathname === '/reload') {
fs.appendFileSync(logPath, `RELOAD project=${projectRoot} at=${new Date().toISOString()}\n`);
res.end(`RELOADED ${projectRoot}`);
return;
}
res.statusCode = 404; res.end('not found');
}).listen(Number(port), '127.0.0.1');
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 4 new findings on 38300d4. The verdict and the full list are in the summary comment.
…host named A flagless fresh open no longer inherits a binding a previous run left under the same session name: that address belongs to the project that bound it. A reused session keeps its address, including one stored as runtime hints before the control address existed, so rewriting the record never drops a binding. The app-reported address is now confirmed wherever it points, so a LAN host left by `open --metro-host` is refused when its server serves another project. Listening sockets keep the address the host reported, so a dev server bound to IPv6 loopback only is dialled at `[::1]` instead of being missed on 127.0.0.1.
…rlier An open whose device selection says nothing about where the device runs must leave the trait an earlier open proved for that device in place, and must not carry it over to a different device.
…owns it The four reload cases went through the package entry, so nothing said which module pinned them. They call reloadMetro directly now, beside client-metro.ts, which holds the host branch that decides whether local discovery runs at all.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on c685434. The verdict and the full list are in the summary comment.
A scoped address like fe80::1%en0 is not dialable by new URL; one such listener threw a raw TypeError that aborted the whole sweep. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
|
This PR is ready at 3b46e85: the blocking points from the earlier review at 21ef0b3 are fixed, and nothing blocking remains. Not blocking: these can be taken or left: add client tests that run These open threads no longer apply and can be resolved: app-reported address check (fixed: the probe and root overlap check use that address); app record rewrite (fixed: every open rewrites the app); socket probe cap (fixed: all sockets are probed in batches of 32); blank Metro port (fixed: it counts as absent); SDK reload default (intended and now documented: without a host the pre-discovery default is kept, and the CLI passes a host for a local daemon); port value parsing (fixed: only digits are accepted); socket listing fallback (fixed: CI is green: all 21 checks pass, including Coverage, which failed at 21ef0b3. Not verified here: tests and the live route were not run, so CI and the author's live transcript are the evidence. Not verified here: real Metro on 8081 and 8096 at the reviewed commit, because that transcript used stand-in dev servers on 8082 and 8083 at 7351ef4 and ffc3f90, with only parser changes after it. Not verified here: a provider-hosted open was not run live, and its Nothing else needs to happen before merge; both notes above can be follow-ups. |
Nothing covered the device-selection mapping through open, and it decides whether a later flagless reload may read this machine's simulator containers and listening ports at all.
Apex Sentinel · ✅ Approve · review of
|
| Command | What it does |
|---|---|
@apex-sentinel-staging note <context> |
Adds context; Sentinel re-checks the cited code |
@apex-sentinel-staging pivot <why> |
Says the finding is wrong; Sentinel re-checks the cited code |
@apex-sentinel-staging like |
Marks the finding as useful |
@apex-sentinel-staging dislike <why> |
Marks it as not useful, so it is not raised again |
On this conversation, comment @apex-sentinel-staging review to run a full review once again.
Summary
A flagless
metro reloadresolvedlocalhost:8081whenever the session had bound no dev server. That port is another project's, so reload acted on the wrong app or failed asfetch failed.Reload now resolves in order: the binding (
--metro-host/--metro-port/--bundle-url, or whatmetro prepareoropenbound); what the app reports (RCT_jsLocation, iOS simulator only, confirmed by one bounded/statusread); then a sweep of ports a local process listens on, for a dev server whoseX-React-Native-Project-Rootis this directory or a parent or child of it. A server naming another project is never used. With no signal it fails typed:metro_dev_server_unresolved.Both lookups are gated on one trait
openrecords: the daemon answers on loopback and named the device from its own inventory. Any device attached to this machine, a physical device included, gets the port sweep; only the preferences read is iOS-simulator only. A provider or inherited device, or an app-less session, keeps the old default.Before / after
Metro on 8096, no flags:
fetch failed→reloadUrl: http://127.0.0.1:8096/reload.No server for this project:
localhost:8081→ typed failure.Validation
Head
c7290dc596; CI owns the gate. Live two-server transcript: stand-in servers on 8083/projA and 8082/projB; flagless reload from each directory reloaded only its own, on iPhone Air and an Android emulator, server logs as evidence. Also live: the typed failure from an unserved project; an app-reported address refused from one project and accepted from the other, loopback and LAN; a[::1]-only server.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.