Skip to content

Commit c891a9c

Browse files
misticevilebottnawi
authored andcommitted
fix: check on undefined for worker.stdio (#45)
1 parent c9574c9 commit c891a9c

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
lines changed

package-lock.json

Lines changed: 38 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WorkerPool.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ class PoolWorker {
2121
this.worker = childProcess.spawn(process.execPath, [].concat(options.nodeArgs || []).concat(workerPath, options.parallelJobs), {
2222
stdio: ['ignore', 1, 2, 'pipe', 'pipe'],
2323
});
24+
25+
// This prevents a problem where the worker stdio can be undefined
26+
// when the kernel hits the limit of open files.
27+
// More info can be found on: https://github.com/webpack-contrib/thread-loader/issues/2
28+
if (!this.worker.stdio) {
29+
throw new Error(`Failed to create the worker pool with workerId: ${workerId} and ${''
30+
}configuration: ${JSON.stringify(options)}. Please verify if you hit the OS open files limit.`);
31+
}
32+
2433
const [, , , readPipe, writePipe] = this.worker.stdio;
2534
this.readPipe = readPipe;
2635
this.writePipe = writePipe;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`workerPool should throw an error when worker.stdio is undefined 1`] = `"Failed to create the worker pool with workerId: 1 and configuration: {}. Please verify if you hit the OS open files limit."`;

test/workerPool.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import childProcess from 'child_process';
2+
import stream from 'stream';
3+
import WorkerPool from '../src/WorkerPool';
4+
5+
jest.mock('child_process', () => {
6+
return {
7+
spawn: jest.fn(() => {
8+
return {};
9+
}),
10+
};
11+
});
12+
13+
describe('workerPool', () => {
14+
it('should throw an error when worker.stdio is undefined', () => {
15+
childProcess.spawn.mockImplementationOnce(() => { return {}; });
16+
17+
const workerPool = new WorkerPool({});
18+
expect(() => workerPool.createWorker()).toThrowErrorMatchingSnapshot();
19+
expect(() => workerPool.createWorker()).toThrowError('Please verify if you hit the OS open files limit');
20+
});
21+
22+
it('should not throw an error when worker.stdio is defined', () => {
23+
childProcess.spawn.mockImplementationOnce(() => {
24+
return {
25+
stdio: new Array(5).fill(new stream.PassThrough()),
26+
};
27+
});
28+
29+
const workerPool = new WorkerPool({});
30+
expect(() => workerPool.createWorker()).not.toThrow();
31+
});
32+
});

0 commit comments

Comments
 (0)