Skip to content

Commit 14b1cd4

Browse files
authored
Document safe profile writes (#498)
1 parent 5b1545f commit 14b1cd4

1 file changed

Lines changed: 63 additions & 1 deletion

File tree

auth/profiles.mdx

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,68 @@ _ = kernelBrowser
127127
```
128128
</CodeGroup>
129129

130+
### Prevent concurrent profile writes
131+
132+
Saving replaces the profile's entire stored browser state; it doesn't merge cookies or local storage from multiple sessions. If more than one browser uses the same profile with `save_changes: true`, the browser that ends last overwrites the profile with its state.
133+
134+
Before starting a writer, call `GET /browsers?status=active&query=<profileId>` and check `profile_save_changes` to find active sessions that can save to that profile. The `query` parameter can match fields other than the profile ID, so filter the results by the exact `profile.id` too.
135+
136+
<CodeGroup>
137+
```typescript TypeScript
138+
const profileId = kernelBrowser.profile!.id;
139+
const activeWriters = [];
140+
141+
for await (const browser of kernel.browsers.list({
142+
status: 'active',
143+
query: profileId,
144+
})) {
145+
if (browser.profile?.id === profileId && browser.profile_save_changes) {
146+
activeWriters.push(browser);
147+
}
148+
}
149+
150+
if (activeWriters.length > 0) {
151+
throw new Error(`Profile already has an active writer: ${activeWriters[0].session_id}`);
152+
}
153+
```
154+
155+
```python Python
156+
profile_id = kernel_browser.profile.id
157+
active_writers = [
158+
browser
159+
for browser in kernel.browsers.list(status="active", query=profile_id)
160+
if browser.profile
161+
and browser.profile.id == profile_id
162+
and browser.profile_save_changes
163+
]
164+
165+
if active_writers:
166+
raise RuntimeError(
167+
f"Profile already has an active writer: {active_writers[0].session_id}"
168+
)
169+
```
170+
171+
```go Go
172+
profileID := kernelBrowser.Profile.ID
173+
pager := client.Browsers.ListAutoPaging(ctx, kernel.BrowserListParams{
174+
Status: kernel.BrowserListParamsStatusActive,
175+
Query: kernel.String(profileID),
176+
})
177+
178+
for pager.Next() {
179+
browser := pager.Current()
180+
if browser.Profile.ID == profileID && browser.ProfileSaveChanges {
181+
panic(fmt.Sprintf("profile already has an active writer: %s", browser.SessionID))
182+
}
183+
}
184+
if err := pager.Err(); err != nil {
185+
panic(err)
186+
}
187+
```
188+
</CodeGroup>
189+
190+
This check and browser creation are separate requests. If multiple workers can start sessions concurrently, use your own lock or lease around both operations so two workers can't pass the check at the same time.
191+
130192
## 3. Use the browser, then close it to persist the state
131193

132194
After using a browser with `save_changes: true`, closing the browser will save cookies and local storage into the profile.
@@ -513,6 +575,6 @@ _ = browser
513575
- A profile's `name` must be unique within your [project](/info/projects). The same name can be reused across different projects in the same org.
514576
- Profiles store cookies and local storage. Start the session with `save_changes: true` to write changes back when the browser is closed.
515577
- To keep a profile immutable for a run, omit `save_changes` (default) when creating the browser.
516-
- Multiple browsers in parallel can use the same profile, but only one browser should write (`save_changes: true`) to it at a time. Parallel browsers with `save_changes: true` may cause profile corruption and unpredictable behavior.
578+
- Multiple browsers in parallel can use the same profile, but only one browser can safely write (`save_changes: true`) to it at a time. Each save overwrites the whole profile, so the browser that ends last wins.
517579
- `save_changes` applies to a profile attached to a single browser — either at creation (`kernel.browsers.create()`) or loaded afterward with `kernel.browsers.update()`. A profile set on a [browser pool's](/browsers/pools) config is loaded read-only and never persisted; `save_changes` sent on a pool's profile is silently ignored. To persist per-user state through a pool, attach the profile with `save_changes: true` after acquiring the browser and release with `reuse: false` — see [Per-user profiles with pools](/browsers/pools#per-user-profiles-with-browser-pools).
518580
- Profile data is encrypted end to end using a per-organization key.

0 commit comments

Comments
 (0)