From 79d757a9b17992b908dcb971348ff3be8fc312dc Mon Sep 17 00:00:00 2001 From: Tsvetan Raikov Date: Thu, 15 Sep 2016 18:41:24 +0300 Subject: [PATCH] Fixing Ctrl+C --- lib/common | 2 +- lib/declarations.ts | 1 + .../ios/socket-proxy-factory.ts | 23 +++++++++++++++ lib/nativescript-cli.ts | 3 -- lib/services/ios-debug-service.ts | 29 ++++++++++++++----- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/common b/lib/common index 6018713562..48801e2eaa 160000 --- a/lib/common +++ b/lib/common @@ -1 +1 @@ -Subproject commit 601871356256131e596d25184319a4fe973a5e0e +Subproject commit 48801e2eaa7ecdfde9211f68b2eb1552c1ad57dd diff --git a/lib/declarations.ts b/lib/declarations.ts index b2be180cf1..bfc22329fc 100644 --- a/lib/declarations.ts +++ b/lib/declarations.ts @@ -228,6 +228,7 @@ interface IAndroidToolsInfoData { interface ISocketProxyFactory { createSocketProxy(factory: () => any): IFuture; + stopServer(): void; } interface IiOSNotification { diff --git a/lib/device-sockets/ios/socket-proxy-factory.ts b/lib/device-sockets/ios/socket-proxy-factory.ts index 21317f5322..0d5575ac8d 100644 --- a/lib/device-sockets/ios/socket-proxy-factory.ts +++ b/lib/device-sockets/ios/socket-proxy-factory.ts @@ -10,8 +10,12 @@ export class SocketProxyFactory implements ISocketProxyFactory { private $config: IConfiguration, private $projectData: IProjectData, private $projectDataService: IProjectDataService, + private $processService: IProcessService, private $options: IOptions) { } + private _server: any; + private _sockets: net.Socket[] = []; + public createSocketProxy(factory: () => net.Socket): IFuture { return (() => { let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback); @@ -83,6 +87,9 @@ export class SocketProxyFactory implements ISocketProxyFactory { }); + this._server = server; + this.$processService.attachToProcessExitSignals(this, this.stopServer); + this.$logger.info("Opened localhost " + localPort); return server; } @@ -97,6 +104,8 @@ export class SocketProxyFactory implements ISocketProxyFactory { server.on("connection", (frontendSocket: net.Socket) => { this.$logger.info("Frontend client connected."); + this._sockets.push(frontendSocket); + frontendSocket.on("end", () => { this.$logger.info('Frontend socket closed!'); if (!(this.$config.debugLivesync && this.$options.watch)) { @@ -126,7 +135,21 @@ export class SocketProxyFactory implements ISocketProxyFactory { this.$logger.info("socket-file-location: " + socketFileLocation); } + this._server = server; + this.$processService.attachToProcessExitSignals(this, this.stopServer); + return socketFileLocation; } + + public stopServer() { + if (this._server) { + this._server.close(); + for (let socket of this._sockets) { + socket.destroy(); + } + this._sockets = []; + this._server = null; + } + } } $injector.register("socketProxyFactory", SocketProxyFactory); diff --git a/lib/nativescript-cli.ts b/lib/nativescript-cli.ts index b32512d1b2..8972dd1f9f 100644 --- a/lib/nativescript-cli.ts +++ b/lib/nativescript-cli.ts @@ -20,9 +20,6 @@ fiber(() => { let messages: IMessagesService = $injector.resolve("$messagesService"); messages.pathsToMessageJsonFiles = [/* Place client-specific json message file paths here */]; - let processService: IProcessService = $injector.resolve("$processService"); - processService.attachToProcessExitSignals(this, process.exit); - if (process.argv[2] === "completion") { commandDispatcher.completeCommand().wait(); } else { diff --git a/lib/services/ios-debug-service.ts b/lib/services/ios-debug-service.ts index 26210e842b..82449daecb 100644 --- a/lib/services/ios-debug-service.ts +++ b/lib/services/ios-debug-service.ts @@ -31,9 +31,13 @@ class IOSDebugService implements IDebugService { private $utils: IUtils, private $iOSNotification: IiOSNotification, private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor, - private $socketProxyFactory: ISocketProxyFactory) { } + private $processService: IProcessService, + private $socketProxyFactory: ISocketProxyFactory) { + this.$processService.attachToProcessExitSignals(this, this.debugStop); + } private _lldbProcess: ChildProcess; + private _sockets: net.Socket[] = []; public get platform(): string { return "ios"; @@ -76,6 +80,11 @@ class IOSDebugService implements IDebugService { public debugStop(): IFuture { return (() => { + this.$socketProxyFactory.stopServer(); + for (let socket of this._sockets) { + socket.destroy(); + } + this._sockets = []; if (this._lldbProcess) { this._lldbProcess.stdin.write("process detach\n"); this._lldbProcess.kill(); @@ -105,19 +114,19 @@ class IOSDebugService implements IDebugService { if (lineText && _.startsWith(lineText, this.$projectData.projectId)) { let pid = _.trimStart(lineText, this.$projectData.projectId + ": "); this._lldbProcess = this.$childProcess.spawn("lldb", [ "-p", pid]); - this._lldbProcess.stdin.write("process continue\n"); + this._lldbProcess.stdin.write("process continue\n"); } else { process.stdout.write(line + "\n"); } }); - this.wireDebuggerClient(() => net.connect(inspectorBackendPort)).wait(); + this.wireDebuggerClient().wait(); }).future()(); } private emulatorStart(): IFuture { return (() => { - this.wireDebuggerClient(() => net.connect(inspectorBackendPort)).wait(); + this.wireDebuggerClient().wait(); let attachRequestMessage = this.$iOSNotification.attachRequest; @@ -152,7 +161,7 @@ class IOSDebugService implements IDebugService { let readyForAttachTimeout = this.getReadyForAttachTimeout(timeout); this.$iOSSocketRequestExecutor.executeLaunchRequest(device, timeout, readyForAttachTimeout, shouldBreak).wait(); - this.wireDebuggerClient(() => device.connectToPort(inspectorBackendPort)).wait(); + this.wireDebuggerClient(device).wait(); }).future()(); } @@ -167,13 +176,17 @@ class IOSDebugService implements IDebugService { return (() => { let timeout = this.getReadyForAttachTimeout(); this.$iOSSocketRequestExecutor.executeAttachRequest(device, timeout).wait(); - this.wireDebuggerClient(() => device.connectToPort(inspectorBackendPort)).wait(); + this.wireDebuggerClient(device).wait(); }).future()(); } - private wireDebuggerClient(factory: () => net.Socket): IFuture { + private wireDebuggerClient(device?: Mobile.IiOSDevice): IFuture { return (() => { - let socketProxy = this.$socketProxyFactory.createSocketProxy(factory).wait(); + let socketProxy = this.$socketProxyFactory.createSocketProxy(() => { + let socket = device ? device.connectToPort(inspectorBackendPort) : net.connect(inspectorBackendPort); + this._sockets.push(socket); + return socket; + }).wait(); this.executeOpenDebuggerClient(socketProxy).wait(); }).future()(); }