Commit c684ca7
fix(cua): make session.stop() idempotent on repeated call (#156)
## Summary
Stacks on top of the unified CUA template branch. Addresses the new
High-Severity Cursor Bugbot finding on PR #143's latest scan: `stop()`
throws on repeated call instead of being a no-op.
## Background
PR #155 moved the session-state reset (`_sessionId = null`, …) into the
`finally` so that a thrown replay-stop or browser-delete error wouldn't
leave stale state behind. That fix was correct, but it exposed a latent
bug: `stop()` opens with `const info = this.info` (TS) / `info =
self.info` (Py), and the `info` getter delegates to the `sessionId`
property, which raises when `_sessionId` is null. So once PR #155
reliably cleared `_sessionId` on the first call, the caller's error-path
retry would hit the throwing getter and mask the original exception —
exactly the failure mode PR #155 was meant to prevent.
## Fix
`stop()` now:
1. Short-circuits at the top with a sentinel `SessionInfo` when no
session is active — never touches the throwing getter.
2. Builds the live-session `info` from local fields directly so the body
never depends on `this.info` / `self.info` either.
Symmetric in TS (`session.ts`) and Python (`session.py`).
## Test plan
- [x] `go build ./...`
- [x] `go test ./pkg/create/...`
- [ ] Force a replay-stop failure (e.g. delete the replay out-of-band,
or pass an invalid replay id) and confirm calling `session.stop()` twice
from the caller's error path no longer raises and the original error
surfaces.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Touches session shutdown/cleanup logic in both Python and TypeScript
templates; while small, mistakes could lead to leaked browser sessions
or masked errors during teardown.
>
> **Overview**
> `KernelBrowserSession.stop()` in both
`pkg/templates/python/cua/session.py` and
`pkg/templates/typescript/cua/session.ts` is updated to be
**idempotent**.
>
> It now short-circuits when no active session exists (returning a
sentinel `SessionInfo` without reading `info`/`sessionId`), and builds
the returned `SessionInfo` directly from internal fields so teardown
can’t fail due to accessing a getter after state has been cleared.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
db0bdea. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>1 parent f588e58 commit c684ca7
2 files changed
Lines changed: 77 additions & 41 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
115 | 135 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
136 | 154 | | |
137 | 155 | | |
138 | 156 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
107 | 119 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
117 | 133 | | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 134 | + | |
| 135 | + | |
128 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
129 | 147 | | |
130 | 148 | | |
131 | 149 | | |
| |||
0 commit comments