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 golang/vscode-go#23
Follow up on golang/vscode-go#267
Change-Id: I29d0c87504464ce2ad0297ec0a5d7ad67c707b88
GitHub-Last-Rev: 13c423a9abfcdfd71515311d08b56b84c322594f
GitHub-Pull-Request: golang/vscode-go#291
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/241117
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/src/debugAdapter2/dapClient.ts b/src/debugAdapter2/dapClient.ts
index bcceba9..14d38a0 100644
--- a/src/debugAdapter2/dapClient.ts
+++ b/src/debugAdapter2/dapClient.ts
@@ -33,6 +33,9 @@
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;
diff --git a/src/debugAdapter2/goDlvDebug.ts b/src/debugAdapter2/goDlvDebug.ts
index e895945..fc3401f 100644
--- a/src/debugAdapter2/goDlvDebug.ts
+++ b/src/debugAdapter2/goDlvDebug.ts
@@ -544,6 +544,7 @@
// 'close' (rc): delve exited with return code rc
class DelveClient extends DAPClient {
private debugProcess: ChildProcess;
+ private serverStarted: boolean = false;
constructor(launchArgs: LaunchRequestArguments) {
super();
@@ -594,6 +595,11 @@
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) => {
@@ -608,13 +614,16 @@
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');
@@ -623,6 +632,6 @@
socket.on('error', (err) => {
throw err;
});
- }, 100);
+ }, 200);
}
}