Skip to content

Commit

Permalink
fs: fix not found close creation context
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jul 17, 2024
1 parent fcda284 commit 86f8f6a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/internal/fs/read/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {

const { Buffer } = require('buffer');

const { FSReqCallback, close, read } = internalBinding('fs');
const binding = internalBinding('fs');

const {
AbortError,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-close-fast-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

require('../common');

const assert = require('assert');
const fs = require('fs');
const path = require('path');

// 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(path.join(__dirname, './test-fs-close.js'), (a, b) => {
assert.strictEqual(a, null);

Check failure on line 18 in test/parallel/test-fs-close-fast-api.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:assert:126 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + [Error: EMFILE: too many open files, open '/home/runner/work/node/node/test/parallel/test-fs-close.js'] { + code: 'EMFILE', + errno: -24, + path: '/home/runner/work/node/node/test/parallel/test-fs-close.js', + syscall: 'open' + } - null at ReadFileContext.callback (/home/runner/work/node/node/test/parallel/test-fs-close-fast-api.js:18:14) at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:299:13) { generatedMessage: true, code: 'ERR_ASSERTION', actual: [Error: EMFILE: too many open files, open '/home/runner/work/node/node/test/parallel/test-fs-close.js'] { errno: -24, code: 'EMFILE', syscall: 'open', path: '/home/runner/work/node/node/test/parallel/test-fs-close.js' }, expected: null, operator: 'strictEqual' } Node.js v23.0.0-pre --- stdout --- undefined Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-fs-close-fast-api.js

Check failure on line 18 in test/parallel/test-fs-close-fast-api.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:assert:126 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + [Error: EMFILE: too many open files, open '/Users/runner/work/node/node/test/parallel/test-fs-close.js'] { + code: 'EMFILE', + errno: -24, + path: '/Users/runner/work/node/node/test/parallel/test-fs-close.js', + syscall: 'open' + } - null at ReadFileContext.callback (/Users/runner/work/node/node/test/parallel/test-fs-close-fast-api.js:18:14) at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:299:13) { generatedMessage: true, code: 'ERR_ASSERTION', actual: [Error: EMFILE: too many open files, open '/Users/runner/work/node/node/test/parallel/test-fs-close.js'] { errno: -24, code: 'EMFILE', syscall: 'open', path: '/Users/runner/work/node/node/test/parallel/test-fs-close.js' }, expected: null, operator: 'strictEqual' } Node.js v23.0.0-pre --- stdout --- undefined Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-fs-close-fast-api.js
assert.ok(b.length > 0);
});
} catch {
// do nothing
}

}

console.log(val);

0 comments on commit 86f8f6a

Please sign in to comment.