Skip to content

Commit 334c3a5

Browse files
edi9999michael-ciniawsky
authored andcommitted
fix(bin): handle process signals correctly when the server isn't ready yet (#1432)
1 parent e2220c4 commit 334c3a5

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

bin/webpack-dev-server.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
1515
const createDomain = require('../lib/util/createDomain'); // eslint-disable-line
1616
const createLog = require('../lib/createLog');
1717

18+
let server;
19+
20+
['SIGINT', 'SIGTERM'].forEach((sig) => {
21+
process.on(sig, () => {
22+
if (server) {
23+
server.close(() => {
24+
process.exit(); // eslint-disable-line no-process-exit
25+
});
26+
} else {
27+
process.exit(); // eslint-disable-line no-process-exit
28+
}
29+
});
30+
});
31+
1832
// Prefer the local installation of webpack-dev-server
1933
if (importLocal(__filename)) {
2034
debug('Using local install of webpack-dev-server');
@@ -392,7 +406,6 @@ function startDevServer(webpackOptions, options) {
392406

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

395-
let server;
396409
try {
397410
server = new Server(compiler, options, log);
398411
} catch (e) {
@@ -404,14 +417,6 @@ function startDevServer(webpackOptions, options) {
404417
throw e;
405418
}
406419

407-
['SIGINT', 'SIGTERM'].forEach((sig) => {
408-
process.on(sig, () => {
409-
server.close(() => {
410-
process.exit(); // eslint-disable-line no-process-exit
411-
});
412-
});
413-
});
414-
415420
if (options.socket) {
416421
server.listeningApp.on('error', (e) => {
417422
if (e.code === 'EADDRINUSE') {

test/cli.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,25 @@ describe('CLI', () => {
4646
done();
4747
});
4848
}).timeout(18000);
49+
50+
it('should exit the process when SIGINT is detected, even before the compilation is done', (done) => {
51+
const cliPath = path.resolve(__dirname, '../bin/webpack-dev-server.js');
52+
const examplePath = path.resolve(__dirname, '../examples/cli/public');
53+
const nodePath = execa.shellSync('which node').stdout;
54+
55+
const proc = execa(nodePath, [cliPath], { cwd: examplePath });
56+
57+
let killed = false;
58+
proc.stdout.on('data', () => {
59+
if (!killed) {
60+
assert(proc.pid !== 0);
61+
proc.kill('SIGINT');
62+
}
63+
killed = true;
64+
});
65+
66+
proc.on('exit', () => {
67+
done();
68+
});
69+
}).timeout(18000);
4970
});

0 commit comments

Comments
 (0)