Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bin): handle process signals correctly when the server isn't ready yet #1432

Merged
merged 1 commit into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});