Problem/Motivation

The browser_preview AI Agent tool (backed by scripts/browser-capture.mjs and src/PlaywrightRunner.php) leaks resources on error paths:

  • scripts/browser-capture.mjs catches errors and calls fail(), which runs process.exit(0). This does NOT unwind the surrounding finally { browser.close() }, so the headless Chromium process is orphaned on every error path (navigation timeout, bad page, screenshot failure). Under load this leaks browser processes.
  • PlaywrightRunner::capture() only unlinks the temporary screenshot PNG on the success path. A non-zero/timed-out Node run, or an ok=false JSON result, leaves the temp PNG in the temp directory forever — an unbounded temp-directory leak.

Steps to reproduce

  1. Trigger the browser_preview AI Agent tool against a URL that times out or errors repeatedly.
  2. Observe orphaned chromium/headless_shell processes accumulating (e.g. ps aux | grep headless_shell).
  3. Observe /tmp/ai_playwright_*.png temp screenshot files accumulating and never being cleaned up.

Proposed resolution

  • In scripts/browser-capture.mjs, close the browser in the catch block before calling fail(), so process.exit(0) never bypasses browser cleanup.
  • In src/PlaywrightRunner.php, unlink the temp screenshot file on every exit path of capture() (success, non-zero/timed-out process, and ok=false JSON result), not only the success path.

Remaining tasks

  • ✅ File an issue about this project
  • ✅ Addition/Change/Update/Fix to this project
  • ✅ Testing to ensure no regression
  • ➖ Automated unit/functional testing coverage
  • ➖ Developer Documentation support on feature change/addition
  • ➖ User Guide Documentation support on feature change/addition
  • ➖ UX/UI designer responsibilities
  • ➖ Accessibility and Readability
  • ❌ Reviewed by a human
  • ❌ Code review by maintainers
  • ❌ Full testing and approval
  • ❌ Credit contributors
  • ❌ Review with the product owner
  • ❌ Update Release Notes
  • ❌ Release

User interface changes

  • N/A

API changes

  • N/A

Data model changes

  • N/A

Release notes snippet

  • N/A
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rajab natshah created an issue.