fix(memory): prevent memory leaks in session, server, and frontend

Backend fixes:
- Session: clear _recentTaskDescriptions Map in stop() and clearBuffers()
- Session: null promise callbacks after rejection in runPrompt() catch block
- Server: store subagent/image watcher listener refs and remove on shutdown

Frontend fixes:
- Plan file windows: store drag/resize handlers and clean up on close
- Plan file manager: store drag handler and clean up on close
- cleanupAllFloatingWindows: now cleans up plan file windows

These prevent unbounded memory growth in long-running sessions.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-30 13:25:18 +01:00
co-authored by Claude Opus 4.5
parent 2693aea11e
commit e3e0d226ce
3 changed files with 910 additions and 70 deletions
+23
View File
@@ -362,6 +362,9 @@ export class Session extends EventEmitter {
// Nice prioritying configuration
private _niceConfig: NiceConfig = { ...DEFAULT_NICE_CONFIG };
// Session color for visual differentiation
private _color: import('./types.js').SessionColor = 'default';
// Store handler references for cleanup (prevents memory leaks)
private _taskTrackerHandlers: {
taskCreated: (task: BackgroundTask) => void;
@@ -592,6 +595,18 @@ export class Session extends EventEmitter {
}
}
// Session color for visual differentiation
get color(): import('./types.js').SessionColor {
return this._color;
}
setColor(color: import('./types.js').SessionColor): void {
const validColors = ['default', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink'];
if (validColors.includes(color)) {
this._color = color;
}
}
// Token tracking getters and setters
get totalTokens(): number {
return this._totalInputTokens + this._totalOutputTokens;
@@ -717,6 +732,7 @@ export class Session extends EventEmitter {
childAgentIds: this._childAgentIds.length > 0 ? this._childAgentIds : undefined,
niceEnabled: this._niceConfig.enabled,
niceValue: this._niceConfig.niceValue,
color: this._color,
};
}
@@ -1314,6 +1330,9 @@ export class Session extends EventEmitter {
} catch (err) {
this._status = 'error';
reject(err);
// Null callbacks to prevent memory leak (onExit won't run if spawn failed)
this.resolvePromise = null;
this.rejectPromise = null;
}
});
}
@@ -1920,6 +1939,9 @@ export class Session extends EventEmitter {
this._status = 'stopped';
this._currentTaskId = null;
// Clear task description cache to prevent memory leak
this._recentTaskDescriptions.clear();
// Kill the associated screen session if requested
if (killScreen && this._screenManager) {
// Try to kill screen even if _screenSession is not set (e.g., restored sessions)
@@ -1973,5 +1995,6 @@ export class Session extends EventEmitter {
this._messages = [];
this._taskTracker.clear();
this._ralphTracker.clear();
this._recentTaskDescriptions.clear();
}
}