summaryrefslogtreecommitdiff
path: root/test/sequential/test-child-process-emfile.js
blob: 8f59842f99563b7a0e92162177039d3bb4d419a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');
const fs = require('fs');

if (common.isWindows) {
  common.skip('no RLIMIT_NOFILE on Windows');
  return;
}

const ulimit = Number(child_process.execSync('ulimit -Hn'));
if (ulimit > 64 || Number.isNaN(ulimit)) {
  // Sorry about this nonsense. It can be replaced if
  // https://github.com/nodejs/node-v0.x-archive/pull/2143#issuecomment-2847886
  // ever happens.
  const result = child_process.spawnSync(
    '/bin/sh',
    ['-c', `ulimit -n 64 && '${process.execPath}' '${__filename}'`]
  );
  assert.strictEqual(result.stdout.toString(), '');
  assert.strictEqual(result.stderr.toString(), '');
  assert.strictEqual(result.status, 0);
  assert.strictEqual(result.error, undefined);
  return;
}

const openFds = [];

for (;;) {
  try {
    openFds.push(fs.openSync(__filename, 'r'));
  } catch (err) {
    assert(err.code === 'EMFILE');
    break;
  }
}

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

proc.on('error', common.mustCall(function(err) {
  assert.strictEqual(err.code, 'EMFILE');
}));

proc.on('exit', function() {
  common.fail('"exit" should not be emitted (the process never spawned!)');
});

// close one fd for LSan
if (openFds.length >= 1) {
  fs.closeSync(openFds.pop());
}