feat: optional idle auto-close for unused browser#223
Open
7sorium wants to merge 1 commit into
Open
Conversation
Adds an opt-in idle timeout that closes the browser after a period of inactivity, so long-lived sessions don't accumulate stale headless browsers eating memory. Disabled by default — enabled only when the PLAYWRIGHT_IDLE_TIMEOUT_MS environment variable is set to a positive number of milliseconds. The timer resets on every browser tool call and is cleared when the browser closes or disconnects. No behavior change unless explicitly configured. Verified: with PLAYWRIGHT_IDLE_TIMEOUT_MS=3000 a browser left idle is closed automatically while the server stays alive; with the variable unset the browser stays open. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.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.
Motivation
When a session keeps the MCP server alive but stops using the browser, the launched Chromium stays open indefinitely — there's currently no inactivity timeout. Across many long-lived sessions these idle headless browsers accumulate and consume significant memory.
What this adds
An opt-in, off-by-default idle auto-close:
PLAYWRIGHT_IDLE_TIMEOUT_MSenvironment variable is set to a positive number of milliseconds (e.g.1800000= 30 min).unref()'d so it never keeps the process alive on its own.No behavior change unless configured — with the variable unset,
getIdleTimeoutMs()returns0and the timer never arms.Verification
PLAYWRIGHT_IDLE_TIMEOUT_MS=3000: a browser left idle is closed automatically ([Playwright MCP] Closing idle browser after 3000ms of inactivity) while the server stays alive.Notes
Pairs naturally with #222 (orphan cleanup on disconnect) but is fully independent and can be merged on its own, in any order.
🤖 Generated with Claude Code