From f29559e46c09fc8c5a51e6485689203c39593b39 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Thu, 20 Feb 2025 08:55:02 +0100 Subject: [PATCH] fix: possible timing issue in utility-process spec (#45727) This fixture has been calling process.exit() immediately after writing to stdout and stderr, which the Node.js docs say is risky behavior: > Calling process.exit() will force the process to exit as quickly as > possible even if there are still asynchronous operations pending that > have not yet completed fully, including I/O operations to > process.stdout and process.stderr. This fixture's been around for years without problems (AFAIK). The writes are very small ('hello\n' and 'world') and finish quickly. But recently I've been testing on a very slow CI machine. There, I see this spec flaking when it expects stderr to be 'world' but it gets ''. This PR changes the fixture to wait for stdout & stderr to flush before calling process.exit(). Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- spec/fixtures/api/utility-process/log.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spec/fixtures/api/utility-process/log.js b/spec/fixtures/api/utility-process/log.js index 87a0ceafdfab..209afeedbce2 100644 --- a/spec/fixtures/api/utility-process/log.js +++ b/spec/fixtures/api/utility-process/log.js @@ -1,3 +1,7 @@ -console.log('hello'); -process.stderr.write('world'); -process.exit(0); +function write (writable, chunk) { + return new Promise((resolve) => writable.write(chunk, resolve)); +} + +write(process.stdout, 'hello\n') + .then(() => write(process.stderr, 'world')) + .then(() => process.exit(0));