1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -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:
Bartek Iwańczuk 2023-08-18 13:48:18 +02:00 committed by Divy Srivastava
parent fc4836bce9
commit 045074e152
2 changed files with 44 additions and 12 deletions

View file

@ -751,3 +751,29 @@ Deno.test(
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}`,
);
});

View file

@ -1610,19 +1610,25 @@ export class ServerImpl extends EventEmitter {
return;
}
this.#ac = ac;
this.#server = serve(
{
handler: handler as Deno.ServeHandler,
...this.#addr,
signal: ac.signal,
// @ts-ignore Might be any without `--unstable` flag
onListen: ({ port }) => {
this.#addr!.port = port;
this.emit("listening");
try {
this.#server = serve(
{
handler: handler as Deno.ServeHandler,
...this.#addr,
signal: ac.signal,
// @ts-ignore Might be any without `--unstable` flag
onListen: ({ port }) => {
this.#addr!.port = port;
this.emit("listening");
},
...this._additionalServeOptions?.(),
},
...this._additionalServeOptions?.(),
},
);
);
} catch (e) {
this.emit("error", e);
return;
}
if (this.#unref) {
this.#server.unref();
}