Skip to content

Commit 26f134f

Browse files
committed
Workaround SR-13822 by keeping the file handle alive for the connection
On Linux, the file handle is closing the file descriptor on deinit despite `closeOnDealloc: false` (https://bugs.swift.org/browse/SR-13822). Workaround by keping the FileHandle alive. rdar://70995458
1 parent 0068eae commit 26f134f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Sources/sourcekit-lsp/main.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ struct Main: ParsableCommand {
159159
fatalError("failed to redirect stdout -> stderr: \(strerror(errno)!)")
160160
}
161161

162+
let realStdoutHandle = FileHandle(fileDescriptor: realStdout, closeOnDealloc: false)
163+
162164
let clientConnection = JSONRPCConnection(
163165
protocol: MessageRegistry.lspProtocol,
164166
inFD: FileHandle.standardInput,
165-
outFD: FileHandle(fileDescriptor: realStdout, closeOnDealloc: false),
167+
outFD: realStdoutHandle,
166168
syncRequests: syncRequests
167169
)
168170

@@ -174,6 +176,9 @@ struct Main: ParsableCommand {
174176
})
175177
clientConnection.start(receiveHandler: server, closeHandler: {
176178
server.prepareForExit()
179+
// FIXME: keep the FileHandle alive until we close the connection to
180+
// workaround SR-13822.
181+
withExtendedLifetime(realStdoutHandle) {}
177182
// Use _Exit to avoid running static destructors due to SR-12668.
178183
_Exit(0)
179184
})

0 commit comments

Comments
 (0)