Skip to content

Commit

Permalink
Wrap nRepl client close in a promise
Browse files Browse the repository at this point in the history
Poor (dumb) man's way to wait for `close` messages to have been sent
before destroying the socket.

* Fixes #2301
  • Loading branch information
PEZ committed Sep 19, 2023
1 parent cb80a72 commit c6b2ec8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function connectToHost(hostname: string, port: number, connectSequence: Re

if (nClient) {
nClient['silent'] = true;
nClient.close();
await nClient.close();
}

let cljSession: NReplSession;
Expand Down Expand Up @@ -863,7 +863,7 @@ export default {
} else {
// the connection may be ended before
// the REPL client was connected.
nClient.close();
void nClient.close();
}
liveShareSupport.didDisconnectRepl();
nClient = undefined;
Expand Down
22 changes: 14 additions & 8 deletions src/nrepl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,20 @@ export class NReplClient {
log(data, Direction.ClientToServer);
}

close() {
for (const id in this.sessions) {
this.sessions[id].close();
}
// TODO: Figure out a way to know when the socket can be destroyed without an error.
setTimeout(() => {
this.disconnect();
}, 1000);
async close() {
return new Promise<NReplClient>((resolve, _reject) => {
for (const id in this.sessions) {
this.sessions[id].close();
}
// TODO: We probably need to make the whole disconnect process awaitable
// So that we do not destroy the socket before we have sent the `close` message
// This setTimeout caused https://github.com/BetterThanTomorrow/calva/issues/2301
// For now wrapped in a promise...
setTimeout(() => {
this.disconnect();
resolve(this);
}, 1000);
});
}

disconnect() {
Expand Down

0 comments on commit c6b2ec8

Please sign in to comment.