Skip to content

Commit

Permalink
test: fix test-child-process-fork-net
Browse files Browse the repository at this point in the history
The child processes are supposed to get 4 messages (2 ends, 2 writes).
Previously the mustCall() wrapping the message listener attempts
to match exactly 1 invocation which is bound to fail but could be
swallowed if the child happens to be killed before the exit event
is fired for the mustCall() check to work. In the CI, on some
machines the kill() could happen after the child process finishes
with the mustCall() check, resulting in EPERM errors in kill().

This patch fixes the mustCall() checks (updating the expected
invocation count to 4) and swallow the errors when kill() fails,
which should be fine because they are only there for cleanup.

PR-URL: #51841
Refs: #51813
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
joyeecheung authored and marco-ippolito committed Feb 27, 2024
1 parent b502be1 commit 1f6551d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions test/parallel/test-child-process-fork-net.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// This tests that a socket sent to the forked process works.
// See https://github.com/nodejs/node/commit/dceebbfa

'use strict';
const {
mustCall,
Expand Down Expand Up @@ -65,7 +68,7 @@ if (process.argv[2] === 'child') {
socket.on('finish', mustCall(() => {
debug(`[${id}] socket finished ${m}`);
}));
}));
}, 4));

process.on('message', mustCall((m) => {
if (m !== 'close') return;
Expand All @@ -74,7 +77,7 @@ if (process.argv[2] === 'child') {
debug(`[${id}] ending ${i}/${needEnd.length}`);
endMe.end('end');
});
}));
}, 4));

process.on('disconnect', mustCall(() => {
debug(`[${id}] process disconnect, ending`);
Expand Down Expand Up @@ -146,9 +149,22 @@ if (process.argv[2] === 'child') {
server.on('close', mustCall(function() {
closeEmitted = true;

child1.kill();
child2.kill();
child3.kill();
// Clean up child processes.
try {
child1.kill();
} catch {
debug('child process already terminated');
}
try {
child2.kill();
} catch {
debug('child process already terminated');
}
try {
child3.kill();
} catch {
debug('child process already terminated');
}
}));

server.listen(0, '127.0.0.1');
Expand Down

0 comments on commit 1f6551d

Please sign in to comment.