Skip to content

Commit 19fa242

Browse files
fix: stop handling SIGINT signal
When Ctrl+C is used in terminal, CLI receives SIGINT signal and tries to clear its resources. In Node.js, when you handle SIGINT signal, you'll have to clean all resources on your own. This leads to different problems, for example in case you press Ctrl+C during doctor execution, CLI will only kill the doctor process, but will continue working. Instead of handling the SIGINT signal, let the OS handle it on its own. This works quite well except one thing - any package that is required may add handler for SIGINT signal and this will break the idea. So repace the `process.on` function with our own one which does not allow adding handler for SIGINT signal.
1 parent 3267539 commit 19fa242

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

lib/common/services/process-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class ProcessService implements IProcessService {
2-
private static PROCESS_EXIT_SIGNALS = ["exit", "SIGINT", "SIGTERM"];
2+
private static PROCESS_EXIT_SIGNALS = ["exit", "SIGTERM"];
33
private _listeners: IListener[];
44

55
public get listenersCount(): number {

lib/common/test/unit-tests/process-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Yok } from "../../yok";
22
import { ProcessService } from "../../services/process-service";
33
import { assert } from "chai";
44

5-
const processExitSignals = ["exit", "SIGINT", "SIGTERM"];
5+
const processExitSignals = ["exit", "SIGTERM"];
66
const emptyFunction = () => { /* no implementation required */ };
77
function createTestInjector(): IInjector {
88
const testInjector = new Yok();

lib/nativescript-cli.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
require("./bootstrap");
2+
23
import { EOL } from "os";
34
import * as shelljs from "shelljs";
45
shelljs.config.silent = true;
56
shelljs.config.fatal = true;
67
import { installUncaughtExceptionListener } from "./common/errors";
8+
import { settlePromises } from "./common/helpers";
79
installUncaughtExceptionListener(process.exit.bind(process, ErrorCodes.UNCAUGHT));
810

9-
import { settlePromises } from "./common/helpers";
11+
const logger: ILogger = $injector.resolve("logger");
12+
const originalProcessOn = process.on;
13+
14+
process.on = (event: string, listener: any): any => {
15+
if (event === "SIGINT") {
16+
logger.trace(new Error(`Trying to handle SIGINT event. CLI overrides this behavior and does not allow handling SIGINT as this causes issues with Ctrl + C in terminal`).stack);
17+
} else {
18+
return originalProcessOn.apply(process, [event, listener]);
19+
}
20+
};
1021

1122
/* tslint:disable:no-floating-promises */
1223
(async () => {
1324
const config: Config.IConfig = $injector.resolve("$config");
1425
const err: IErrors = $injector.resolve("$errors");
1526
err.printCallStack = config.DEBUG;
1627

17-
const logger: ILogger = $injector.resolve("logger");
18-
1928
const extensibilityService: IExtensibilityService = $injector.resolve("extensibilityService");
2029
try {
2130
await settlePromises<IExtensionData>(extensibilityService.loadExtensions());

0 commit comments

Comments
 (0)