Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);