Skip to content

Commit 934f492

Browse files
authored
Clean up handlers and remove bus listeners on v3.close() (#1418)
# why With many concurrent sessions, memory leaks can crash the node process # what changed Ensured handler cleanup as well as removed eventbus listeners when calling `stagehand.close()` # test plan <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fully clean up V3 on close to prevent memory leaks and stuck handlers. We remove event bus listeners and clear local state during teardown. - **Bug Fixes** - Remove all event bus listeners on close. - Reset act/extract/observe handlers and clear history. - Ensure instance is removed from the global registry. <sup>Written for commit 6330dde. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
1 parent 8cff9ac commit 934f492

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
Cleanup handlers and bus listeners on close

packages/core/lib/v3/v3.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,17 @@ export class V3 {
13741374
} catch {
13751375
// ignore
13761376
}
1377+
// Clear all event bus listeners to prevent memory leaks and hanging handlers
1378+
try {
1379+
this.bus.removeAllListeners();
1380+
} catch {
1381+
// ignore
1382+
}
1383+
// Clear accumulated data to free memory
1384+
this._history = [];
1385+
this.actHandler = null;
1386+
this.extractHandler = null;
1387+
this.observeHandler = null;
13771388
// Remove from global registry
13781389
V3._instances.delete(this);
13791390
}

0 commit comments

Comments
 (0)