From 97f4cc77668a28e4cee44ba61225b8f9897bb470 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Mon, 11 Jul 2022 15:27:34 +0800 Subject: [PATCH] cluster: fix fd leak PR-URL: https://github.com/nodejs/node/pull/43650 Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina --- lib/internal/cluster/child.js | 2 ++ .../test-cluster-worker-handle-close.js | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/parallel/test-cluster-worker-handle-close.js diff --git a/lib/internal/cluster/child.js b/lib/internal/cluster/child.js index 00638e77845..2fc4390e72c 100644 --- a/lib/internal/cluster/child.js +++ b/lib/internal/cluster/child.js @@ -210,6 +210,8 @@ function onconnection(message, handle) { if (accepted) server.onconnection(0, handle); + else + handle.close(); } function send(message, cb) { diff --git a/test/parallel/test-cluster-worker-handle-close.js b/test/parallel/test-cluster-worker-handle-close.js new file mode 100644 index 00000000000..47a80ef1cd1 --- /dev/null +++ b/test/parallel/test-cluster-worker-handle-close.js @@ -0,0 +1,27 @@ +'use strict'; +const common = require('../common'); +const cluster = require('cluster'); +const net = require('net'); + +if (cluster.isPrimary) { + cluster.schedulingPolicy = cluster.SCHED_RR; + cluster.fork(); +} else { + const server = net.createServer(common.mustNotCall()); + server.listen(0, common.mustCall(() => { + net.connect(server.address().port); + })); + process.prependListener('internalMessage', common.mustCallAtLeast((message, handle) => { + if (message.act !== 'newconn') { + return; + } + // Make the worker drops the connection, see `rr` and `onconnection` in child.js + server.close(); + const close = handle.close; + handle.close = common.mustCall(() => { + close.call(handle, common.mustCall(() => { + process.exit(); + })); + }); + })); +}