From d8f6136b2671f1e231b3e7228ea78ad46d4b8f38 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 17 Jul 2024 15:56:38 -0400 Subject: [PATCH] fs: fix not found `close` creation context --- lib/internal/fs/read/context.js | 10 ++++----- test/parallel/test-fs-close-fast-api.js | 29 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-fs-close-fast-api.js diff --git a/lib/internal/fs/read/context.js b/lib/internal/fs/read/context.js index b1a5d6ae03e953..2e00dc6a56fe97 100644 --- a/lib/internal/fs/read/context.js +++ b/lib/internal/fs/read/context.js @@ -15,7 +15,7 @@ const { const { Buffer } = require('buffer'); -const { FSReqCallback, close, read } = internalBinding('fs'); +const binding = internalBinding('fs'); const { AbortError, @@ -102,11 +102,11 @@ class ReadFileContext { length = MathMin(kReadFileBufferLength, this.size - this.pos); } - const req = new FSReqCallback(); + const req = new binding.FSReqCallback(); req.oncomplete = readFileAfterRead; req.context = this; - read(this.fd, buffer, offset, length, -1, req); + binding.read(this.fd, buffer, offset, length, -1, req); } close(err) { @@ -117,12 +117,12 @@ class ReadFileContext { return; } - const req = new FSReqCallback(); + const req = new binding.FSReqCallback(); req.oncomplete = readFileAfterClose; req.context = this; this.err = err; - close(this.fd, req); + binding.close(this.fd, req); } } diff --git a/test/parallel/test-fs-close-fast-api.js b/test/parallel/test-fs-close-fast-api.js new file mode 100644 index 00000000000000..15f373873f7c54 --- /dev/null +++ b/test/parallel/test-fs-close-fast-api.js @@ -0,0 +1,29 @@ +'use strict'; + +require('../common'); + +const assert = require('assert'); +const fs = require('fs'); + +// This test runs `fs.readFile` which calls ReadFileContext +// that triggers binding.close() when read operation is done. +// The goal of this test is to not crash +let val; + +// For loop is required to trigger fast API. +for (let i = 0; i < 100_000; i++) { + try { + val = fs.readFile(__filename, (a, b) => { + try { + assert.strictEqual(a, null); + assert.ok(b.length > 0); + } catch { + // Ignore all errors + } + }); + } catch { + // do nothing + } +} + +console.log(val);