From 00791fd2cc03cf0fca036bd7b12e4ecb1480a5e7 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Tue, 24 Dec 2024 00:31:02 +0530 Subject: [PATCH] fix read streams blocking node threads (#24652) fixes https://github.com/microsoft/vscode-python/issues/24594 fixes https://github.com/microsoft/vscode-python/issues/24507 Co-authored-by: Eleanor Boyd --- src/client/common/pipes/namedPipes.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/client/common/pipes/namedPipes.ts b/src/client/common/pipes/namedPipes.ts index 9722b4bdcc58..8cccd4cdcfed 100644 --- a/src/client/common/pipes/namedPipes.ts +++ b/src/client/common/pipes/namedPipes.ts @@ -12,6 +12,7 @@ import { CancellationError, CancellationToken, Disposable } from 'vscode'; import { traceVerbose } from '../../logging'; import { isWindows } from '../utils/platform'; import { createDeferred } from '../utils/async'; +import { noop } from '../utils/misc'; const { XDG_RUNTIME_DIR } = process.env; export function generateRandomPipeName(prefix: string): string { @@ -187,6 +188,13 @@ export async function createReaderPipe(pipeName: string, token?: CancellationTok } catch { // Intentionally ignored } - const reader = fs.createReadStream(pipeName, { encoding: 'utf-8' }); - return new rpc.StreamMessageReader(reader, 'utf-8'); + const fd = await fs.open(pipeName, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK); + const socket = new net.Socket({ fd }); + const reader = new rpc.SocketMessageReader(socket, 'utf-8'); + socket.on('close', () => { + fs.close(fd).catch(noop); + reader.dispose(); + }); + + return reader; }