From c366f9769f6bfa39229311c82cca0cec0d211361 Mon Sep 17 00:00:00 2001 From: Juan Francisco Lebrero <101231690+frizynn@users.noreply.github.com> Date: Mon, 23 Mar 2026 12:51:38 -0300 Subject: [PATCH] fix: clean up extension error listener on session dispose (#2165) The dispose() method was not cleaning up _extensionErrorUnsubscriber, causing the extension error handler to remain subscribed after session disposal. This leads to memory leaks across session reloads as old error handlers accumulate on the extension runner. Also wrap the unsubscriber call in _applyExtensionBindings() with try-catch so that if the previous unsubscriber throws, the new subscription is still set up correctly. --- packages/pi-coding-agent/src/core/agent-session.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/pi-coding-agent/src/core/agent-session.ts b/packages/pi-coding-agent/src/core/agent-session.ts index 03389954f..4fc8513bf 100644 --- a/packages/pi-coding-agent/src/core/agent-session.ts +++ b/packages/pi-coding-agent/src/core/agent-session.ts @@ -687,6 +687,8 @@ export class AgentSession { * Call this when completely done with the session. */ dispose(): void { + this._extensionErrorUnsubscriber?.(); + this._extensionErrorUnsubscriber = undefined; this._disconnectFromAgent(); this._eventListeners = []; } @@ -1928,7 +1930,11 @@ export class AgentSession { runner.setUIContext(this._extensionUIContext); runner.bindCommandContext(this._extensionCommandContextActions); - this._extensionErrorUnsubscriber?.(); + try { + this._extensionErrorUnsubscriber?.(); + } catch { + // Ignore errors from previous unsubscriber + } this._extensionErrorUnsubscriber = this._extensionErrorListener ? runner.onError(this._extensionErrorListener) : undefined;