Skip to content

persist_browser_session flag saves sessions but never retrieves them on subsequent runs #4390

@pengu-fr

Description

@pengu-fr

Problem

The workflow settings UI provides a "Save & Reuse Browser Session" checkbox, clearly indicating that the expected behavior is to both save and reuse browser sessions across workflow runs. However, while sessions are saved at the end of each run, they are never retrieved on subsequent runs.

This means cookies, localStorage, and login states are lost between workflow executions, defeating the purpose of the feature.

Expected Behavior

When "Save & Reuse Browser Session" is enabled:

  1. First run: Browser session is created and saved
  2. Subsequent runs: The same browser session is loaded, preserving cookies, localStorage, and authentication state

Actual Behavior

Each workflow run starts with a fresh browser context, ignoring any previously saved session data. The flag only triggers a save operation at the end, but the saved session is never reused.

Related Issues


Root Cause Analysis

Issue 1: Sub-workflows don't inherit persist_browser_session

When task_v2 blocks create sub-workflows, they don't inherit the parent workflow's persist_browser_session setting. The browser state is created before the block executes, using the sub-workflow's settings (which default to false).

Issue 2: No mechanism to load existing sessions

Even when persist_browser_session=True, there's no logic to check for and load an existing session directory when launching the browser.

Issue 3: SingletonSocket/Lock file copy errors

When saving browser profiles, Chromium's lock files (SingletonSocket, SingletonLock, SingletonCookie) cause copy errors because they are Unix sockets, not regular files.


Solution

1. Propagate persist_browser_session from parent workflows

Check parent workflow for persist_browser_session setting in all 7 call sites where browser state is created:

  • skyvern/services/task_v2_service.py (3 locations)
  • skyvern/forge/sdk/workflow/models/block.py (2 locations)
  • skyvern/forge/agent.py (1 location)
  • skyvern/core/script_generations/script_skyvern_page.py (1 location)

2. Create and reuse persistent session directory

Create a stable session directory path based on organization_id and workflow_permanent_id, and pass it to the browser factory to use as user_data_dir. If the directory was manually deleted, it is simply recreated empty and the workflow starts with a fresh session.

3. Skip Chromium lock files when saving

Filter out SingletonSocket, SingletonLock, and SingletonCookie files when copying browser profiles.


Test Results

Tested with a workflow that visits a website displaying a cookie consent banner:

  • First run: Cookie banner appears and is dismissed
  • Second run: Cookie banner does not appear (cookies were preserved)

This confirms the fix correctly saves and restores browser session state across workflow runs.


Known Limitations

  1. Concurrent runs not supported: Chromium locks the user_data_dir, so only one workflow run can use a persistent session at a time.

  2. Not compatible with browser_profile_id: If browser_profile_id is set, persist_browser_session is ignored (browser profiles take precedence).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions