Fix race condition between app and browser on npm command#1616
Fix race condition between app and browser on npm command#1616prathmeshpatel merged 3 commits intomainfrom
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughThe pull request introduces server readiness polling to the startup sequence in 📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
chelojimenez
left a comment
There was a problem hiding this comment.
Review: Fix race condition between app and browser on npm command
Approve — this correctly fixes the root cause. The hardcoded 2-second delay was insufficient on slower machines, causing the sandboxed iframe to fail with chrome-error://chromewebdata/ before the server was ready.
What's good
- TCP-level check via
net.createConnectionis the right approach — lightweight and doesn't depend on HTTP routes - Proper cleanup with
settledflag to prevent double-resolution - Graceful degradation (warns user to visit URL manually if server doesn't start in 30s)
- Re-checks
cancelledafter awaiting readiness
Minor suggestions
-
Description says "exponential backoff" but it's a fixed 200ms interval. This is actually better for this use case (open browser ASAP), just a description nitpick.
-
Host mismatch on IPv6 systems: The server binds to
127.0.0.1(server/index.ts:27), but the readiness check useshostwhich could belocalhostin dev mode. On IPv6-preferring systems,localhostmay resolve to::1, causing the TCP check to fail even though the server is running on127.0.0.1. Consider always polling127.0.0.1for the readiness check regardless of the display URL. -
Unrelated formatting changes in the test files — not a blocker, but ideally a separate commit.
None of these are blocking. #2 is worth considering to avoid a different "works for some but not others" scenario.
|

Summary
Replaced the 2-second delay with a proper connection check that waits for the server to actually accept connections before attempting to open the browser. Added
waitForServerReady()function that polls the server port with exponential backoff and a 30-second timeout to ensure the application is fully ready before launching the browserOn

npx @mcpjam/inspector@latest: