Skip to content

fix(browser): import Chromium cookies on Windows despite the live DB lock#8130

Open
lucasdaniellopes wants to merge 1 commit into
stablyai:mainfrom
lucasdaniellopes:fix/windows-cookie-import-staging-lock
Open

fix(browser): import Chromium cookies on Windows despite the live DB lock#8130
lucasdaniellopes wants to merge 1 commit into
stablyai:mainfrom
lucasdaniellopes:fix/windows-cookie-import-staging-lock

Conversation

@lucasdaniellopes

Copy link
Copy Markdown

Problem

On Windows, importing cookies from Chrome/Chromium browsers fails with:

Could not create staging cookie database.

(After the #7882 fix, the earlier unable to open database file / "Try closing Chrome first" step is passed — this is the next wall, and it blocks Windows regardless of whether the source browser is closed.)

Root cause

importCookiesFromBrowser copies the target partition's live Cookies DB into a staging file before populating it:

copyFileSync(liveCookiesPath, stagingCookiesPath) // liveCookiesPath = Partitions/<p>/Network/Cookies

On Windows, Electron's network service holds that live DB open with an exclusive lock, so any external open of the file fails. I verified this empirically against a running Orca profile DB — both fail with EBUSY:

copyFileSync : FAILED -> EBUSY
readFileSync : FAILED -> EBUSY   // not just CopyFileEx's share mode — the handle is exclusive

Because the copy is a hard prerequisite, its failure aborts the whole import — even though the in-memory cookies.set() path (the primary import mechanism, already run further down) needs no file copy at all. This makes Chromium cookie import effectively impossible on Windows while Orca is running (and Orca must be running to perform the import). macOS/Linux are unaffected because their SQLite locks are advisory, so the copy succeeds.

Fix

Make the SQLite staging step best-effort. When the live DB can't be copied, skip the staging/cold-start-swap path and import cookies into the live session via cookies.set() — the path that already runs on every platform.

  • macOS/Linux: unchanged (staging still succeeds; full behavior preserved, including the cold-start fallback for cookies that fail cookies.set() validation).
  • Windows: import now succeeds. The only degradation is that cookies which fail cookies.set() validation (rare non-ASCII values) can't be recovered via the cold-start swap and are dropped, instead of the entire import failing.

All staging DB operations are guarded so the pipeline runs with stagingDb === null.

Testing

  • New unit test: falls back to in-memory import when the live target DB cannot be copied — simulates the Windows EBUSY on the staging copy and asserts the import still succeeds via cookies.set() and leaves no staging artifact. Confirmed it fails without this change (Could not create staging cookie database.ok:false) and passes with it.
  • browser-cookie-import.test.ts: 20/20 passing.
  • tsgo typecheck, oxlint, and check:max-lines-ratchet: all green.

Related: #6875 (the Windows reproduction was reported there but the issue was closed as macOS-only).

…lock

On Windows the live partition Cookies DB is held with an exclusive lock by
Electron's network service, so copyFileSync (and even readFileSync) fail with
EBUSY. Cookie import copied that live DB into a staging file before populating
it, so the copy failure aborted the whole import with "Could not create staging
cookie database." — making Chromium cookie import impossible on Windows while
Orca is running.

Make staging best-effort: when the live DB can't be copied, skip the SQLite
staging / cold-start-swap path and import cookies into the live session via
cookies.set() (the primary path that already runs on every platform).
macOS/Linux behavior is unchanged; on Windows only cookies that fail
cookies.set() validation (rare non-ASCII values) are dropped instead of the
entire import failing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Chromium cookie importing now treats copying the live Cookies database into staging as best-effort. When staging is unavailable, the importer skips staging database operations and continues with in-memory cookies.set() calls, while conditional cleanup and pending recovery preserve the existing staged path. A test covers Windows-style EBUSY copy failure, successful in-memory import, and an empty staging directory.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the problem, fix, and testing, but it omits required Screenshots, AI Review Report, Security Audit, and Notes sections. Add the missing template sections, including screenshots/no visual change, AI review findings, security audit, and platform-specific notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: Windows Chromium cookie import now works despite the live DB lock.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 23aef0f8-18e6-455b-b71f-e21ade869cbf

📥 Commits

Reviewing files that changed from the base of the PR and between 970d446 and d3d15eb.

📒 Files selected for processing (2)
  • src/main/browser/browser-cookie-import.test.ts
  • src/main/browser/browser-cookie-import.ts

Comment on lines +1741 to +1761
if (stagingAvailable && memoryFailed > 0) {
// Why: some cookies couldn't be loaded via cookies.set() (non-ASCII values
// or other validation failures). Keep the staging DB so the next cold start
// picks them up from SQLite where CookieMonster reads them without validation.
browserSessionRegistry.setPendingCookieImport(targetPartition, stagingCookiesPath)
diag(` staged at ${stagingCookiesPath} for ${memoryFailed} cookies that need restart`)
} else {
try {
unlinkSync(stagingCookiesPath)
} catch {
/* best-effort */
if (stagingAvailable) {
try {
unlinkSync(stagingCookiesPath)
} catch {
/* best-effort */
}
}
diag(` all cookies loaded in-memory — no restart needed`)
// Why: without staging (e.g. the Windows exclusive lock on the live DB), cookies
// that fail cookies.set() validation cannot be recovered via a cold-start swap.
diag(
stagingAvailable || memoryFailed === 0
? ` all cookies loaded in-memory — no restart needed`
: ` imported in-memory; ${memoryFailed} non-loadable cookies dropped (no staging)`
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="src/main/browser/browser-cookie-import.ts"

echo '--- context around lines 1680-1785 ---'
sed -n '1680,1785p' "$file" | cat -n

echo
echo '--- search for imported/memoryFailed/summary.importedCookies ---'
rg -n "imported|memoryFailed|importedCookies|stagingAvailable" "$file"

Repository: stablyai/orca

Length of output: 6420


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="src/main/browser/browser-cookie-import.ts"

echo '--- exact lines around summary assignment ---'
sed -n '1760,1785p' "$file" | cat -n

echo
echo '--- definition / references for importedCookies ---'
rg -n "importedCookies|CookieImportSummary|summary =" src/main/browser/browser-cookie-import.ts

Repository: stablyai/orca

Length of output: 1425


Count usable cookies in the no-staging path. summary.importedCookies still includes cookies that fail cookies.set() when staging is unavailable, so the reported count can exceed the number of cookies actually available after import. Use the in-memory success count there, or document that the field counts attempted imports.

@euclodius

Copy link
Copy Markdown

Confirmed this exact failure on the latest public build.

Environment:

  • Orca v1.4.134
  • Windows 11 desktop app
  • Project Runtime configured as WSL / Ubuntu 24.04
  • Source browser: Google Chrome on Windows
  • Target: default Orca browser profile

Observed after the #7882-era fix:

Could not create staging cookie database

This matches the failure mode described in this PR. I do not think WSL is the direct root cause here, since the failing part appears to be the Windows-side Electron/Orca target partition cookie DB lock. But it would still be useful to include Windows desktop + WSL runtime/worktree setup in the smoke matrix, because that is the real environment where this browser handoff/import flow is being used and where the previous #6875 Windows reproduction also came from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants