Skip to content

debug: more robust wait for server to listen to socket #291

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

Closed
wants to merge 3 commits into from
Closed
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: 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);
}
}