Skip to content

Commit c9f1475

Browse files
elibenhyangah
authored andcommitted
debug: more robust wait for server to listen to socket
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 <[email protected]>
1 parent b93cc05 commit c9f1475

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/debugAdapter2/dapClient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export class DAPClient extends EventEmitter {
3333
this.outputStream.write(`Content-Length: ${Buffer.byteLength(json, 'utf8')}\r\n\r\n${json}`, 'utf8');
3434
}
3535

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

src/debugAdapter2/goDlvDebug.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ export class GoDlvDapDebugSession extends LoggingDebugSession {
544544
// 'close' (rc): delve exited with return code rc
545545
class DelveClient extends DAPClient {
546546
private debugProcess: ChildProcess;
547+
private serverStarted: boolean = false;
547548

548549
constructor(launchArgs: LaunchRequestArguments) {
549550
super();
@@ -594,6 +595,11 @@ class DelveClient extends DAPClient {
594595
this.debugProcess.stdout.on('data', (chunk) => {
595596
const str = chunk.toString();
596597
this.emit('stdout', str);
598+
599+
if (!this.serverStarted) {
600+
this.serverStarted = true;
601+
this.connectSocketToServer(launchArgs.port, launchArgs.host);
602+
}
597603
});
598604

599605
this.debugProcess.on('close', (rc) => {
@@ -608,13 +614,16 @@ class DelveClient extends DAPClient {
608614
this.debugProcess.on('error', (err) => {
609615
throw err;
610616
});
617+
}
611618

612-
// Give the Delve DAP server some time to start up before connecting.
613-
// TODO: do this in a more robust way.
619+
// Connect this client to the server. The server is expected to be listening
620+
// on host:port.
621+
private connectSocketToServer(port: number, host: string) {
622+
// Add a slight delay to ensure that Delve started up the server.
614623
setTimeout(() => {
615624
const socket = net.createConnection(
616-
launchArgs.port,
617-
launchArgs.host,
625+
port,
626+
host,
618627
() => {
619628
this.connect(socket, socket);
620629
this.emit('connected');
@@ -623,6 +632,6 @@ class DelveClient extends DAPClient {
623632
socket.on('error', (err) => {
624633
throw err;
625634
});
626-
}, 100);
635+
}, 200);
627636
}
628637
}

0 commit comments

Comments
 (0)