From 26f134ff2e3cf56fc36f024a7906a3abed23db79 Mon Sep 17 00:00:00 2001 From: Ben Langmuir Date: Tue, 3 Nov 2020 10:15:16 -0800 Subject: [PATCH] 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 --- Sources/sourcekit-lsp/main.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/sourcekit-lsp/main.swift b/Sources/sourcekit-lsp/main.swift index dd9be6e2..11c8e9ca 100644 --- a/Sources/sourcekit-lsp/main.swift +++ b/Sources/sourcekit-lsp/main.swift @@ -159,10 +159,12 @@ struct Main: ParsableCommand { fatalError("failed to redirect stdout -> stderr: \(strerror(errno)!)") } + let realStdoutHandle = FileHandle(fileDescriptor: realStdout, closeOnDealloc: false) + let clientConnection = JSONRPCConnection( protocol: MessageRegistry.lspProtocol, inFD: FileHandle.standardInput, - outFD: FileHandle(fileDescriptor: realStdout, closeOnDealloc: false), + outFD: realStdoutHandle, syncRequests: syncRequests ) @@ -174,6 +176,9 @@ struct Main: ParsableCommand { }) clientConnection.start(receiveHandler: server, closeHandler: { server.prepareForExit() + // FIXME: keep the FileHandle alive until we close the connection to + // workaround SR-13822. + withExtendedLifetime(realStdoutHandle) {} // Use _Exit to avoid running static destructors due to SR-12668. _Exit(0) })