deno: Used port is not freed when using Deno.run with --watch

When running this code

Deno.serve(() => {}, { port: x })

with this command

deno run -A --unstable --watch script.ts

I get this error everytime i make a change and have to kill the script (ctrl + c) and run it again.

Watcher File change detected! Restarting!
error: Uncaught (in promise) AddrInUse: Address already in use (os error 98)
Watcher Process finished. Restarting on file change...

deno 1.26.1 (release, x86_64-unknown-linux-gnu) Linux Fedora 37

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 19 (1 by maintainers)

Commits related to this issue

Most upvoted comments

You can get around this by adding an AbortController (which might be a good idea in general):

const abortController = new AbortController();

serve(myRequestHandler, {
  signal: abortController.signal,
  port: 8080,
});

globalThis.addEventListener("unload", () => abortController.abort());

Yeah this isn’t fixed on 1.31.3 for the built-in Deno.serve.

$ deno --version
deno 1.31.3 (release, aarch64-apple-darwin)
v8 11.0.226.19
typescript 4.9.4

$ deno run --unstable --watch --allow-net mod.ts
Watcher Process started.
Listening on http://127.0.0.1:9000/
Watcher File change detected! Restarting!
error: Uncaught (in promise) AddrInUse: Address already in use (os error 48)
// mod.ts
Deno.serve(() => new Response("hi"));

Could this issue be re-opened? The problem is still present for me with deno 1.30.3 (release, aarch64-apple-darwin). It looks like there was some back-and-forth with reverted PRs—did a fix ever get merged and released?

This problem is happening in 1.31.2 as well in Windows computers (only arch we were able to reproduce)

Seeing this on 1.37.0 using Deno.serve.

It happens intermittently which perhaps suggests a race condition.

Issue seems to be fixed at deno 1.31.3 without needing the above workaround