Skip to content

Commit

Permalink
debug: more robust wait for server to listen to socket
Browse files Browse the repository at this point in the history
Take the waiting code from the existing adapter - try to connect after server emits to stdout. Also add comment in dapClient.ts for clarity.

Updates #23
Follow up on #267

Change-Id: I29d0c87504464ce2ad0297ec0a5d7ad67c707b88
GitHub-Last-Rev: 13c423a
GitHub-Pull-Request: #291
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/241117
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
  • Loading branch information
eliben authored and hyangah committed Jul 8, 2020
1 parent b93cc05 commit c9f1475
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/debugAdapter2/dapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class DAPClient extends EventEmitter {
this.outputStream.write(`Content-Length: ${Buffer.byteLength(json, 'utf8')}\r\n\r\n${json}`, 'utf8');
}

// Connect this client to a server, which is represented by read and write
// streams. Before this method is called, send() won't work and no messages
// from the server will be delivered.
protected connect(readable: stream.Readable, writable: stream.Writable): void {
this.outputStream = writable;

Expand Down
19 changes: 14 additions & 5 deletions src/debugAdapter2/goDlvDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ export class GoDlvDapDebugSession extends LoggingDebugSession {
// 'close' (rc): delve exited with return code rc
class DelveClient extends DAPClient {
private debugProcess: ChildProcess;
private serverStarted: boolean = false;

constructor(launchArgs: LaunchRequestArguments) {
super();
Expand Down Expand Up @@ -594,6 +595,11 @@ class DelveClient extends DAPClient {
this.debugProcess.stdout.on('data', (chunk) => {
const str = chunk.toString();
this.emit('stdout', str);

if (!this.serverStarted) {
this.serverStarted = true;
this.connectSocketToServer(launchArgs.port, launchArgs.host);
}
});

this.debugProcess.on('close', (rc) => {
Expand All @@ -608,13 +614,16 @@ class DelveClient extends DAPClient {
this.debugProcess.on('error', (err) => {
throw err;
});
}

// Give the Delve DAP server some time to start up before connecting.
// TODO: do this in a more robust way.
// Connect this client to the server. The server is expected to be listening
// on host:port.
private connectSocketToServer(port: number, host: string) {
// Add a slight delay to ensure that Delve started up the server.
setTimeout(() => {
const socket = net.createConnection(
launchArgs.port,
launchArgs.host,
port,
host,
() => {
this.connect(socket, socket);
this.emit('connected');
Expand All @@ -623,6 +632,6 @@ class DelveClient extends DAPClient {
socket.on('error', (err) => {
throw err;
});
}, 100);
}, 200);
}
}

0 comments on commit c9f1475

Please sign in to comment.