AIStudio2API
AIStudio2API copied to clipboard
[Bug] camoufoxnative.Start launches Firefox with camoufox-cache and runs restoreStartupCache before acquiring retiring worker's cacheLock
发生了什么 / 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
- Old worker calls
Retire()(internal/camoufoxnative/worker.go):- Marks
retiringCaches.Store(directory, struct{}{})and runsworker.shutdown()in a background goroutine while still holdingworker.cacheLock. shutdown()navigates tabs toabout:blank, waitspageUnloadDelay(1s), sendssession.end, kills the browser process, runssaveStartupCache(profile, cacheDirectory)(os.RemoveAll+os.Renameonprofile-startup), and finally unlockscacheLock.
- Marks
- Before
shutdown()finishes,camoufoxnative.Startruns for the same account:lockAccountCachesees the lock held andretiringCachespresent, returning(cacheDirectory, cacheLock, pending=true, nil).Startsetsoptions.CacheDirectory = cacheDirectoryand immediately callsworker.launch(...).launchBrowser(internal/camoufoxnative/launcher.go) copiesprofile-startupviarestoreStartupCache(options.CacheDirectory, profile)and launches Firefox withbrowser.cache.disk.parent_directoryset tocacheDirectory.Startonly waits oncacheLock.TryLockContext(ctx, cacheLockPoll)afterworker.launchandsession.newhave already started Firefox.
Consequence
- Two Firefox processes for the same account concurrently point
browser.cache.disk.parent_directoryat the samecamoufox-cachedirectory. restoreStartupCachereadsprofile-startupconcurrently with the retiring worker'ssaveStartupCache(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