fix: #3608944 Stop leaking headless Chromium and temp screenshots on error paths

Fixes #3608944: https://www.drupal.org/project/ai_playwright/issues/3608944

Problem

Two resource leaks in the browser_preview AI Agent tool error paths:

  • scripts/browser-capture.mjs caught errors and called fail(), which runs process.exit(0). That does not unwind the surrounding finally { browser.close() }, so headless Chromium is orphaned on every error path (navigation timeout, bad page, screenshot failure).
  • PlaywrightRunner::capture() only unlinked the temp screenshot PNG on the success path. A non-zero/timed-out Node run, or an ok=false JSON result, left the temp PNG in the temp directory forever.

Fix

  • Close the browser in the catch block before calling fail() in scripts/browser-capture.mjs.
  • Unlink the temp screenshot file on every exit path of PlaywrightRunner::capture().

Verified locally: temp dir stays empty after a forced error path, capture still succeeds on the happy path, phpcs clean.

AI-Generated: Yes (Used Claude Code).

Checkpoints

  • 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

Merge request reports

Loading