Skip to content

Commit 108a075

Browse files
committed
fix(core): add registerExitHandler utility
1 parent 76b3f57 commit 108a075

File tree

43 files changed

+138
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+138
-174
lines changed

packages/detox/src/executors/build/build.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export function runCliBuild(
4545

4646
// Ensure the child process is killed when the parent exits
4747
process.on('exit', () => childProcess.kill());
48-
process.on('SIGTERM', () => childProcess.kill());
4948

5049
childProcess.on('error', (err) => {
5150
reject(err);

packages/detox/src/executors/test/test.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ function runCliTest(
6666

6767
// Ensure the child process is killed when the parent exits
6868
process.on('exit', () => childProcess.kill());
69-
process.on('SIGTERM', () => childProcess.kill());
7069

7170
childProcess.on('error', (err) => {
7271
reject(err);

packages/esbuild/src/executors/esbuild/esbuild.impl.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export async function* esbuildExecutor(
173173
})
174174
);
175175

176-
registerCleanupCallback(() => {
176+
process.once('exit', () => {
177177
assetsResult?.stop();
178178
packageJsonResult?.stop();
179179
disposeFns.forEach((fn) => fn());
@@ -269,17 +269,4 @@ async function runTypeCheck(
269269
return { errors, warnings };
270270
}
271271

272-
function registerCleanupCallback(callback: () => void) {
273-
const wrapped = () => {
274-
callback();
275-
process.off('SIGINT', wrapped);
276-
process.off('SIGTERM', wrapped);
277-
process.off('exit', wrapped);
278-
};
279-
280-
process.on('SIGINT', wrapped);
281-
process.on('SIGTERM', wrapped);
282-
process.on('exit', wrapped);
283-
}
284-
285272
export default esbuildExecutor;

packages/expo/src/executors/build-list/build-list.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export function runCliBuildList(
5252

5353
// Ensure the child process is killed when the parent exits
5454
process.on('exit', () => childProcess.kill());
55-
process.on('SIGTERM', () => childProcess.kill());
5655

5756
let output = '';
5857
childProcess.stdout.on('data', (message) => {

packages/expo/src/executors/build/build.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ function runCliBuild(
6868

6969
// Ensure the child process is killed when the parent exits
7070
process.on('exit', () => childProcess.kill());
71-
process.on('SIGTERM', () => childProcess.kill());
7271

7372
childProcess.on('error', (err) => {
7473
reject(err);

packages/expo/src/executors/export/export.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function exportAsync(
4848

4949
// Ensure the child process is killed when the parent exits
5050
process.on('exit', () => childProcess.kill());
51-
process.on('SIGTERM', () => childProcess.kill());
5251

5352
childProcess.on('error', (err) => {
5453
reject(err);

packages/expo/src/executors/prebuild/prebuild.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export function prebuildAsync(
5555

5656
// Ensure the child process is killed when the parent exits
5757
process.on('exit', () => childProcess.kill());
58-
process.on('SIGTERM', () => childProcess.kill());
5958

6059
childProcess.on('error', (err) => {
6160
reject(err);

packages/expo/src/executors/run/run.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ function runCliRun(
7070

7171
// Ensure the child process is killed when the parent exits
7272
process.on('exit', () => childProcess.kill());
73-
process.on('SIGTERM', () => childProcess.kill());
7473

7574
childProcess.on('error', (err) => {
7675
reject(err);

packages/expo/src/executors/start/start.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ function startAsync(
5656

5757
// Ensure the child process is killed when the parent exits
5858
process.on('exit', () => childProcess.kill());
59-
process.on('SIGTERM', () => childProcess.kill());
6059

6160
childProcess.on('error', (err) => {
6261
reject(err);

packages/expo/src/executors/submit/submit.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function runCliSubmit(
4747

4848
// Ensure the child process is killed when the parent exits
4949
process.on('exit', () => childProcess.kill());
50-
process.on('SIGTERM', () => childProcess.kill());
5150

5251
childProcess.on('error', (err) => {
5352
reject(err);

packages/expo/src/executors/update/update.impl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function runCliUpdate(
4747

4848
// Ensure the child process is killed when the parent exits
4949
process.on('exit', () => childProcess.kill());
50-
process.on('SIGTERM', () => childProcess.kill());
5150

5251
childProcess.on('error', (err) => {
5352
reject(err);

packages/js/src/executors/node/node.impl.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { killTree } from './lib/kill-tree';
2222
import { fileExists } from 'nx/src/utils/fileutils';
2323
import { getRelativeDirectoryToProjectRoot } from '../../utils/get-main-file-dir';
2424
import { interpolate } from 'nx/src/tasks-runner/utils';
25+
import { registerExitHandler } from 'nx/src/utils/signals';
2526

2627
interface ActiveTask {
2728
id: string;
@@ -233,18 +234,11 @@ export async function* nodeExecutor(
233234
}
234235
};
235236

236-
process.on('SIGTERM', async () => {
237-
await stopAllTasks('SIGTERM');
238-
process.exit(128 + 15);
239-
});
240-
process.on('SIGINT', async () => {
241-
await stopAllTasks('SIGINT');
242-
process.exit(128 + 2);
243-
});
244-
process.on('SIGHUP', async () => {
245-
await stopAllTasks('SIGHUP');
246-
process.exit(128 + 1);
247-
});
237+
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP'] as const) {
238+
registerExitHandler('SIGTERM', async () => {
239+
await stopAllTasks(signal);
240+
});
241+
}
248242

249243
registerCleanup(async () => {
250244
await stopAllTasks('SIGTERM');

packages/js/src/executors/swc/swc.impl.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { compileSwc, compileSwcWatch } from '../../utils/swc/compile-swc';
2828
import { getSwcrcPath } from '../../utils/swc/get-swcrc-path';
2929
import { generateTmpSwcrc } from '../../utils/swc/inline';
3030
import { isUsingTsSolutionSetup } from '../../utils/typescript/ts-solution-setup';
31+
import { registerExitHandler } from 'nx/src/utils/signals';
3132

3233
function normalizeOptions(
3334
options: SwcExecutorOptions,
@@ -186,8 +187,8 @@ export async function* swcExecutor(
186187

187188
if (options.watch) {
188189
let disposeFn: () => void;
189-
process.on('SIGINT', () => disposeFn());
190-
process.on('SIGTERM', () => disposeFn());
190+
registerExitHandler('SIGINT', (s) => disposeFn());
191+
registerExitHandler('SIGTERM', (s) => disposeFn());
191192

192193
return yield* compileSwcWatch(context, options, async () => {
193194
const assetResult = await copyAssets(options, context);

packages/js/src/executors/tsc/tsc.batch-impl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
watchTaskProjectsPackageJsonFileChanges,
2828
} from './lib/batch';
2929
import { createEntryPoints } from '../../utils/package-json/create-entry-points';
30+
import { registerExitHandler } from 'nx/src/utils/signals';
3031

3132
export async function* tscBatchExecutor(
3233
taskGraph: TaskGraph,
@@ -150,13 +151,12 @@ export async function* tscBatchExecutor(
150151
}
151152
);
152153

153-
const handleTermination = async (exitCode: number) => {
154+
const handleTermination = async () => {
154155
watchAssetsChangesDisposer();
155156
watchProjectsChangesDisposer();
156-
process.exit(exitCode);
157157
};
158-
process.on('SIGINT', () => handleTermination(128 + 2));
159-
process.on('SIGTERM', () => handleTermination(128 + 15));
158+
registerExitHandler('SIGINT', (s) => handleTermination());
159+
registerExitHandler('SIGTERM', (s) => handleTermination());
160160

161161
return yield* mapAsyncIterable(typescriptCompilation, async (iterator) => {
162162
// drain the iterator, we don't use the results

packages/js/src/executors/tsc/tsc.impl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { watchForSingleFileChanges } from '../../utils/watch-for-single-file-cha
2424
import { getCustomTrasformersFactory, normalizeOptions } from './lib';
2525
import { readTsConfig } from '../../utils/typescript/ts-config';
2626
import { createEntryPoints } from '../../utils/package-json/create-entry-points';
27+
import { registerExitHandler } from 'nx/src/utils/signals';
2728

2829
export function determineModuleFormatFromTsConfig(
2930
absolutePathToTsConfig: string
@@ -169,14 +170,13 @@ export async function* tscExecutor(
169170
)
170171
);
171172
}
172-
const handleTermination = async (exitCode: number) => {
173+
const handleTermination = async () => {
173174
await typescriptCompilation.close();
174175
disposeWatchAssetChanges();
175176
disposePackageJsonChanges?.();
176-
process.exit(exitCode);
177177
};
178-
process.on('SIGINT', () => handleTermination(128 + 2));
179-
process.on('SIGTERM', () => handleTermination(128 + 15));
178+
registerExitHandler('SIGINT', (s) => handleTermination());
179+
registerExitHandler('SIGTERM', (s) => handleTermination());
180180
}
181181

182182
return yield* typescriptCompilation.iterator;

packages/js/src/executors/verdaccio/verdaccio.impl.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ export async function verdaccioExecutor(
5757
}
5858
};
5959
process.on('exit', processExitListener);
60-
process.on('SIGTERM', processExitListener);
61-
process.on('SIGINT', processExitListener);
62-
process.on('SIGHUP', processExitListener);
6360

6461
try {
6562
await startVerdaccio(options, context.root);

packages/js/src/utils/swc/compile-swc.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ export async function* compileSwcWatch(
229229
swcWatcher.stdout.on('data', stdoutOnData);
230230
swcWatcher.stderr.on('data', stderrOnData);
231231

232-
process.on('SIGINT', processOnExit);
233-
process.on('SIGTERM', processOnExit);
234232
process.on('exit', processOnExit);
235233

236234
swcWatcher.on('exit', watcherOnExit);

packages/module-federation/src/executors/utils/build-static-remotes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export async function buildStaticRemotes(
8787
res();
8888
}
8989
});
90-
process.on('SIGTERM', () => staticProcess.kill('SIGTERM'));
9190
process.on('exit', () => staticProcess.kill('SIGTERM'));
9291
});
9392

packages/module-federation/src/plugins/nx-module-federation-plugin/rspack/nx-module-federation-ssr-dev-server-plugin.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ export class NxModuleFederationSSRDevServerPlugin
130130
process.on('exit', () => {
131131
this.devServerProcess?.kill('SIGKILL');
132132
});
133-
process.on('SIGINT', () => {
134-
this.devServerProcess?.kill('SIGKILL');
135-
});
136133
callback();
137134
});
138135
}

packages/module-federation/src/plugins/utils/build-static-remotes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export async function buildStaticRemotes(
7676
res();
7777
}
7878
});
79-
process.on('SIGTERM', () => staticProcess.kill('SIGTERM'));
8079
process.on('exit', () => staticProcess.kill('SIGTERM'));
8180
});
8281

packages/module-federation/src/plugins/utils/start-remote-proxies.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export function startRemoteProxies(
5555
const proxyServer = (sslCert ? https : http)
5656
.createServer({ cert: sslCert, key: sslKey }, expressProxy)
5757
.listen(appConfig.port);
58-
process.on('SIGTERM', () => proxyServer.close());
5958
process.on('exit', () => proxyServer.close());
6059
}
6160
console.info(`NX Static remotes proxies started successfully`);

packages/module-federation/src/plugins/utils/start-static-remotes-file-server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,5 @@ export function startStaticRemotesFileServer(
5858
},
5959
}
6060
);
61-
process.on('SIGTERM', () => httpServerProcess.kill('SIGTERM'));
6261
process.on('exit', () => httpServerProcess.kill('SIGTERM'));
6362
}

packages/module-federation/src/utils/start-remote-proxies.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export function startRemoteProxies(
4141
const proxyServer = (sslCert ? https : http)
4242
.createServer({ cert: sslCert, key: sslKey }, expressProxy)
4343
.listen(staticRemotesConfig.config[app].port);
44-
process.on('SIGTERM', () => proxyServer.close());
4544
process.on('exit', () => proxyServer.close());
4645
}
4746
logger.info(`NX Static remotes proxies started successfully`);

packages/module-federation/src/utils/start-ssr-remote-proxies.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export function startSsrRemoteProxies(
5858
const proxyServer = (sslCert ? https : http)
5959
.createServer({ cert: sslCert, key: sslKey }, expressProxy)
6060
.listen(staticRemotesConfig.config[app].port);
61-
process.on('SIGTERM', () => proxyServer.close());
6261
process.on('exit', () => proxyServer.close());
6362
}
6463
logger.info(`Nx SSR Static remotes proxies started successfully`);

packages/next/src/executors/build/build.impl.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { NextBuildBuilderOptions } from '../../utils/types';
2020
import { ChildProcess, fork } from 'child_process';
2121
import { createCliOptions } from '../../utils/create-cli-options';
2222
import { signalToCode } from 'nx/src/utils/exit-codes';
23+
import { registerExitHandler } from 'nx/src/utils/signals';
2324

2425
let childProcess: ChildProcess;
2526

@@ -157,12 +158,11 @@ function runCliBuild(
157158
// Ensure the child process is killed when the parent exits
158159
process.on('exit', () => childProcess.kill());
159160

160-
process.on('SIGTERM', (signal) => {
161-
reject({ code: signalToCode(signal), signal });
162-
});
163-
process.on('SIGINT', (signal) => {
164-
reject({ code: signalToCode(signal), signal });
165-
});
161+
for (const signal of ['SIGINT', 'SIGTERM'] as const) {
162+
registerExitHandler(signal, () => {
163+
reject({ code: signalToCode(signal), signal });
164+
});
165+
}
166166

167167
childProcess.on('error', (err) => {
168168
reject({ error: err });

packages/next/src/executors/server/server.impl.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ export default async function* serveExecutor(
8282
}
8383
};
8484
process.on('exit', () => killServer());
85-
process.on('SIGINT', () => killServer());
86-
process.on('SIGTERM', () => killServer());
87-
process.on('SIGHUP', () => killServer());
8885

8986
await waitForPortOpen(options.port, { host: options.hostname });
9087

packages/nx/bin/nx.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { performance } from 'perf_hooks';
2121
import { setupWorkspaceContext } from '../src/utils/workspace-context';
2222
import { daemonClient } from '../src/daemon/client/client';
2323
import { removeDbConnections } from '../src/utils/db-connection';
24-
import { signalToCode } from '../src/utils/exit-codes';
24+
import { registerExitHandler } from '../src/utils/signals';
2525

2626
// In case Nx Cloud forcibly exits while the TUI is running, ensure the terminal is restored etc.
2727
process.on('exit', (...args) => {
@@ -283,26 +283,13 @@ const getLatestVersionOfNx = ((fn: () => string) => {
283283
return () => cache || (cache = fn());
284284
})(_getLatestVersionOfNx);
285285

286-
function nxCleanup(signal?: NodeJS.Signals) {
286+
function nxCleanup() {
287287
removeDbConnections();
288-
if (signal) {
289-
process.exit(signalToCode(signal));
290-
} else {
291-
process.exit();
292-
}
293288
}
294289

295-
process.on('exit', () => {
296-
nxCleanup();
297-
});
298-
process.on('SIGINT', () => {
299-
nxCleanup('SIGINT');
300-
});
301-
process.on('SIGTERM', () => {
302-
nxCleanup('SIGTERM');
303-
});
304-
process.on('SIGHUP', () => {
305-
nxCleanup('SIGHUP');
306-
});
290+
registerExitHandler('SIGINT', nxCleanup);
291+
registerExitHandler('SIGTERM', nxCleanup);
292+
registerExitHandler('SIGHUP', nxCleanup);
293+
registerExitHandler('exit', nxCleanup);
307294

308295
main();

packages/nx/src/command-line/graph/graph.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { ConfigurationSourceMaps } from '../../project-graph/utils/project-confi
5757
import { createTaskHasher } from '../../hasher/create-task-hasher';
5858
import { ProjectGraphError } from '../../project-graph/error-types';
5959
import { isNxCloudUsed } from '../../utils/nx-cloud-utils';
60+
import { registerExitHandler } from '../../utils/signals';
6061

6162
export interface GraphError {
6263
message: string;
@@ -667,14 +668,18 @@ async function startServer(
667668
}
668669
});
669670

670-
const handleTermination = async (exitCode: number) => {
671+
const handleTermination = async () => {
671672
if (unregisterFileWatcher) {
672673
unregisterFileWatcher();
673674
}
674-
process.exit(exitCode);
675675
};
676-
process.on('SIGINT', () => handleTermination(128 + 2));
677-
process.on('SIGTERM', () => handleTermination(128 + 15));
676+
677+
registerExitHandler('SIGINT', () => {
678+
handleTermination();
679+
});
680+
registerExitHandler('SIGTERM', () => {
681+
handleTermination();
682+
});
678683

679684
return new Promise<{ app: Server; url: URL }>((res) => {
680685
app.listen(port, host, () => {

0 commit comments

Comments
 (0)