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 d8f6136
Show file tree
Hide file tree
Showing 2 changed files with 34 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
29 changes: 29 additions & 0 deletions test/parallel/test-fs-close-fast-api.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit d8f6136

Please sign in to comment.