Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/playwright-core/src/tools/backend/browserBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ export class BrowserBackend extends EventEmitter<{ disconnected: [] }> implement
}

async callTool(name: string, rawArguments: mcpServer.CallToolRequest['params']['arguments'] & { _meta?: Record<string, any> } = {}, signal?: AbortSignal): Promise<mcpServer.CallToolResult> {
this._idleTimer?.poke();
this._idleTimer?.callStarted();
try {
return await this._callTool(name, rawArguments, signal);
} finally {
this._idleTimer?.callFinished();
}
}

private async _callTool(name: string, rawArguments: mcpServer.CallToolRequest['params']['arguments'] & { _meta?: Record<string, any> }, signal?: AbortSignal): Promise<mcpServer.CallToolResult> {
const json = !!rawArguments._meta?.json;
const formatError = (message: string): mcpServer.CallToolResult => ({
content: [{ type: 'text' as const, text: json ? JSON.stringify({ isError: true, error: message }, null, 2) : `### Error\n${message}` }],
Expand Down
32 changes: 29 additions & 3 deletions packages/playwright-core/src/tools/backend/idleTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,46 @@ export const defaultIdleTimeout = 60 * 60 * 1000;
export class IdleTimer {
private _timeout: number;
private _onIdle: () => void;
private _running = 0;
private _timer: NodeJS.Timeout | undefined;
private _disposed = false;

constructor(timeout: number, onIdle: () => void) {
this._timeout = timeout;
this._onIdle = onIdle;
}

callStarted() {
if (this._disposed)
return;
++this._running;
this._clearTimer();
}

callFinished() {
if (this._running > 0)
--this._running;
if (this._disposed)
return;
if (this._running === 0)
this._timer = setTimeout(this._onIdle, this._timeout).unref();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can restart the timer after browser_close, potentially keeping the closed browser alive longer than needed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Pushed a fix:

  • Added a _disposed flag to IdleTimer and separated clearing the active timer during calls (_clearTimer) from permanent disposal (dispose).
  • Guarded callStarted(), callFinished(), and poke() so that once disposed, the timer is never re-armed.
  • Explicitly dispose _idleTimer on browser_close / disconnect so the closed browser instance is not retained.
  • Added regression test does not restart the idle timer after browser_close in tests/mcp/idle-timeout.spec.ts.

}

poke() {
this.dispose();
this._timer = setTimeout(this._onIdle, this._timeout);
if (this._disposed)
return;
this._clearTimer();
if (!this._running)
this._timer = setTimeout(this._onIdle, this._timeout).unref();
}

dispose() {
private _clearTimer() {
clearTimeout(this._timer);
this._timer = undefined;
}

dispose() {
this._disposed = true;
this._clearTimer();
}
}
25 changes: 25 additions & 0 deletions tests/mcp/idle-timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ test('cdp endpoint only disconnects on idle and reconnects to the same pages', a
'close browser': 1,
});
});

test('does not close the browser while a tool call is running', async ({ startClient, server }) => {
const { client, stderr } = await startClient({
args: ['--idle-timeout=500'],
env: { DEBUG: 'pw:mcp:test' },
});

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.HELLO_WORLD },
});

// The wait outlasts the idle timeout, which only starts once the call completes.
expect(await client.callTool({
name: 'browser_wait_for',
arguments: { time: 1 },
})).toHaveResponse({
code: `await new Promise(f => setTimeout(f, 1 * 1000));`,
});

expect(formatLog(stderr())).toEqual({
'create browser (persistent)': 1,
'create context': 1,
});
});
Loading