Skip to content

Commit 86f8f6a

Browse files
committed
fs: fix not found close creation context
1 parent fcda284 commit 86f8f6a

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/internal/fs/read/context.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515

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

18-
const { FSReqCallback, close, read } = internalBinding('fs');
18+
const binding = internalBinding('fs');
1919

2020
const {
2121
AbortError,
@@ -102,11 +102,11 @@ class ReadFileContext {
102102
length = MathMin(kReadFileBufferLength, this.size - this.pos);
103103
}
104104

105-
const req = new FSReqCallback();
105+
const req = new binding.FSReqCallback();
106106
req.oncomplete = readFileAfterRead;
107107
req.context = this;
108108

109-
read(this.fd, buffer, offset, length, -1, req);
109+
binding.read(this.fd, buffer, offset, length, -1, req);
110110
}
111111

112112
close(err) {
@@ -117,12 +117,12 @@ class ReadFileContext {
117117
return;
118118
}
119119

120-
const req = new FSReqCallback();
120+
const req = new binding.FSReqCallback();
121121
req.oncomplete = readFileAfterClose;
122122
req.context = this;
123123
this.err = err;
124124

125-
close(this.fd, req);
125+
binding.close(this.fd, req);
126126
}
127127
}
128128

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
const fs = require('fs');
7+
const path = require('path');
8+
9+
// This test runs `fs.readFile` which calls ReadFileContext
10+
// that triggers binding.close() when read operation is done.
11+
// The goal of this test is to not crash
12+
let val;
13+
14+
// For loop is required to trigger fast API.
15+
for (let i = 0; i < 100_000; i++) {
16+
try {
17+
val = fs.readFile(path.join(__dirname, './test-fs-close.js'), (a, b) => {
18+
assert.strictEqual(a, null);
19+
assert.ok(b.length > 0);
20+
});
21+
} catch {
22+
// do nothing
23+
}
24+
25+
}
26+
27+
console.log(val);

0 commit comments

Comments
 (0)