Skip to content

Commit debb5ce

Browse files
committed
Fix SIGINT handling to work even if server not yet ready
1 parent 3c1ff11 commit debb5ce

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

bin/webpack-dev-server.js

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

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

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

396-
let server;
397411
try {
398412
server = new Server(compiler, options, log);
399413
} catch (e) {
@@ -405,14 +419,6 @@ function startDevServer(webpackOptions, options) {
405419
throw e;
406420
}
407421

408-
['SIGINT', 'SIGTERM'].forEach((sig) => {
409-
process.on(sig, () => {
410-
server.close(() => {
411-
process.exit(); // eslint-disable-line no-process-exit
412-
});
413-
});
414-
});
415-
416422
if (options.socket) {
417423
server.listeningApp.on('error', (e) => {
418424
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', (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)