Skip to content

Commit 3aa3f2b

Browse files
authored
Merge pull request #2064 from NativeScript/raikov/ctrl-c
A proper fix for all Ctrl+C issues
2 parents 90b33c7 + 79d757a commit 3aa3f2b

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

lib/declarations.ts

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ interface IAndroidToolsInfoData {
228228

229229
interface ISocketProxyFactory {
230230
createSocketProxy(factory: () => any): IFuture<any>;
231+
stopServer(): void;
231232
}
232233

233234
interface IiOSNotification {

lib/device-sockets/ios/socket-proxy-factory.ts

+23
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ export class SocketProxyFactory implements ISocketProxyFactory {
1010
private $config: IConfiguration,
1111
private $projectData: IProjectData,
1212
private $projectDataService: IProjectDataService,
13+
private $processService: IProcessService,
1314
private $options: IOptions) { }
1415

16+
private _server: any;
17+
private _sockets: net.Socket[] = [];
18+
1519
public createSocketProxy(factory: () => net.Socket): IFuture<any> {
1620
return (() => {
1721
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
@@ -83,6 +87,9 @@ export class SocketProxyFactory implements ISocketProxyFactory {
8387

8488
});
8589

90+
this._server = server;
91+
this.$processService.attachToProcessExitSignals(this, this.stopServer);
92+
8693
this.$logger.info("Opened localhost " + localPort);
8794
return server;
8895
}
@@ -97,6 +104,8 @@ export class SocketProxyFactory implements ISocketProxyFactory {
97104
server.on("connection", (frontendSocket: net.Socket) => {
98105
this.$logger.info("Frontend client connected.");
99106

107+
this._sockets.push(frontendSocket);
108+
100109
frontendSocket.on("end", () => {
101110
this.$logger.info('Frontend socket closed!');
102111
if (!(this.$config.debugLivesync && this.$options.watch)) {
@@ -126,7 +135,21 @@ export class SocketProxyFactory implements ISocketProxyFactory {
126135
this.$logger.info("socket-file-location: " + socketFileLocation);
127136
}
128137

138+
this._server = server;
139+
this.$processService.attachToProcessExitSignals(this, this.stopServer);
140+
129141
return socketFileLocation;
130142
}
143+
144+
public stopServer() {
145+
if (this._server) {
146+
this._server.close();
147+
for (let socket of this._sockets) {
148+
socket.destroy();
149+
}
150+
this._sockets = [];
151+
this._server = null;
152+
}
153+
}
131154
}
132155
$injector.register("socketProxyFactory", SocketProxyFactory);

lib/nativescript-cli.ts

-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ fiber(() => {
2020
let messages: IMessagesService = $injector.resolve("$messagesService");
2121
messages.pathsToMessageJsonFiles = [/* Place client-specific json message file paths here */];
2222

23-
let processService: IProcessService = $injector.resolve("$processService");
24-
processService.attachToProcessExitSignals(this, process.exit);
25-
2623
if (process.argv[2] === "completion") {
2724
commandDispatcher.completeCommand().wait();
2825
} else {

lib/services/ios-debug-service.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ class IOSDebugService implements IDebugService {
3131
private $utils: IUtils,
3232
private $iOSNotification: IiOSNotification,
3333
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
34-
private $socketProxyFactory: ISocketProxyFactory) { }
34+
private $processService: IProcessService,
35+
private $socketProxyFactory: ISocketProxyFactory) {
36+
this.$processService.attachToProcessExitSignals(this, this.debugStop);
37+
}
3538

3639
private _lldbProcess: ChildProcess;
40+
private _sockets: net.Socket[] = [];
3741

3842
public get platform(): string {
3943
return "ios";
@@ -76,6 +80,11 @@ class IOSDebugService implements IDebugService {
7680

7781
public debugStop(): IFuture<void> {
7882
return (() => {
83+
this.$socketProxyFactory.stopServer();
84+
for (let socket of this._sockets) {
85+
socket.destroy();
86+
}
87+
this._sockets = [];
7988
if (this._lldbProcess) {
8089
this._lldbProcess.stdin.write("process detach\n");
8190
this._lldbProcess.kill();
@@ -105,19 +114,19 @@ class IOSDebugService implements IDebugService {
105114
if (lineText && _.startsWith(lineText, this.$projectData.projectId)) {
106115
let pid = _.trimStart(lineText, this.$projectData.projectId + ": ");
107116
this._lldbProcess = this.$childProcess.spawn("lldb", [ "-p", pid]);
108-
this._lldbProcess.stdin.write("process continue\n");
117+
this._lldbProcess.stdin.write("process continue\n");
109118
} else {
110119
process.stdout.write(line + "\n");
111120
}
112121
});
113122

114-
this.wireDebuggerClient(() => net.connect(inspectorBackendPort)).wait();
123+
this.wireDebuggerClient().wait();
115124
}).future<void>()();
116125
}
117126

118127
private emulatorStart(): IFuture<void> {
119128
return (() => {
120-
this.wireDebuggerClient(() => net.connect(inspectorBackendPort)).wait();
129+
this.wireDebuggerClient().wait();
121130

122131
let attachRequestMessage = this.$iOSNotification.attachRequest;
123132

@@ -152,7 +161,7 @@ class IOSDebugService implements IDebugService {
152161
let readyForAttachTimeout = this.getReadyForAttachTimeout(timeout);
153162

154163
this.$iOSSocketRequestExecutor.executeLaunchRequest(device, timeout, readyForAttachTimeout, shouldBreak).wait();
155-
this.wireDebuggerClient(() => device.connectToPort(inspectorBackendPort)).wait();
164+
this.wireDebuggerClient(device).wait();
156165
}).future<void>()();
157166
}
158167

@@ -167,13 +176,17 @@ class IOSDebugService implements IDebugService {
167176
return (() => {
168177
let timeout = this.getReadyForAttachTimeout();
169178
this.$iOSSocketRequestExecutor.executeAttachRequest(device, timeout).wait();
170-
this.wireDebuggerClient(() => device.connectToPort(inspectorBackendPort)).wait();
179+
this.wireDebuggerClient(device).wait();
171180
}).future<void>()();
172181
}
173182

174-
private wireDebuggerClient(factory: () => net.Socket): IFuture<void> {
183+
private wireDebuggerClient(device?: Mobile.IiOSDevice): IFuture<void> {
175184
return (() => {
176-
let socketProxy = this.$socketProxyFactory.createSocketProxy(factory).wait();
185+
let socketProxy = this.$socketProxyFactory.createSocketProxy(() => {
186+
let socket = device ? device.connectToPort(inspectorBackendPort) : net.connect(inspectorBackendPort);
187+
this._sockets.push(socket);
188+
return socket;
189+
}).wait();
177190
this.executeOpenDebuggerClient(socketProxy).wait();
178191
}).future<void>()();
179192
}

0 commit comments

Comments
 (0)