Skip to content

Commit 04fb14c

Browse files
Trottrvagg
authored andcommitted
test: fix flaky test-child-process-emfile
Require the test setup to obtain an EMFILE error and not ENFILE as ENFILE means there is a race condition with other processes that may close files before `spawn()` is called by the test. Fixes: #2666 PR-URL: #3430 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6936468 commit 04fb14c

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

test/sequential/test-child-process-emfile.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var spawn = require('child_process').spawn;
5-
var fs = require('fs');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const child_process = require('child_process');
5+
const fs = require('fs');
66

77
if (common.isWindows) {
88
console.log('1..0 # Skipped: no RLIMIT_NOFILE on Windows');
99
return;
1010
}
1111

12-
var openFds = [];
12+
const ulimit = Number(child_process.execSync('ulimit -Hn'));
13+
if (ulimit > 64 || Number.isNaN(ulimit)) {
14+
// Sorry about this nonsense. It can be replaced if
15+
// https://github.com/nodejs/node-v0.x-archive/pull/2143#issuecomment-2847886
16+
// ever happens.
17+
const result = child_process.spawnSync(
18+
'/bin/sh',
19+
['-c', `ulimit -n 64 && '${process.execPath}' '${__filename}'`]
20+
);
21+
assert.strictEqual(result.stdout.toString(), '');
22+
assert.strictEqual(result.stderr.toString(), '');
23+
assert.strictEqual(result.status, 0);
24+
assert.strictEqual(result.error, undefined);
25+
return;
26+
}
27+
28+
const openFds = [];
1329

1430
for (;;) {
1531
try {
1632
openFds.push(fs.openSync(__filename, 'r'));
1733
} catch (err) {
18-
assert(err.code === 'EMFILE' || err.code === 'ENFILE');
34+
assert(err.code === 'EMFILE');
1935
break;
2036
}
2137
}
2238

2339
// Should emit an error, not throw.
24-
var proc = spawn(process.execPath, ['-e', '0']);
40+
const proc = child_process.spawn(process.execPath, ['-e', '0']);
2541

2642
proc.on('error', common.mustCall(function(err) {
27-
assert(err.code === 'EMFILE' || err.code === 'ENFILE');
43+
assert.strictEqual(err.code, 'EMFILE');
2844
}));
2945

3046
proc.on('exit', function() {

0 commit comments

Comments
 (0)