Skip to content

fix(camoufoxnative): 启动时结束遗留 Camoufox 进程组再删除 profile - #106

Closed
RyoKusnadi wants to merge 1 commit into
Mag1cFall:mainfrom
RyoKusnadi:fix/kill-stale-camoufox-on-startup
Closed

RyoKusnadi wants to merge 1 commit into
Mag1cFall:mainfrom
RyoKusnadi:fix/kill-stale-camoufox-on-startup

Conversation

@RyoKusnadi

Copy link
Copy Markdown

改动 / Changes

Problem: On macOS and Linux, if the service exits without a clean Close() (crash, SIGKILL, force-quit, portable update), its Camoufox process groups keep running under PID 1. The next start deletes their profile directories but leaves the processes alive, so each unclean restart leaks about 1.5 GB (5 workers by default). Fixes #98.

How it happens: the service dies, the OS releases the .aistudio2api-profile.lock flock, and RemoveStaleProfiles() at the next start locks and deletes the profile while its Camoufox is still running (internal/camoufoxnative/profile.go).

Cause: the profile does not record which process uses it, so RemoveStaleProfiles() can only delete the directory.

Fix:

  • After command.Start() in launchBrowser, write the Camoufox PID (which is also its process group id, since it starts with Setpgid) to .aistudio2api-profile.pid in the profile.
  • In RemoveStaleProfiles(), after taking the profile lock and before deleting, read that file and run ps -ww -o pgid=,args= -p PID. Only if the process is its own group leader and its command line ends with -profile <this exact profile path>, send SIGKILL to that group.
  • Windows is unchanged (Job Objects already kill the tree); it gets empty stubs.

Nothing is killed by name. A stale or reused PID that does not match the profile path is left alone.

Scope: The issue also suggests Pdeathsig on Linux. It is not added: it does not exist on macOS and fires on thread exit, so the startup cleanup is the one fix for both platforms. Left as a possible follow-up.

验证 / Verification

  • TestIsProfileBrowser (matching rules), TestRemoveStaleProfilesWithRecordedProcess (fake PID 2147483646, temp dir as TMPDIR), TestTerminateStaleBrowserIgnoresBadRecord (missing, non-numeric, 0, 1, negative PIDs). No browser is started and no real process is signalled.
  • Before the fix the new tests do not compile (undefined: profilePIDName), since the helpers did not exist.
  • go vet, go test ./internal/camoufoxnative pass; GOOS=windows go vet passes; gofmt -l is clean.
  • Not run: a real Camoufox kill/restart on macOS or Linux, the full suite.

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

Labels

None yet

Projects

None yet

2 participants