mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
fix(ext/node): delay accept() call 2 ticks in net.Server#listen (#25481)
A workaround for the issue #25480 `Deno.Listener` can't be closed synchronously after `accept()` is called. This PR delays the `accept` call 2 ticks (The listener callback is called 1 tick later. So the 1 tick delay is not enough), and makes `net.Server` capable of being closed synchronously. This unblocks `npm:detect-port` and `npm:portfinder` closes #18301 closes #25175
This commit is contained in:
parent
30687c786c
commit
ce1d668929
2 changed files with 28 additions and 1 deletions
|
@ -45,6 +45,7 @@ import {
|
|||
INITIAL_ACCEPT_BACKOFF_DELAY,
|
||||
MAX_ACCEPT_BACKOFF_DELAY,
|
||||
} from "ext:deno_node/internal_binding/_listen.ts";
|
||||
import { nextTick } from "ext:deno_node/_next_tick.ts";
|
||||
|
||||
/** The type of TCP socket. */
|
||||
enum socketType {
|
||||
|
@ -228,7 +229,14 @@ export class TCP extends ConnectionWrap {
|
|||
this.#port = address.port;
|
||||
|
||||
this.#listener = listener;
|
||||
this.#accept();
|
||||
|
||||
// TODO(kt3k): Delays the accept() call 2 ticks. Deno.Listener can't be closed
|
||||
// synchronously when accept() is called. By delaying the accept() call,
|
||||
// the user can close the server synchronously in the callback of listen().
|
||||
// This workaround enables `npm:detect-port` to work correctly.
|
||||
// Remove these nextTick calls when the below issue resolved:
|
||||
// https://github.com/denoland/deno/issues/25480
|
||||
nextTick(nextTick, () => this.#accept());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -228,3 +228,22 @@ Deno.test("[node/net] BlockList doesn't leak resources", () => {
|
|||
blockList.addAddress("1.1.1.1");
|
||||
assert(blockList.check("1.1.1.1"));
|
||||
});
|
||||
|
||||
Deno.test("[node/net] net.Server can listen on the same port immediately after it's closed", async () => {
|
||||
const serverClosed = Promise.withResolvers<void>();
|
||||
const server = net.createServer();
|
||||
server.on("error", (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
server.listen(0, () => {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const { port } = server.address() as any;
|
||||
server.close();
|
||||
server.listen(port, () => {
|
||||
server.close(() => {
|
||||
serverClosed.resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
await serverClosed.promise;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue