Skip to content

A proper fix for all Ctrl+C issues #2064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ interface IAndroidToolsInfoData {

interface ISocketProxyFactory {
createSocketProxy(factory: () => any): IFuture<any>;
stopServer(): void;
}

interface IiOSNotification {
Expand Down
23 changes: 23 additions & 0 deletions lib/device-sockets/ios/socket-proxy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
return (() => {
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
3 changes: 0 additions & 3 deletions lib/nativescript-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 21 additions & 8 deletions lib/services/ios-debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -76,6 +80,11 @@ class IOSDebugService implements IDebugService {

public debugStop(): IFuture<void> {
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();
Expand Down Expand Up @@ -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<void>()();
}

private emulatorStart(): IFuture<void> {
return (() => {
this.wireDebuggerClient(() => net.connect(inspectorBackendPort)).wait();
this.wireDebuggerClient().wait();

let attachRequestMessage = this.$iOSNotification.attachRequest;

Expand Down Expand Up @@ -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<void>()();
}

Expand All @@ -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<void>()();
}

private wireDebuggerClient(factory: () => net.Socket): IFuture<void> {
private wireDebuggerClient(device?: Mobile.IiOSDevice): IFuture<void> {
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<void>()();
}
Expand Down