Skip to content

Commit 0542421

Browse files
committed
Drop jsonrpc connection after all messages are handled (#23391)
1 parent e5a4c75 commit 0542421

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/client/pythonEnvironments/base/locators/common/nativePythonFinder.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CancellationToken, Disposable, Event, EventEmitter } from 'vscode';
55
import * as ch from 'child_process';
66
import * as path from 'path';
77
import * as rpc from 'vscode-jsonrpc/node';
8+
import { PassThrough } from 'stream';
89
import { isWindows } from '../../../../common/platform/platformService';
910
import { EXTENSION_ROOT_DIR } from '../../../../constants';
1011
import { traceError, traceInfo, traceLog, traceVerbose, traceWarn } from '../../../../logging';
@@ -56,24 +57,43 @@ class NativeGlobalPythonFinderImpl implements NativeGlobalPythonFinder {
5657

5758
public startSearch(token?: CancellationToken): Promise<void> {
5859
const deferred = createDeferred<void>();
59-
const proc = ch.spawn(NATIVE_LOCATOR, [], { stdio: 'pipe' });
60+
const proc = ch.spawn(NATIVE_LOCATOR, [], { env: process.env });
6061
const disposables: Disposable[] = [];
6162

63+
// jsonrpc package cannot handle messages coming through too quicly.
64+
// Lets handle the messages and close the stream only when
65+
// we have got the exit event.
66+
const readable = new PassThrough();
67+
proc.stdout.pipe(readable, { end: false });
68+
const writable = new PassThrough();
69+
writable.pipe(proc.stdin, { end: false });
70+
const disposeStreams = new Disposable(() => {
71+
readable.end();
72+
readable.destroy();
73+
writable.end();
74+
writable.destroy();
75+
});
6276
const connection = rpc.createMessageConnection(
63-
new rpc.StreamMessageReader(proc.stdout),
64-
new rpc.StreamMessageWriter(proc.stdin),
77+
new rpc.StreamMessageReader(readable),
78+
new rpc.StreamMessageWriter(writable),
6579
);
6680

6781
disposables.push(
6882
connection,
83+
disposeStreams,
84+
connection.onError((ex) => {
85+
disposeStreams.dispose();
86+
traceError('Error in Native Python Finder', ex);
87+
}),
6988
connection.onNotification('pythonEnvironment', (data: NativeEnvInfo) => {
7089
this._onDidFindPythonEnvironment.fire(data);
7190
}),
7291
connection.onNotification('envManager', (data: NativeEnvManagerInfo) => {
7392
this._onDidFindEnvironmentManager.fire(data);
7493
}),
7594
connection.onNotification('exit', () => {
76-
traceVerbose('Native Python Finder exited');
95+
traceInfo('Native Python Finder exited');
96+
disposeStreams.dispose();
7797
}),
7898
connection.onNotification('log', (data: NativeLog) => {
7999
switch (data.level) {

0 commit comments

Comments
 (0)