deno: Uncaught AddrInUse when restarting server in watch mode after saving file

Source: https://stackoverflow.com/questions/74444443/deno-watcher-uncaught-addrinuse-only-one-usage-of-each-socket-address

Reproduction:

  • Run the code below with deno run -A --watch server.ts:
import express, { Express, Request, Response } from "npm:express@4.18";
import { v4 as uuidv4 } from 'npm:uuid@9.0.0';
const app: Express = express();

app.use("/", (_req: Request, res: Response) => {
    res.send(`uuid: ${uuidv4()}.`);
})

app.listen(8000, () => {
    console.log("App Running: http://localhost:8000");
});
  • Make a change and save the file (either with auto-save or manually)
  • When the file watcher restarts the error is thrown
Watcher File change detected! Restarting!
error: Uncaught AddrInUse: Only one usage of each socket address (protocol/network address/port) is normally permitted. (os error 10048)
Watcher Process finished. Restarting on file change...

I found that this error can happen when there is another process listening on the same port: https://stackoverflow.com/a/62190276/12769288, so maybe the file watcher tries to restart the server while the previous one is still listening?

Additionally the same error seems to happen on occasion in one of the tests: https://github.com/denoland/deno/issues/16044

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 44 (15 by maintainers)

Commits related to this issue

Most upvoted comments

The work to fix this issue is being done out-of-bounds in another repo. We expect to ship the rewrite in v1.31 in February.

@GJZwiers this problem has been fixed and will be released in v1.32.4 next week.

I had to revert the PR that fixed it because it introduced another problems. We’re working on a rewrite of the server that should fix this problem.

For the people getting this issue when using std/http/server.ts, the issue should get fixed in the next patch release.

For the people getting this issue when using Deno.serve, I have made a separate issue: https://github.com/denoland/deno/issues/18186.

@Csulit It seems you are getting a different kind of error message, possibly a bug in Fresh. I suggest creating an issue over in that repository.

@bartlomieju Thanks, it seems fixed indeed. I will close this again after 1.32.4 is released.

I can confirm that I have very same issue on Windows 10 + WSL 2 linux subsystem but in my case there’s additional info:

Watcher File change detected! Restarting!
error: Uncaught AddrInUse: Address already in use (os error 98)
    core.runMicrotasks();
         ^
    at Object.runMicrotasks (deno:core/01_core.js:377:30)
    at processTicksAndRejections (https://deno.land/std@0.167.0/node/_next_tick.ts:62:10)
Watcher Process finished. Restarting on file change...

The fix is in https://github.com/denoland/deno/pull/16616. I’ll try to land that PR tonight.

Sill an issue in Ubuntu on Deno 1.40 when you kill the process with Ctrol+C and launch it again. It requires to killall -9 deno

Same problem on version 1.33.2:

➜  express5 deno --version
deno 1.33.2 (release, x86_64-apple-darwin)
v8 11.4.183.1
typescript 5.0.3
import express from "npm:express"

const app = express()

app.get('/', (req, res) => {
    res.send('hello world')
})

app.listen(3000)

console.log("Serve is listen on port: ", 3000)

Can reproduce it as well on 1.32.3, reopening issue.

The issue is fixed on version 1.31.3 so closing this, but the Deno.serve bug seems to be there still, for which https://github.com/denoland/deno/issues/16267 is still open.

For now, until the issue is fixed, the easiest workaround is to downgrade to v1.30.0

sudo deno upgrade --version 1.30.0

I ran into the same problem on 1.31.1, upgraded to 1.31.2 with fingers crossed, but same thing.

As a workaround to the problem, this seems to work for me.

import { Server } from 'https://deno.land/std@0.179.0/http/server.ts'

const handler = () => new Response('Hello World!')
const hostname = 'localhost'
const port = 8080

const server = new Server({ hostname, port, handler })
console.log(`Starting to listen on port ${port}`)
server.listenAndServe()

globalThis.addEventListener('unload', () => {
  server.close()
})

Same here with the basic test suggested by @ynwd

~ deno -V
deno 1.31.2

~ deno run -A --unstable --watch main.ts
Watcher Process started.
Listening on http://127.0.0.1:9000/

# edit main.ts

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

~ sudo netstat -tanup | grep 9000
tcp  0  0  127.0.0.1:9000  0.0.0.0:*  LISTEN  143534/deno         

i found this issue is still there while using the unstable FlashServer

I’m having the same problem with linux Pop!OS

Watcher File change detected! Restarting!
error: Uncaught AddrInUse: Address already in use (os error 98)
    core.runMicrotasks();
         ^
    at Object.runMicrotasks (deno:core/01_core.js:377:30)
    at processTicksAndRejections (https://deno.land/std@0.167.0/node/_next_tick.ts:62:10)