Skip to content

Commit

Permalink
[now-static-build] Exit dev server child processes upon SIGINT/SIGTERM (
Browse files Browse the repository at this point in the history
#3136)

Explicitly send the SIGINT / SIGTERM signal to `now dev` server child processes, so that they are not left running when running the now-dev unit tests.

Related to #3113 which has hanging unit tests that never "complete".
  • Loading branch information
TooTallNate committed Oct 21, 2019
1 parent e62b9e8 commit 5767e9e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/now-static-build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import ms from 'ms';
import path from 'path';
import spawn from 'cross-spawn';
import getPort from 'get-port';
import { SpawnOptions } from 'child_process';
import isPortReachable from 'is-port-reachable';
import { ChildProcess, SpawnOptions } from 'child_process';
import { existsSync, readFileSync, statSync, readdirSync } from 'fs';
import { frameworks, Framework } from './frameworks';
import {
Expand Down Expand Up @@ -102,7 +102,20 @@ function getCommand(pkg: PackageJson, cmd: string, { zeroConfig }: Config) {

export const version = 2;

const nowDevScriptPorts = new Map();
const nowDevScriptPorts = new Map<string, number>();
const nowDevChildProcesses = new Set<ChildProcess>();

['SIGINT', 'SIGTERM'].forEach(signal => {
process.once(signal as NodeJS.Signals, () => {
for (const child of nowDevChildProcesses) {
debug(
`Got ${signal}, killing dev server child process (pid=${child.pid})`
);
process.kill(child.pid, signal);
}
process.exit(0);
});
});

const getDevRoute = (srcBase: string, devPort: number, route: Route) => {
const basic: Route = {
Expand Down Expand Up @@ -272,8 +285,9 @@ export async function build({
env: { ...process.env, PORT: String(devPort) },
};

const child = spawn('yarn', ['run', devScript], opts);
const child: ChildProcess = spawn('yarn', ['run', devScript], opts);
child.on('exit', () => nowDevScriptPorts.delete(entrypoint));
nowDevChildProcesses.add(child);

// Now wait for the server to have listened on `$PORT`, after which we
// will ProxyPass any requests to that development server that come in
Expand Down

0 comments on commit 5767e9e

Please sign in to comment.