Skip to content

Commit

Permalink
fix: check on undefined for worker.stdio (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic authored and evilebottnawi committed Dec 17, 2018
1 parent c9574c9 commit c891a9c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 32 deletions.
70 changes: 38 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/WorkerPool.js
Expand Up @@ -21,6 +21,15 @@ class PoolWorker {
this.worker = childProcess.spawn(process.execPath, [].concat(options.nodeArgs || []).concat(workerPath, options.parallelJobs), {
stdio: ['ignore', 1, 2, 'pipe', 'pipe'],
});

// This prevents a problem where the worker stdio can be undefined
// when the kernel hits the limit of open files.
// More info can be found on: https://github.com/webpack-contrib/thread-loader/issues/2
if (!this.worker.stdio) {
throw new Error(`Failed to create the worker pool with workerId: ${workerId} and ${''
}configuration: ${JSON.stringify(options)}. Please verify if you hit the OS open files limit.`);
}

const [, , , readPipe, writePipe] = this.worker.stdio;
this.readPipe = readPipe;
this.writePipe = writePipe;
Expand Down
3 changes: 3 additions & 0 deletions test/__snapshots__/workerPool.test.js.snap
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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."`;
32 changes: 32 additions & 0 deletions test/workerPool.test.js
@@ -0,0 +1,32 @@
import childProcess from 'child_process';
import stream from 'stream';
import WorkerPool from '../src/WorkerPool';

jest.mock('child_process', () => {
return {
spawn: jest.fn(() => {
return {};
}),
};
});

describe('workerPool', () => {
it('should throw an error when worker.stdio is undefined', () => {
childProcess.spawn.mockImplementationOnce(() => { return {}; });

const workerPool = new WorkerPool({});
expect(() => workerPool.createWorker()).toThrowErrorMatchingSnapshot();
expect(() => workerPool.createWorker()).toThrowError('Please verify if you hit the OS open files limit');
});

it('should not throw an error when worker.stdio is defined', () => {
childProcess.spawn.mockImplementationOnce(() => {
return {
stdio: new Array(5).fill(new stream.PassThrough()),
};
});

const workerPool = new WorkerPool({});
expect(() => workerPool.createWorker()).not.toThrow();
});
});

0 comments on commit c891a9c

Please sign in to comment.