Skip to content

Commit f7eca55

Browse files
authored
Merge pull request #83 from CodinGame/force-close-connection
Force close connection on shutdown
2 parents 5c55513 + 2b78c0b commit f7eca55

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/createLanguageClient.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,25 @@ async function createLanguageClient (
125125
return undefined
126126
}
127127

128-
const client = new MonacoLanguageClient({
128+
// Hack to force close the connection when the language client stops
129+
// Even if the `shutdown` request failed
130+
class _MonacoLanguageClient extends MonacoLanguageClient {
131+
override async stop (timeout: number): Promise<void> {
132+
// eslint-disable-next-line dot-notation
133+
const connection = this['_connection']
134+
try {
135+
await super.stop(timeout)
136+
} finally {
137+
try {
138+
connection.dispose()
139+
} catch (err) {
140+
// ignore
141+
}
142+
}
143+
}
144+
}
145+
146+
const client = new _MonacoLanguageClient({
129147
id: `${id}-languageclient`,
130148
name: `CodinGame ${id} Language Client`,
131149
clientOptions: {

src/infrastructure.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,18 @@ export interface Infrastructure {
5151
getInitializationOptions? (): LSPAny
5252
}
5353

54+
class CloseOnDisposeWebSocketMessageReader extends WebSocketMessageReader {
55+
override dispose () {
56+
super.dispose()
57+
this.socket.dispose()
58+
}
59+
}
60+
5461
async function openWebsocketConnection (url: URL | string): Promise<MessageTransports> {
5562
const webSocket = new WebSocket(url)
5663
const socket: IWebSocket = toSocket(webSocket)
5764

58-
const reader = new WebSocketMessageReader(socket)
65+
const reader = new CloseOnDisposeWebSocketMessageReader(socket)
5966
const writer = new WebSocketMessageWriter(socket)
6067

6168
await new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)