AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

[Bug] camoufoxnative.Start launches Firefox with camoufox-cache and runs restoreStartupCache before acquiring retiring worker's cacheLock

Open photobrand opened this issue 1 week ago • 0 comments

发生了什么 / What happened

Summary

When a camoufoxnative.Worker is retired asynchronously via Retire() and a new Worker for the same account starts before shutdown completes, camoufoxnative.Start launches the new Camoufox process pointing at camoufox-cache and restores profile-startup before waiting on cacheLock.

Observed Race in Code

  1. Old worker calls Retire() (internal/camoufoxnative/worker.go):
    • Marks retiringCaches.Store(directory, struct{}{}) and runs worker.shutdown() in a background goroutine while still holding worker.cacheLock.
    • shutdown() navigates tabs to about:blank, waits pageUnloadDelay (1s), sends session.end, kills the browser process, runs saveStartupCache(profile, cacheDirectory) (os.RemoveAll + os.Rename on profile-startup), and finally unlocks cacheLock.
  2. Before shutdown() finishes, camoufoxnative.Start runs for the same account:
    • lockAccountCache sees the lock held and retiringCaches present, returning (cacheDirectory, cacheLock, pending=true, nil).
    • Start sets options.CacheDirectory = cacheDirectory and immediately calls worker.launch(...).
    • launchBrowser (internal/camoufoxnative/launcher.go) copies profile-startup via restoreStartupCache(options.CacheDirectory, profile) and launches Firefox with browser.cache.disk.parent_directory set to cacheDirectory.
    • Start only waits on cacheLock.TryLockContext(ctx, cacheLockPoll) after worker.launch and session.new have already started Firefox.

Consequence

  • Two Firefox processes for the same account concurrently point browser.cache.disk.parent_directory at the same camoufox-cache directory.
  • restoreStartupCache reads profile-startup concurrently with the retiring worker's saveStartupCache (os.RemoveAll / os.Rename), risking missing/corrupted startup cache or file-in-use errors on Windows.

Suggested Fix

Acquire cacheLock.TryLockContext(ctx, cacheLockPoll) when pending == true before calling worker.launch (so restoreStartupCache and browser.cache.disk.parent_directory only run once the retiring process has exited and finished saveStartupCache).

版本 / Version

v0.3.13

photobrand avatar Nov 21 '26 10:11 photobrand