Repository navigation
fix(camoufoxnative): 启动时结束遗留 Camoufox 进程组再删除 profile - #106
Closed
RyoKusnadi wants to merge 1 commit into
Closed
RyoKusnadi wants to merge 1 commit into
RyoKusnadi wants to merge 1 commit into
Conversation
Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改动 / 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.lockflock, andRemoveStaleProfiles()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:
command.Start()inlaunchBrowser, write the Camoufox PID (which is also its process group id, since it starts withSetpgid) to.aistudio2api-profile.pidin the profile.RemoveStaleProfiles(), after taking the profile lock and before deleting, read that file and runps -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.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
Pdeathsigon 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 asTMPDIR),TestTerminateStaleBrowserIgnoresBadRecord(missing, non-numeric, 0, 1, negative PIDs). No browser is started and no real process is signalled.undefined: profilePIDName), since the helpers did not exist.go vet,go test ./internal/camoufoxnativepass;GOOS=windows go vetpasses;gofmt -lis clean.