Skip to content

Commit

Permalink
fix(bin): handle process signals correctly when the server isn't re…
Browse files Browse the repository at this point in the history
…ady yet (#1432)
  • Loading branch information
edi9999 authored and michael-ciniawsky committed Aug 26, 2018
1 parent e2220c4 commit 334c3a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
23 changes: 14 additions & 9 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
const createDomain = require('../lib/util/createDomain'); // eslint-disable-line
const createLog = require('../lib/createLog');

let server;

['SIGINT', 'SIGTERM'].forEach((sig) => {
process.on(sig, () => {
if (server) {
server.close(() => {
process.exit(); // eslint-disable-line no-process-exit
});
} else {
process.exit(); // eslint-disable-line no-process-exit
}
});
});

// Prefer the local installation of webpack-dev-server
if (importLocal(__filename)) {
debug('Using local install of webpack-dev-server');
Expand Down Expand Up @@ -392,7 +406,6 @@ function startDevServer(webpackOptions, options) {

const suffix = (options.inline !== false || options.lazy === true ? '/' : '/webpack-dev-server/');

let server;
try {
server = new Server(compiler, options, log);
} catch (e) {
Expand All @@ -404,14 +417,6 @@ function startDevServer(webpackOptions, options) {
throw e;
}

['SIGINT', 'SIGTERM'].forEach((sig) => {
process.on(sig, () => {
server.close(() => {
process.exit(); // eslint-disable-line no-process-exit
});
});
});

if (options.socket) {
server.listeningApp.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
Expand Down
21 changes: 21 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,25 @@ describe('CLI', () => {
done();
});
}).timeout(18000);

it('should exit the process when SIGINT is detected, even before the compilation is done', (done) => {
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = path.resolve(__dirname, '../examples/cli/public');
const nodePath = execa.shellSync('which node').stdout;

const proc = execa(nodePath, [cliPath], { cwd: examplePath });

let killed = false;
proc.stdout.on('data', () => {
if (!killed) {
assert(proc.pid !== 0);
proc.kill('SIGINT');
}
killed = true;
});

proc.on('exit', () => {
done();
});
}).timeout(18000);
});

0 comments on commit 334c3a5

Please sign in to comment.