From e50b4d78e589ef66996e9a7255b039698a3753d8 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Fri, 4 Oct 2024 17:20:46 -0700 Subject: [PATCH] Remove elements from `outstandingRequests` on crash Otherwise, we can have a retain cycle where the `OutstandingRequest` keeps the `JSONRPCConnection` alive. Also move the code that handled `outstandingRequests` out of the `Task` because `outstandingRequests` should only be accessed from `queue`. --- .../JSONRPCConnection.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift b/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift index be8c44dcc..ace790dcf 100644 --- a/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift +++ b/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift @@ -172,13 +172,14 @@ public final class JSONRPCConnection: Connection { ) ioGroup.notify(queue: queue) { [weak self] in - guard let self = self else { return } + guard let self else { return } + for outstandingRequest in self.outstandingRequests.values { + outstandingRequest.replyHandler(LSPResult.failure(ResponseError.internalError("JSON-RPC Connection closed"))) + } + self.outstandingRequests = [:] + self.receiveHandler = nil // break retain cycle Task { - for outstandingRequest in self.outstandingRequests.values { - outstandingRequest.replyHandler(LSPResult.failure(ResponseError.internalError("JSON-RPC Connection closed"))) - } await self.closeHandler?() - self.receiveHandler = nil // break retain cycle } }