Skip to content

Commit 88825da

Browse files
Fix connect ECONNREFUSED error when trying to debug on iOS Sim
When trying to debug on iOS Simulator (via NativeScript Inspector), we often receive `Error: connect ECONNREFUSED 127.0.0.1:18181` error. There are two different problems. First one is incorrect identifying of the application's PID - we are using `_.trimStart` method totally incorrectly and in case your application identifier has numbers in it, they'll also be replaced in the search PID string. For example in case your app id is `org.nativescript.app10`, and the real PID of the running application is 18129, the current usage of `trimStart` method will give you the PID 8129. Fix this by applying regular expression and find the correct PID. The second problem is that there's some delay between opening the NativeScript Inspector and the debugged application on the device. In many cases the first time when we try to connect, we receive the error ECONNREFUSED. However in the previous implementation, we were trying to connect multiple times. Get back the old code and adjust it to work with Promises instead of the old sync execution.
1 parent d94a5ec commit 88825da

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

lib/common

Submodule common updated 1 file

lib/declarations.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ interface IAndroidToolsInfoData {
288288
}
289289

290290
interface ISocketProxyFactory extends NodeJS.EventEmitter {
291-
createTCPSocketProxy(factory: () => Promise<any>): any;
291+
createTCPSocketProxy(factory: () => Promise<any>): Promise<any>;
292292
createWebSocketProxy(factory: () => Promise<any>): Promise<any>;
293293
}
294294

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

+29-25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CONNECTION_ERROR_EVENT_NAME } from "../../constants";
33
import { PacketStream } from "./packet-stream";
44
import * as net from "net";
55
import * as ws from "ws";
6+
import * as helpers from "../../common/helpers";
67
import temp = require("temp");
78

89
export class SocketProxyFactory extends EventEmitter implements ISocketProxyFactory {
@@ -14,7 +15,9 @@ export class SocketProxyFactory extends EventEmitter implements ISocketProxyFact
1415
super();
1516
}
1617

17-
public createTCPSocketProxy(factory: () => Promise<net.Socket>): net.Server {
18+
public async createTCPSocketProxy(factory: () => Promise<net.Socket>): Promise<net.Server> {
19+
const socketFactory = async (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
20+
1821
this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
1922

2023
let server = net.createServer({
@@ -31,32 +34,33 @@ export class SocketProxyFactory extends EventEmitter implements ISocketProxyFact
3134
}
3235
});
3336

34-
const backendSocket = await factory();
35-
this.$logger.info("Backend socket created.");
36-
37-
backendSocket.on("end", () => {
38-
this.$logger.info("Backend socket closed!");
39-
if (!(this.$config.debugLivesync && this.$options.watch)) {
40-
process.exit(0);
41-
}
42-
});
37+
await socketFactory((backendSocket: net.Socket) => {
38+
this.$logger.info("Backend socket created.");
4339

44-
frontendSocket.on("close", () => {
45-
console.log("frontend socket closed");
46-
if (!(<any>backendSocket).destroyed) {
47-
backendSocket.destroy();
48-
}
40+
backendSocket.on("end", () => {
41+
this.$logger.info("Backend socket closed!");
42+
if (!(this.$config.debugLivesync && this.$options.watch)) {
43+
process.exit(0);
44+
}
45+
});
46+
47+
frontendSocket.on("close", () => {
48+
console.log("frontend socket closed");
49+
if (!(<any>backendSocket).destroyed) {
50+
backendSocket.destroy();
51+
}
52+
});
53+
backendSocket.on("close", () => {
54+
console.log("backend socket closed");
55+
if (!(<any>frontendSocket).destroyed) {
56+
frontendSocket.destroy();
57+
}
58+
});
59+
60+
backendSocket.pipe(frontendSocket);
61+
frontendSocket.pipe(backendSocket);
62+
frontendSocket.resume();
4963
});
50-
backendSocket.on("close", () => {
51-
console.log("backend socket closed");
52-
if (!(<any>frontendSocket).destroyed) {
53-
frontendSocket.destroy();
54-
}
55-
});
56-
57-
backendSocket.pipe(frontendSocket);
58-
frontendSocket.pipe(backendSocket);
59-
frontendSocket.resume();
6064
});
6165

6266
let socketFileLocation = temp.path({ suffix: ".sock" });

lib/services/ios-debug-service.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,18 @@ class IOSDebugService extends DebugServiceBase implements IPlatformDebugService
103103

104104
let lineStream = byline(child_process.stdout);
105105
this._childProcess = child_process;
106+
const pidRegExp = new RegExp(`${debugData.applicationIdentifier}:\\s?(\\d+)`);
106107

107108
lineStream.on('data', (line: NodeBuffer) => {
108109
let lineText = line.toString();
109110
if (lineText && _.startsWith(lineText, debugData.applicationIdentifier)) {
110-
let pid = _.trimStart(lineText, debugData.applicationIdentifier + ": ");
111+
const pidMatch = lineText.match(pidRegExp);
112+
113+
if (!pidMatch) {
114+
this.$logger.trace(`Line ${lineText} does not contain the searched pattern: ${pidRegExp}.`);
115+
}
116+
117+
const pid = pidMatch[1];
111118
this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
112119
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
113120
this._lldbProcess.stdout.pipe(process.stdout);
@@ -182,7 +189,7 @@ class IOSDebugService extends DebugServiceBase implements IPlatformDebugService
182189
const commitSHA = "02e6bde1bbe34e43b309d4ef774b1168d25fd024"; // corresponds to 55.0.2883 Chrome version
183190
return `chrome-devtools://devtools/remote/serve_file/@${commitSHA}/inspector.html?experiments=true&ws=localhost:${this._socketProxy.options.port}`;
184191
} else {
185-
this._socketProxy = this.$socketProxyFactory.createTCPSocketProxy(this.getSocketFactory(device));
192+
this._socketProxy = await this.$socketProxyFactory.createTCPSocketProxy(this.getSocketFactory(device));
186193
await this.openAppInspector(this._socketProxy.address(), debugData, debugOptions);
187194
return null;
188195
}

0 commit comments

Comments
 (0)