Skip to content

Commit

Permalink
Do not throw an error when killing the verdaccio process (#7695)
Browse files Browse the repository at this point in the history
Killing the verdaccio process throws an error because the disconnect event emits when the process is killed. We throw an error on a disconnect to catch any unexpected verdaccio disconnections.

Fix this by deregistering the disconnect handler before killing the verdaccio process.
  • Loading branch information
mattsoulanille authored May 24, 2023
1 parent f7fa5af commit b122429
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions scripts/publish-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,15 @@ async function main() {
// Build and publish all packages to a local Verdaccio repo for staging.
console.log(
chalk.magenta.bold('~~~ Staging packages locally in Verdaccio ~~~'));
const verdaccio = await runVerdaccio();
const killVerdaccio = await runVerdaccio();
try {
for (const pkg of packages) {
await publish(pkg, VERDACCIO_REGISTRY);
}
} finally {
// Make sure to kill the verdaccio server before exiting even if publish
// throws an error. Otherwise, it blocks the port for the next run.
verdaccio.kill();
killVerdaccio();
}

if (args.dry) {
Expand Down
24 changes: 16 additions & 8 deletions scripts/release-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as readline from 'readline';
import * as shell from 'shelljs';
import rimraf from 'rimraf';
import * as path from 'path';
import {ChildProcess, fork} from 'child_process';
import {fork} from 'child_process';

export interface Phase {
// The list of packages that will be updated with this change.
Expand Down Expand Up @@ -615,7 +615,7 @@ export function memoize<I, O>(f: (arg: I) => Promise<O>): (arg: I) => Promise<O>
}
}
export async function runVerdaccio(): Promise<ChildProcess> {
export async function runVerdaccio(): Promise<() => void> {
// Remove the verdaccio package store.
// TODO(mattsoulanille): Move the verdaccio storage and config file here
// once the nightly verdaccio tests are handled by this script.
Expand All @@ -625,7 +625,8 @@ export async function runVerdaccio(): Promise<ChildProcess> {
// messaging works and verdaccio can tell node that it has started.
// https://verdaccio.org/docs/verdaccio-programmatically/#using-fork-from-child_process-module
const verdaccioBin = require.resolve('verdaccio/bin/verdaccio');
const serverProcess = fork(verdaccioBin, ['--config=e2e/scripts/verdaccio.yaml']);
const config = path.join(__dirname, '../e2e/scripts/verdaccio.yaml');
const serverProcess = fork(verdaccioBin, [`--config=${config}`]);
const ready = new Promise<void>((resolve, reject) => {
const timeLimitMilliseconds = 30_000;
console.log(`Waiting ${timeLimitMilliseconds / 1000} seconds for ` +
Expand All @@ -647,15 +648,22 @@ export async function runVerdaccio(): Promise<ChildProcess> {
serverProcess.on('error', (err: unknown) => {
throw new Error(`Verdaccio error: ${err}`);
});
serverProcess.on('disconnect', (err: unknown) => {
throw new Error(`Verdaccio disconnected: ${err}`);
});
const onUnexpectedDisconnect = (err: unknown) => {
throw new Error(`Verdaccio process unexpectedly disconnected: ${err}`);
};
serverProcess.on('disconnect', onUnexpectedDisconnect);
const killVerdaccio = () => {
serverProcess.off('disconnect', onUnexpectedDisconnect);
serverProcess.kill();
};
// Kill verdaccio when node exits.
process.on('exit', () => {serverProcess.kill();});
process.on('exit', killVerdaccio);
await ready;
return serverProcess;
return killVerdaccio;
}
/**
Expand Down

0 comments on commit b122429

Please sign in to comment.