Skip to content

Manage a browser session

Learn how to manage session termination and inspect completed sessions

Updated View as Markdown

While Browsers automatically handles session termination when you disconnect, understanding how sessions end helps you debug failed runs, manage long-running sessions, optimize resource usage, and investigate timeouts or errors.

Session termination

Browser sessions can end in these ways:

  1. Automatic timeout Sessions have a default timeout configured at the application level, which can be customized when creating a session. For longer-running tasks, enable keep alive.

  2. Manual termination You can end sessions explicitly by closing the browser programmatically (browser.close() or driver.quit()), using the Sessions API, or releasing keep-alive sessions when no longer needed.

  3. Unhandled errors Unhandled errors in your automation code can cause your script to disconnect from the browser, ending the session prematurely. Common scenarios include network interruptions, uncaught exceptions, or exceeded resource limits.

    To prevent premature termination, make sure to implement proper error handling and cleanup in your code.

  4. CDP inactivity timeout CDP (Chrome DevTools Protocol) connections are closed after 10 minutes without any CDP commands. If your script holds a session open without sending CDP commands for this period, the connection will be terminated.

    To keep the connection alive, send a periodic heartbeat, a lightweight CDP command at a regular interval:

    // Send a heartbeat every 5 minutes to prevent CDP inactivity timeout
    setInterval(async () => {
      await page.evaluate(() => undefined);
    }, 5 * 60 * 1000);
    import asyncio
    
    # Send a heartbeat every 5 minutes to prevent CDP inactivity timeout
    async def heartbeat(page):
        while True:
            try:
                await asyncio.sleep(5 * 60)
                await page.evaluate("undefined")
            except Exception:
                break
    
    asyncio.create_task(heartbeat(page))

Session timeout settings

Configure timeouts at two levels:

Application level Set the default timeout for all sessions in your application settings. This acts as the fallback when no session-specific timeout is set.

Session level Override the application timeout for specific sessions when creating them. This gives you fine-grained control over individual session durations.

Debugging completed sessions

The Session Inspector is your primary tool for analyzing completed sessions:

Measuring usage

Track and analyze your browser session usage through multiple interfaces:

console Your central hub at agent.mq/overview shows total browser minutes, active sessions, usage trends, and billing information.

Sessions list Browse your session history at agent.mq/sessions to view duration, status, and resource consumption for individual sessions.

For programmatic access to these metrics, see the Measuring Usage Guide.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close