Skip to content

fix: fix debugger attaching after a page reload #4241

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
Jan 3, 2019
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
3 changes: 1 addition & 2 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,7 @@ interface IAppDebugSocketProxyFactory extends NodeJS.EventEmitter {
getTCPSocketProxy(deviceIdentifier: string, appId: string): any;
addTCPSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise<any>;

getWebSocketProxy(deviceIdentifier: string, appId: string): any;
addWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise<any>;
ensureWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise<any>;

removeAllProxies(): void;
}
Expand Down
20 changes: 12 additions & 8 deletions lib/device-sockets/ios/app-debug-socket-proxy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
return this.deviceTcpServers[`${deviceIdentifier}-${appId}`];
}

public getWebSocketProxy(deviceIdentifier: string, appId: string): ws.Server {
return this.deviceWebServers[`${deviceIdentifier}-${appId}`];
}

public async addTCPSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise<net.Server> {
const cacheKey = `${device.deviceInfo.identifier}-${appId}`;
const existingServer = this.deviceTcpServers[cacheKey];
Expand Down Expand Up @@ -84,7 +80,17 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
return server;
}

public async addWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise<ws.Server> {
public async ensureWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise<ws.Server> {
const existingWebProxy = this.deviceWebServers[`${device.deviceInfo.identifier}-${appId}`];
const result = existingWebProxy || await this.addWebSocketProxy(device, appId);

// TODO: do not remove till VSCode waits for this message in order to reattach
this.$logger.info("Opened localhost " + result.options.port);

return result;
}

private async addWebSocketProxy(device: Mobile.IiOSDevice, appId: string): Promise<ws.Server> {
const cacheKey = `${device.deviceInfo.identifier}-${appId}`;
const existingServer = this.deviceWebServers[cacheKey];
if (existingServer) {
Expand Down Expand Up @@ -152,12 +158,11 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu
appDebugSocket.on("close", () => {
this.$logger.info("Backend socket closed!");
webSocket.close();
server.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the server socket not closed on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server socket is appDebugSocket and this is it's close handler. server is our proxy server and it's not closed in order to stay available for new client connections. Because of this line, the Chrome Dev Tools was not able to reconnect when you try to refresh the page.

delete this.deviceWebServers[cacheKey];
});

webSocket.on("close", () => {
this.$logger.info('Frontend socket closed!');
appDebugSocket.unpipe(packets);
packets.destroy();
device.destroyDebugSocket(appId);
if (!this.$options.watch) {
Expand All @@ -167,7 +172,6 @@ export class AppDebugSocketProxyFactory extends EventEmitter implements IAppDebu

});

this.$logger.info("Opened localhost " + localPort);
return server;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/services/ios-device-debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe
if (debugOptions.chrome) {
this.$logger.info("'--chrome' is the default behavior. Use --inspector to debug iOS applications using the Safari Web Inspector.");
}
const existingWebProxy = this.$appDebugSocketProxyFactory.getWebSocketProxy(this.deviceIdentifier, debugData.applicationIdentifier);
const webSocketProxy = existingWebProxy || await this.$appDebugSocketProxyFactory.addWebSocketProxy(this.device, debugData.applicationIdentifier);
const webSocketProxy = await this.$appDebugSocketProxyFactory.ensureWebSocketProxy(this.device, debugData.applicationIdentifier);

return this.getChromeDebugUrl(debugOptions, webSocketProxy.options.port);
}
Expand Down