Skip to content

Commit

Permalink
fix(core): fix process being prevented from exiting (#29240)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

The db cleanup handler does not exit the process and thus does not
handle `SIGINT` properly.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Nx processes handle SIGINT properly.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

(cherry picked from commit de8b189)
  • Loading branch information
FrozenPandaz committed Dec 6, 2024
1 parent c071f84 commit 41d0df6
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/nx/bin/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { performance } from 'perf_hooks';
import { setupWorkspaceContext } from '../src/utils/workspace-context';
import { daemonClient } from '../src/daemon/client/client';
import { removeDbConnections } from '../src/utils/db-connection';
import { signalToCode } from '../src/utils/exit-codes';

function main() {
if (
Expand Down Expand Up @@ -275,12 +276,26 @@ const getLatestVersionOfNx = ((fn: () => string) => {
return () => cache || (cache = fn());
})(_getLatestVersionOfNx);

function nxCleanup() {
function nxCleanup(signal?: NodeJS.Signals) {
removeDbConnections();
if (signal) {
process.exit(signalToCode(signal));
} else {
process.exit();
}
}
process.on('exit', nxCleanup);
process.on('SIGINT', nxCleanup);
process.on('SIGTERM', nxCleanup);
process.on('SIGHUP', nxCleanup);

process.on('exit', () => {
nxCleanup();
});
process.on('SIGINT', () => {
nxCleanup('SIGINT');
});
process.on('SIGTERM', () => {
nxCleanup('SIGTERM');
});
process.on('SIGHUP', () => {
nxCleanup('SIGHUP');
});

main();

0 comments on commit 41d0df6

Please sign in to comment.