Skip to content

Commit 844eda6

Browse files
committed
fix(ts-anthropic-cua): add try/finally to ensure browser session cleanup
Wrap replay stopping logic in try/finally to ensure browser session is always deleted even if stopReplay() fails. This prevents resource leaks on the Kernel platform when replay recording is enabled and stopping fails. Matches the existing Python implementation behavior.
1 parent 7c204a5 commit 844eda6

File tree

1 file changed

+15
-12
lines changed
  • pkg/templates/typescript/anthropic-computer-use

1 file changed

+15
-12
lines changed

pkg/templates/typescript/anthropic-computer-use/session.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,23 @@ export class KernelBrowserSession {
188188
const info = this.info;
189189

190190
if (this._sessionId) {
191-
// Stop replay if recording was enabled
192-
if (this.options.recordReplay && this._replayId) {
193-
// Wait grace period before stopping to capture final state
194-
if (this.options.replayGracePeriod > 0) {
195-
console.log(`Waiting ${this.options.replayGracePeriod}s grace period...`);
196-
await this.sleep(this.options.replayGracePeriod * 1000);
191+
try {
192+
// Stop replay if recording was enabled
193+
if (this.options.recordReplay && this._replayId) {
194+
// Wait grace period before stopping to capture final state
195+
if (this.options.replayGracePeriod > 0) {
196+
console.log(`Waiting ${this.options.replayGracePeriod}s grace period...`);
197+
await this.sleep(this.options.replayGracePeriod * 1000);
198+
}
199+
await this.stopReplay();
200+
info.replayViewUrl = this._replayViewUrl || undefined;
197201
}
198-
await this.stopReplay();
199-
info.replayViewUrl = this._replayViewUrl || undefined;
202+
} finally {
203+
// Always clean up the browser session, even if replay stopping fails
204+
console.log(`Destroying browser session: ${this._sessionId}`);
205+
await this.kernel.browsers.deleteByID(this._sessionId);
206+
console.log('Browser session destroyed.');
200207
}
201-
202-
console.log(`Destroying browser session: ${this._sessionId}`);
203-
await this.kernel.browsers.deleteByID(this._sessionId);
204-
console.log('Browser session destroyed.');
205208
}
206209

207210
// Reset state

0 commit comments

Comments
 (0)