Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: Reloading local server chooses incorrect port #3608

Closed
tvthatsme opened this issue Jul 13, 2023 · 2 comments
Closed

🐛 BUG: Reloading local server chooses incorrect port #3608

tvthatsme opened this issue Jul 13, 2023 · 2 comments
Labels
awaiting reporter response Needs clarification or followup from OP bug Something that isn't working

Comments

@tvthatsme
Copy link

Which Cloudflare product(s) does this pertain to?

Wrangler

What version of Wrangler are you using?

3.2.0

What operating system are you using?

Linux

Describe the Bug

When running wrangler dev in a GitHub Action, we are experiencing the local server reloading randomly. It doesn't always do it, but to date I haven't yet been able to figure out why.

 ⛅️ wrangler 3.2.0
------------------
wrangler dev now uses local mode by default, powered by 🔥 Miniflare and 👷 workerd.
To run an edge preview session for your Worker, use wrangler dev --remote
Your worker has access to the following bindings:
- Vars:
  - redacted: "redacted"
  - ENV: "development"
⎔ Starting local server...
[mf:inf] Ready on http://127.0.0.1:3000/
⎔ Reloading local server...
/runner/_work/xyz/node_modules/wrangler/wrangler-dist/cli.js:30632
            throw a;
            ^
Error: connect ECONNREFUSED 127.0.0.1:[41](https://github.com/xyz/actions/runs/5545002487/jobs/10123297740?pr=11225#step:10:42)[45](https://github.com/xyz/actions/runs/5545002487/jobs/10123297740?pr=11225#step:10:46)9
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)
Emitted 'error' event on WebSocket instance at:
    at emitErrorAndClose (/runner/_work/xyz/node_modules/wrangler/wrangler-dist/cli.js:108968:17)
    at ClientRequest.<anonymous> (/runner/_work/xyz/node_modules/wrangler/wrangler-dist/cli.js:108864:9)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:[49](https://github.com/xyz/actions/runs/5545002487/jobs/10123297740?pr=11225#step:10:50)4:9)
    at Socket.emit (node:events:[51](https://github.com/xyz/actions/runs/5545002487/jobs/10123297740?pr=11225#step:10:52)3:28)
    at emitErrorNT (node:internal/streams/destroy:1[57](https://github.com/xyz/actions/runs/5545002487/jobs/10123297740?pr=11225#step:10:58):8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 41459
}

After reloading the local server, wrangler tries to reconnect but uses a random port. In the example ☝️ case, it chooses port 41459 instead of the port 3000 that was configured and first connected to.

I know that running dev in a CI environment might be strange, but most of the times this is working for the particular testing that we are doing.

To get around this issue, I wanted to turn off the "watch" feature of development mode. I did a yarn patch like this:

diff --git a/wrangler-dist/cli.js b/wrangler-dist/cli.js
index 923ce6b66301e94168a07595495fdf47de8c9f86..d61fc4ba64d19eb115fd7bf13f7ef8736e818a1f 100644
--- a/wrangler-dist/cli.js
+++ b/wrangler-dist/cli.js
@@ -127986,6 +127986,9 @@ function useLocalWorker(props) {
         miniflareServerRef.current = void 0;
       });
     } else {
+      if (props.bindings.vars?.['no-refresh']) {
+        return;
+      }
       logger.log(import_chalk4.default.dim("\u2394 Reloading local server..."));
     }
     const currentServer = server2;

And running wrangler like this yarn dev --var no-refresh:true is working perfectly.

This issue consists then of two parts:

  1. Any idea on why the server is restarting as if a watched file has changed.
  2. Is a --turn-off-reloading type of flag something we could add to the list of dev command options?

Thank you!

@tvthatsme tvthatsme added the bug Something that isn't working label Jul 13, 2023
@admah admah added the miniflare Relating to Miniflare label Jul 14, 2023
@mrbbot
Copy link
Contributor

mrbbot commented Oct 9, 2023

Hey! 👋 Apologies for the delayed response here.

  1. Any idea on why the server is restarting as if a watched file has changed.

Are you running multiple of these servers on the same CI machine? It's possible the servers are restarting because of Wrangler's service registry, which attempts to automatically link wrangler dev sessions so you can add service bindings between them. When a new wrangler dev session starts, others will reload.

  1. Is a --turn-off-reloading type of flag something we could add to the list of dev command options?

Probably not. Instead, I'd recommend you look at https://developers.cloudflare.com/workers/wrangler/api/#unstable_dev. It provides a programatic API for wrangler dev, and in the default configuration, doesn't support reloading, so shouldn't exhibit the problems you're seeing.

@mrbbot mrbbot added awaiting reporter response Needs clarification or followup from OP and removed miniflare Relating to Miniflare labels Oct 9, 2023
@lrapoport-cf
Copy link
Contributor

hi @tvthatsme 👋 we haven't heard from you in a while, so we’re going to close this issue for now. if this is still a problem, please feel free to open a new issue referencing this one. please be sure to include an updated summary and any additional requested details, followup, or context and we will revisit and investigate further. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reporter response Needs clarification or followup from OP bug Something that isn't working
Projects
None yet
Development

No branches or pull requests

4 participants