From 67b7a95b0409cc2dd8b7b42a6f5a80a1de335cd5 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Thu, 15 Oct 2026 08:29:30 +0200 Subject: [PATCH] test: deflake test-filehandle-close An fd released with closeSync() can be reused by background I/O before FileHandle.close() runs in the thread pool, so closing it can succeed. Construct the handle around a high invalid fd to keep the native EBADF rejection path deterministic. Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-10-15.md Signed-off-by: Filip Skokan Assisted-by: Codex --- test/parallel/test-filehandle-close.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-filehandle-close.js b/test/parallel/test-filehandle-close.js index 6e55f3f06d19..179c62250bbb 100644 --- a/test/parallel/test-filehandle-close.js +++ b/test/parallel/test-filehandle-close.js @@ -1,14 +1,18 @@ +// Flags: --expose-internals 'use strict'; const common = require('../common'); const assert = require('assert'); -const fs = require('fs'); +const { internalBinding } = require('internal/test/binding'); +const { FileHandle } = require('internal/fs/promises'); +const { FileHandle: NativeFileHandle } = internalBinding('fs'); -// Test that using FileHandle.close to close an already-closed fd fails -// with EBADF. +// Test that closing a FileHandle with an invalid fd fails with EBADF. +// Use a high invalid fd so background I/O cannot reuse it before close runs. (async function() { - const fh = await fs.promises.open(__filename); - fs.closeSync(fh.fd); + const fh = common.runWithInvalidFD( + (fd) => new FileHandle(new NativeFileHandle(fd)), + ); await assert.rejects(() => fh.close(), { code: 'EBADF',