Skip to content

Commit 1da83d1

Browse files
author
Tsvetan Raikov
committed
Fixing Ctrl+C
1 parent 9b31d4d commit 1da83d1

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
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

+31-6
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,27 @@ 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(() => {
124+
let socket = net.connect(inspectorBackendPort);
125+
this._sockets.push(socket);
126+
return socket;
127+
}).wait();
115128
}).future<void>()();
116129
}
117130

118131
private emulatorStart(): IFuture<void> {
119132
return (() => {
120-
this.wireDebuggerClient(() => net.connect(inspectorBackendPort)).wait();
133+
this.wireDebuggerClient(() => {
134+
let socket = net.connect(inspectorBackendPort);
135+
this._sockets.push(socket);
136+
return socket;
137+
}).wait();
121138

122139
let attachRequestMessage = this.$iOSNotification.attachRequest;
123140

@@ -152,7 +169,11 @@ class IOSDebugService implements IDebugService {
152169
let readyForAttachTimeout = this.getReadyForAttachTimeout(timeout);
153170

154171
this.$iOSSocketRequestExecutor.executeLaunchRequest(device, timeout, readyForAttachTimeout, shouldBreak).wait();
155-
this.wireDebuggerClient(() => device.connectToPort(inspectorBackendPort)).wait();
172+
this.wireDebuggerClient(() => {
173+
let socket = device.connectToPort(inspectorBackendPort);
174+
this._sockets.push(socket);
175+
return socket;
176+
}).wait();
156177
}).future<void>()();
157178
}
158179

@@ -167,7 +188,11 @@ class IOSDebugService implements IDebugService {
167188
return (() => {
168189
let timeout = this.getReadyForAttachTimeout();
169190
this.$iOSSocketRequestExecutor.executeAttachRequest(device, timeout).wait();
170-
this.wireDebuggerClient(() => device.connectToPort(inspectorBackendPort)).wait();
191+
this.wireDebuggerClient(() => {
192+
let socket = device.connectToPort(inspectorBackendPort);
193+
this._sockets.push(socket);
194+
return socket;
195+
}).wait();
171196
}).future<void>()();
172197
}
173198

0 commit comments

Comments
 (0)