From 4f58e0cae8fb71cdccc2ecdf6e27ec5793b96491 Mon Sep 17 00:00:00 2001 From: Christian Aurich Zanettini Martins Date: Sun, 15 Nov 2026 02:44:48 -0300 Subject: [PATCH] test: deflake test-process-timeout-blocked The fixtures that exit while a Worker is blocked created their file in an 'exit' listener, before the process actually starts exiting, e.g. while an uncaught error is still being formatted. If the deadline expired in between, the main thread handled the timeout and the test failed instead of retrying. The main thread cannot do that once it is exiting, so retry when it did, and drop the 'exit' listeners. Signed-off-by: Christian Aurich Zanettini Martins --- .../fixtures/process-timeout/blocked-worker-at-exit.js | 6 ++---- .../process-timeout/blocked-worker-at-process-exit.js | 6 ++---- test/parallel/test-process-timeout-blocked.js | 10 +++++++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/fixtures/process-timeout/blocked-worker-at-exit.js b/test/fixtures/process-timeout/blocked-worker-at-exit.js index 5ff21b423f62..febad9d0c58f 100644 --- a/test/fixtures/process-timeout/blocked-worker-at-exit.js +++ b/test/fixtures/process-timeout/blocked-worker-at-exit.js @@ -1,8 +1,8 @@ 'use strict'; // Lets the event loop stop while a Worker is blocked in a synchronous native -// call, so that exiting the process waits for the Worker. The first argument is -// a path at which a file is created once the event loop has stopped. +// call, so that exiting the process waits for the Worker. The child process +// creates a file named after the first argument once it has started. const fs = require('fs'); const path = require('path'); const { Worker } = require('worker_threads'); @@ -26,5 +26,3 @@ const interval = setInterval(() => { worker.unref(); } }, 10); - -process.on('exit', () => fs.writeFileSync(marker, '')); diff --git a/test/fixtures/process-timeout/blocked-worker-at-process-exit.js b/test/fixtures/process-timeout/blocked-worker-at-process-exit.js index b7e853e62f6b..05afe71fa2e6 100644 --- a/test/fixtures/process-timeout/blocked-worker-at-process-exit.js +++ b/test/fixtures/process-timeout/blocked-worker-at-process-exit.js @@ -1,7 +1,7 @@ 'use strict'; // Exits the process while a Worker is blocked in a synchronous native call, so -// that exiting the process waits for the Worker. The first argument is a path -// at which a file is created once the process is exiting. The second argument +// that exiting the process waits for the Worker. The child process creates a +// file named after the first argument once it has started. The second argument // is how the process exits: 'exit' calls process.exit(), 'throw' throws an // uncaught exception, and 'reject' leaves a promise rejection unhandled. const fs = require('fs'); @@ -27,5 +27,3 @@ const interval = setInterval(() => { if (mode === 'reject') return Promise.reject(new Error('boom')); process.exit(0); }, 10); - -process.on('exit', () => fs.writeFileSync(marker, '')); diff --git a/test/parallel/test-process-timeout-blocked.js b/test/parallel/test-process-timeout-blocked.js index db3553e11d59..84c3ecb937e7 100644 --- a/test/parallel/test-process-timeout-blocked.js +++ b/test/parallel/test-process-timeout-blocked.js @@ -13,8 +13,10 @@ const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); // The deadline is measured from the start of the process, so on a slow machine -// it can expire before the fixture has reached the state under test, which the -// fixture signals by creating a file. If it did not get that far and did not +// it can expire before the fixture has reached the state under test. Unless the +// main thread is blocked or exiting by then, it handles the timeout and prints +// what it was doing. A fixture that expects it to do so signals that it got far +// enough by creating a file. If the fixture did not get that far and did not // print the expected message, run it again with a longer timeout. function runUntilReady(fixture, expected, args = []) { for (let timeout = common.platformTimeout(1000); ; timeout *= 2) { @@ -30,7 +32,9 @@ function runUntilReady(fixture, expected, args = []) { ], { encoding: 'utf8' }); const elapsed = Number(process.hrtime.bigint() - start) / 1e6; - if (!expected.test(child.stderr) && !fs.existsSync(marker) && + if (!expected.test(child.stderr) && + /^Main thread was/m.test(child.stderr) && + !fs.existsSync(marker) && timeout < common.platformTimeout(16000)) { continue; }