Skip to content

Commit 5a913e6

Browse files
authored
Refactor NativePythonFinderImpl for better disposable management (#1144)
Fixes #1141 Improve management of disposables in the NativePythonFinderImpl class to enhance resource cleanup and prevent potential memory leaks. ## Changes - Added class-level `startDisposables` array to track disposables created in `start()` - Updated `dispose()` to properly stop the worker pool and dispose all tracked resources - Ensures event handlers (`log`, `telemetry`, `onError`, etc.) are properly cleaned up
1 parent 2bf2cc5 commit 5a913e6

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@
2727
"prettier.tabWidth": 4,
2828
"python-envs.defaultEnvManager": "ms-python.python:venv",
2929
"python-envs.pythonProjects": [],
30-
"git.branchRandomName.enable": true
30+
"git.branchRandomName.enable": true,
31+
"git.branchProtection": ["main"],
32+
"git.branchProtectionPrompt": "alwaysCommitToNewBranch"
3133
}

src/managers/common/nativePythonFinder.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
108108
private readonly connection: rpc.MessageConnection;
109109
private readonly pool: WorkerPool<NativePythonEnvironmentKind | Uri[] | undefined, NativeInfo[]>;
110110
private cache: Map<string, NativeInfo[]> = new Map();
111+
private readonly startDisposables: Disposable[] = [];
111112

112113
constructor(
113114
private readonly outputChannel: LogOutputChannel,
@@ -192,6 +193,8 @@ class NativePythonFinderImpl implements NativePythonFinder {
192193
}
193194

194195
public dispose() {
196+
this.pool.stop();
197+
this.startDisposables.forEach((d) => d.dispose());
195198
this.connection.dispose();
196199
}
197200

@@ -221,14 +224,13 @@ class NativePythonFinderImpl implements NativePythonFinder {
221224
// we have got the exit event.
222225
const readable = new PassThrough();
223226
const writable = new PassThrough();
224-
const disposables: Disposable[] = [];
225227
try {
226228
const proc = spawnProcess(this.toolPath, ['server'], { env: process.env, stdio: 'pipe' });
227229
proc.stdout.pipe(readable, { end: false });
228230
proc.stderr.on('data', (data) => this.outputChannel.error(`[pet] ${data.toString()}`));
229231
writable.pipe(proc.stdin, { end: false });
230232

231-
disposables.push({
233+
this.startDisposables.push({
232234
dispose: () => {
233235
try {
234236
if (proc.exitCode === null) {
@@ -246,7 +248,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
246248
new rpc.StreamMessageReader(readable),
247249
new rpc.StreamMessageWriter(writable),
248250
);
249-
disposables.push(
251+
this.startDisposables.push(
250252
connection,
251253
new Disposable(() => {
252254
readable.end();
@@ -276,7 +278,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
276278
}),
277279
connection.onNotification('telemetry', (data) => this.outputChannel.info('[pet] Telemetry: ', data)),
278280
connection.onClose(() => {
279-
disposables.forEach((d) => d.dispose());
281+
this.startDisposables.forEach((d) => d.dispose());
280282
}),
281283
);
282284

0 commit comments

Comments
 (0)