Prevent AI browser testing from generating junk files and crashing your Mac
When AI agents run headless Chrome for UI testing, interrupted tests can leave behind massive amounts of junk files, causing Macs to fail bootup due to 41 million files in the temp directory. This issue can be prevented by setting up isolated temp directories, using cleanup traps, and optimizing Chrome flags.
Reddit r/ClaudeAI/u/bekircagricelik
Claude CodemacOSChrome
Added 2026-10-31 · originally published 2026-10-26
Steps
- 1Set up an isolated temporary folder for each test run (via per-task TMPDIR settings) and delete it upon task completion—whether successful or exiting via EXIT/INT/TERM.
- 2When launching Chrome through a wrapper script, kill the entire process group to prevent Chrome from becoming an orphaned process.
- 3Add flags --use-mock-keychain --password-store=basic to prevent dozens of test profiles from repeatedly spamming the macOS keychain.
- 4Before starting large-scale test runs, count the files in the temporary directory and refuse to start if they exceed the threshold.
- 5Run the command to check the current piled-up file count: find "$(getconf DARWIN_USER_TEMP_DIR)" | wc -l
Copy-ready prompt
I need to prevent garbage files from accumulating during automated browser tests. Please help me write a script or add logic to my code that assigns an independent [temporary directory path] for each test run, uses a trap to ensure cleanup upon script exit (including success, interruption, or crash), launches headless Chrome with the flags --use-mock-keychain --password-store=basic, and refuses to start if the temporary directory file count exceeds the [set threshold] before testing begins.
#AI Agent#Automated Testing#Chrome#Mac Operations
Related use cases
Summarized by AI from the original source - refer to the original post for details. Copyright belongs to the original author.