denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.99k stars 5.23k forks source link

Uncaught AddrInUse when restarting server in watch mode after saving file #16699

Open GJZwiers opened 1 year ago

GJZwiers commented 1 year ago

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

Reproduction:

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");
});
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

wesbos commented 1 year ago

Having this issue too. I can't use --watch with an express server because it says the port is in use after the first restart.

I think deno is too fast and it restarts the app before the express server can release the port.

wesbos commented 1 year ago

I don't think it's time bound after doing some tests. I think maybe an issue with the beforeExit event not firing?

bartlomieju commented 1 year ago

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

minhluudinh commented 1 year ago

This issue still exists in Deno v1.28

wesbos commented 1 year ago

Still an issue in 1.28.3

GJZwiers commented 1 year ago

Seems like it resurfaced in 1.28.3 because I can't reproduce it on 1.28.2

bartlomieju commented 1 year ago

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.

jakub-gawryl commented 1 year ago

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...
felipe300 commented 1 year ago

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)
synthbeat commented 1 year ago

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)

Can confirm same bug still present on macOS.

deno 1.29.1 (release, x86_64-apple-darwin) v8 10.9.194.5 typescript 4.9.4

jesusandres31 commented 1 year ago

still same error on deno 1.29.2 (release, x86_64-pc-windows-msvc)

bartlomieju commented 1 year ago

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.

geric commented 1 year ago

no longer an issue on 1.31.0.

GJZwiers commented 1 year ago

Cannot reproduce anymore either, I will close this now.

MihaiSturza commented 1 year ago

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

ynwd commented 1 year ago
await Deno.serve(() => new Response("Hello World!"));
deno run -A --unstable --watch main.ts   
Watcher File change detected! Restarting!
error: Uncaught (in promise) AddrInUse: Address already in use (os error 48)
Watcher Process finished. Restarting on file change...
markc commented 1 year ago

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         
sstiglitz commented 1 year ago

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()
})
markc commented 1 year ago

@sstiglitz would you have any idea how this strategy could be applied to Deno Fresh?

csulit commented 1 year ago

Still an issue with the latest version.

Still an issue

Watcher File change detected! Restarting! error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'port') port = (server.addrs[0] as Deno.NetAddr).port; ^ at serve (https://deno.land/std@0.150.0/http/server.ts:591:44) at start (https://deno.land/x/fresh@1.1.4/src/server/mod.ts:70:11) at async file:///Users/christianangelosulit/Documents/GitHub/kmc/deno_project/my-project/main.ts:13:1 Watcher Process finished. Restarting on file change... ^C xxx@Christians-MacBook-Pro-2 my-project % xxx@Christians-MacBook-Pro-2 my-project % xxx@Christians-MacBook-Pro-2 my-project % deno upgrade Looking up latest version Local deno version 1.31.2 is the most recent release

markc commented 1 year ago

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

sudo deno upgrade --version 1.30.0
GJZwiers commented 1 year ago

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.

ryansb commented 1 year ago

For brew users, the workaround is:

# the cask version that pointed to deno@1.30.0
FORMULA_URL='https://raw.githubusercontent.com/Homebrew/homebrew-core/2b3035ebfc70660dc8753678b43404d5d8016f99/Formula/deno.rb'
# get the .rb file locally
curl -O $FORMULA_URL
# replace the installed deno with the v1.30.0 version
brew uninstall deno
brew install ./deno.rb
DwieDave commented 1 year ago

# the cask version that pointed to deno@1.30.0

Do you have an URL to deno@1.31.1 because that for me was the last version without this error. How did you get the URL to the old formula version?

EDIT: Just noticed the version you linked is 1.31.0, so that difference should be fine by me for now. Thanks for the workaround.

mrtornado commented 1 year ago

I had the same issue here after I upgraded to 1.31.1 so I downgraded to 1.31.0 since it was working fine for me. Just stay at 1.31.0 until we have a fix.

youwei997 commented 1 year ago

I had the same issue here after I upgraded to 1.31.1 so I downgraded to 1.31.0 since it was working fine for me. Just stay at 1.31.0 until we have a fix.

Thanks, 1.31.0 is normal. I had a problem with the update to 1.31.2 before, and I feel that this kind of problem should not have happened

CarlosAmaral commented 1 year ago

Same problem on version 1.31.2 with fresh

GJZwiers commented 1 year ago

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.

RrNn commented 1 year ago

Looks like it crept in again on version 1.32.3

GJZwiers commented 1 year ago

Can reproduce it as well on 1.32.3, reopening issue.

bartlomieju commented 1 year ago

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

GJZwiers commented 1 year ago

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

starrematte commented 1 year ago

Hi all. Until the next week release, I found a solution by using the serveListener (v 1.32.3):

import { serveListener } from "https://deno.land/std@0.182.0/http/server.ts";

const listener = Deno.listen({ port: 8080 })
serveListener(listener, (req) => {
  console.log("handling", JSON.stringify(req.url))
  return new Response("hello there!", { status: 200 })
})

Cheers!

EDIT: remove await before serveListener RE-EDIT: seems unstable too at this point

keelii commented 1 year ago

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)
sant123 commented 1 year ago

I'm having the same issue 😕

image

image

bartlomieju commented 1 year ago

See https://github.com/denoland/deno/issues/18960, it will be fixed next week.

ooyay commented 1 year ago

still in 1.33.2 at my koa project, but doing this its woking..


const server = app.listen(PORT)
addEventListener('unload', () => {
  server.close()
})
lionel-rowe commented 1 year ago

@bartlomieju I'm still having the same issue while using fresh@1.1.2, as of the latest commit (including #18998) built with cargo build --release, WSL on Windows 11 with Ubuntu 22.04.2 LTS.

bartlomieju commented 1 year ago

CC @mmastrac

bartlomieju commented 1 year ago

@lionel-rowe was that a debug or release build?

dsherret commented 1 year ago

Release (cargo build --release)

huseeiin commented 1 year ago

same thing in 1.34.3

avalero commented 7 months ago

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

PierreCapo commented 7 months ago

Same issue on recent Deno versions. I am on MacOS 14.2. It makes the watcher completely unusable sadly

emehmet commented 4 months ago

Same issue on Deno v1.43.3. I use Apllo server and expressjs on Linux debian 12. It's like every refresh opens a different thread. Each opened thread is giving a different addrinuse error.

StephenHaney commented 4 weeks ago

Having this issue with Deno.serve and a Hono router in Deno v1.45.4.

Also, my Deno.serve is still running even though I've exited the terminal completely – maybe related?

sveisvei commented 1 week ago

Reproducable with adding

addEventListener("hmr", (e) => {
   console.log("HMR triggered", e);
});