mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 16:42:21 -05:00
fix(node/http): emit error when addr in use (#20200)
Closes https://github.com/denoland/deno/issues/20186
This commit is contained in:
parent
2150ae7d60
commit
fc6a537c2b
2 changed files with 44 additions and 12 deletions
|
@ -751,3 +751,29 @@ Deno.test(
|
||||||
assertEquals(body, "hello");
|
assertEquals(body, "hello");
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Deno.test("[node/http] server emits error if addr in use", async () => {
|
||||||
|
const promise = deferred<void>();
|
||||||
|
const promise2 = deferred<Error>();
|
||||||
|
|
||||||
|
const server = http.createServer();
|
||||||
|
server.listen(9001);
|
||||||
|
|
||||||
|
const server2 = http.createServer();
|
||||||
|
server2.on("error", (e) => {
|
||||||
|
promise2.resolve(e);
|
||||||
|
});
|
||||||
|
server2.listen(9001);
|
||||||
|
|
||||||
|
const err = await promise2;
|
||||||
|
server.close(() => promise.resolve());
|
||||||
|
server2.close();
|
||||||
|
await promise;
|
||||||
|
const expectedMsg = Deno.build.os === "windows"
|
||||||
|
? "Only one usage of each socket address"
|
||||||
|
: "Address already in use";
|
||||||
|
assert(
|
||||||
|
err.message.startsWith(expectedMsg),
|
||||||
|
`Wrong error: ${err.message}`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
|
@ -1610,19 +1610,25 @@ export class ServerImpl extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#ac = ac;
|
this.#ac = ac;
|
||||||
this.#server = serve(
|
try {
|
||||||
{
|
this.#server = serve(
|
||||||
handler: handler as Deno.ServeHandler,
|
{
|
||||||
...this.#addr,
|
handler: handler as Deno.ServeHandler,
|
||||||
signal: ac.signal,
|
...this.#addr,
|
||||||
// @ts-ignore Might be any without `--unstable` flag
|
signal: ac.signal,
|
||||||
onListen: ({ port }) => {
|
// @ts-ignore Might be any without `--unstable` flag
|
||||||
this.#addr!.port = port;
|
onListen: ({ port }) => {
|
||||||
this.emit("listening");
|
this.#addr!.port = port;
|
||||||
|
this.emit("listening");
|
||||||
|
},
|
||||||
|
...this._additionalServeOptions?.(),
|
||||||
},
|
},
|
||||||
...this._additionalServeOptions?.(),
|
);
|
||||||
},
|
} catch (e) {
|
||||||
);
|
this.emit("error", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.#unref) {
|
if (this.#unref) {
|
||||||
this.#server.unref();
|
this.#server.unref();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue