Skip to content

Fix a race condition in LocalConnection #1431

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

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
48 changes: 32 additions & 16 deletions Sources/InProcessClient/LocalConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,60 @@ import LanguageServerProtocol
/// conn.send(...) // handled by server
/// conn.close()
/// ```
///
/// - Note: Unchecked sendable conformance because shared state is guarded by `queue`.
public final class LocalConnection: Connection, @unchecked Sendable {
enum State {
public final class LocalConnection: Connection, Sendable {
private enum State {
case ready, started, closed
}

/// A name of the endpoint for this connection, used for logging, e.g. `clangd`.
private let name: String

/// The queue guarding `_nextRequestID`.
let queue: DispatchQueue = DispatchQueue(label: "local-connection-queue")
private let queue: DispatchQueue = DispatchQueue(label: "local-connection-queue")

var _nextRequestID: Int = 0
/// - Important: Must only be accessed from `queue`
nonisolated(unsafe) private var _nextRequestID: Int = 0

var state: State = .ready
/// - Important: Must only be accessed from `queue`
nonisolated(unsafe) private var state: State = .ready

var handler: MessageHandler? = nil
/// - Important: Must only be accessed from `queue`
nonisolated(unsafe) private var handler: MessageHandler? = nil

public init(name: String) {
self.name = name
}

deinit {
if state != .closed {
close()
queue.sync {
if state != .closed {
closeAssumingOnQueue()
}
}
}

public func start(handler: MessageHandler) {
precondition(state == .ready)
state = .started
self.handler = handler
queue.sync {
precondition(state == .ready)
state = .started
self.handler = handler
}
}

public func close() {
/// - Important: Must only be called from `queue`
private func closeAssumingOnQueue() {
dispatchPrecondition(condition: .onQueue(queue))
precondition(state != .closed)
handler = nil
state = .closed
}

public func close() {
queue.sync {
closeAssumingOnQueue()
}
}

func nextRequestID() -> RequestID {
return queue.sync {
_nextRequestID += 1
Expand All @@ -81,7 +94,10 @@ public final class LocalConnection: Connection, @unchecked Sendable {
\(notification.forLogging)
"""
)
self.handler?.handle(notification)
guard let handler = queue.sync(execute: { handler }) else {
return
}
handler.handle(notification)
}

public func send<Request: RequestType>(
Expand All @@ -97,7 +113,7 @@ public final class LocalConnection: Connection, @unchecked Sendable {
"""
)

guard let handler = self.handler else {
guard let handler = queue.sync(execute: { handler }) else {
logger.info(
"""
Replying to request \(id, privacy: .public) with .serverCancelled because no handler is specified in \(self.name, privacy: .public)
Expand Down