BlogDebuggingturno-browser

Closing an idle Chrome without closing it mid-call

turno-browser closes an agent's Chrome after 15 minutes without a tool call, or 5 under memory pressure, and opens it again on the next one. The first version counted from the start of the last call, so a call longer than the limit had Chrome closed under it.

5 min read

View sourceFeed

An agent's Chrome can stay open for hours after the last thing it did. In turno-browser 0.3.0 it closes after 15 minutes without a tool call, headed too, and the next call opens it again with the same profile. TURNO_BROWSER_IDLE changes the limit, and "0" keeps Chrome open. Since 0.11.0 the limit is 5 minutes when the system reports memory pressure as Chrome starts, through the low-resource preset that 0.9.0 added.

The first version of that limit counted from the start of the last call. A call that took longer than the limit, on a slow page or a busy machine, failed halfway with "Target page, context or browser has been closed". The fix landed before 0.3.0 was tagged, so the idle close and its fix went out together.

Where the countdown started

The limit is a timer that every tool call restarts. The timer is upstream's design, from Playwright MCP, and turno keeps the same shape for the engines it starts:

packages/browser/src/idle-timer.tsTypeScript
/** Upstream's `IdleTimer`: every tool call restarts the countdown. */
export class IdleTimer implements IdleTimerLike {
  private timer: NodeJS.Timeout | undefined;
  constructor(
    private readonly ms: number,
    private readonly onIdle: () => void,
  ) {}
  poke(): void {
    this.dispose();
    this.timer = setTimeout(this.onIdle, this.ms); 
  }
  dispose(): void {
    clearTimeout(this.timer);
    this.timer = undefined;
  }
}

Nothing is wrong with the timer. The problem was when it was poked. Upstream's BrowserBackend pokes it at the start of a call and never at the end, so the countdown runs while the call runs. With the limit at L and a call that takes longer than L:

MomentCountdown from the start of the callCountdown from its end
The call startsstarts countingstops
L after the start, call still runningfires: Chrome closes, the call failsnothing
The call endsalready firedstarts counting
L after the end, no new callfires: Chrome closes

At the default of 15 minutes a single call rarely runs that long. With a short TURNO_BROWSER_IDLE, it is easy to reach.

Holding the timer during a call

The fix puts a wrapper between upstream's backend and the timer. Every call holds it, and the countdown only runs when no call does:

packages/browser/src/host.tsTypeScript
  /** Upstream's poke at the start of a call: ignored while a call holds the timer. */
  poke(): void {
    if (this.calls === 0 && this.open()) this.inner.poke();
  }

  /** Stops the countdown until the returned function runs (once) at the end of the call. */
  begin(): () => void {
    this.calls++;
    this.inner.dispose();
    let ended = false;
    return () => {
      if (ended) return;
      ended = true;
      this.calls--;
      this.poke(); 
    };
  }

Where a call used to run running.launched.idleTimer?.poke(), it now runs endCall = running.idle?.begin(), and the call ends with endCall?.() in a finally, so an error ends it too.

The wrapper handles three cases. A counter, not a flag, because two calls can overlap and the countdown must wait for the last one. The function begin returns does its work once, so a call that ends on two paths does not release a hold it no longer has. And the open check keeps the timer from starting again for a Chrome that has already closed, which would only close it a second time.

The tests that came with it cover each one: a call that waits until the test releases it must find the timer disarmed and leave it armed with a final poke, two overlapping calls keep it stopped until the second ends, and a browser that closed during a call does not start counting again. The change is pull request 64.

How others handle it

The Playwright MCP and agent-browser rows come from each project's own docs, as listed on the alternatives page, checked on 2026-11-19. The turno-browser row comes from its own docs and changelog: the 15 minutes from Tabs and lifecycle, the 5 under memory pressure from the low-resource preset, and "headed too" from the 0.3.0 entry of the changelog.

ProjectWithout calls
turno-browserChrome closes after 15 minutes, or 5 under memory pressure, headed too, and opens again on the next call
Playwright MCP--idle-timeout closes the browser after an hour when headless; a headed browser stays open unless you set it
agent-browserThe daemon closes a headless browser after an hour without commands; headed browsers are exempt

The difference is the headed case: turno-browser applies the same limit to a Chrome with a window.

The same message, another cause

The same "Target page, context or browser has been closed" error had a second cause: a call that started while the tab governor of 0.3.0 was still closing a popup or a parked tab. Since 0.4.0 a call waits for the governor before it runs, as told in what the flaky browser tests measured.

A Chrome nobody closed

The idle limit covers a server that is alive. A server that is killed with SIGKILL never runs its exit handlers, and Playwright only kills Chrome's process group from those handlers, so the Chrome stays open on the profile. The same release, in pull request 52, handles both ends.

On start, turno-browser-mcp closes a Chrome that an earlier run left open, under strict conditions: the profile's lock names the process, the process table confirms it is a Chrome browser process for exactly that profile, and its parent is gone. A Chrome that another live process drives is never touched. When it closes one, it says so on stderr.

On shutdown, when its stdin closes (for a server on stdio) or it gets SIGTERM or SIGHUP, it puts a deadline on itself. The MCP SDK sends SIGTERM 2 seconds after closing a server's stdin and SIGKILL 2 seconds later, while the program's own watchdog allows itself 15 seconds to close the browser. The guard gives a graceful close 1.5 seconds, then sends SIGTERM to Chrome's browser process and gives it 1 second more, and the exit handler sends SIGKILL to the whole group if it is still there:

packages/browser/src/lifecycle.tsTypeScript
export function installShutdownGuard(userDataDir: string, opts: ShutdownOptions = {}): void {
  const graceMs = opts.graceMs ?? 1500;
  const termMs = opts.termMs ?? 1000;

That is about 2.5 seconds in all, inside the SDK's 4. SIGTERM goes to the browser process alone first because it shuts its helpers down and writes the profile, cookies included. SIGHUP is turned into SIGTERM, so the server closes the browser instead of dying with it open.

What is still open

The sweep only closes a Chrome whose parent is gone. A Chrome opened by hand on the identity's profile holds it while it lives, and turno browser doctor names it and asks you to close the window; no command does it for you.

A Chrome that does not stop within 5 seconds of SIGKILL (since 0.4.0) is reported as one that "did not stop" and left alone.

The full list of what closes and when, with each limit and how to change it, is on Tabs and lifecycle. A Chrome a killed run left behind shows in doctor as a Chrome left running with no server, and turno browser doctor --fix closes it.