Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Docker] userDataDir creates new sessions instead of reusing existing ones - data persistence not working #4913

@felinto-dev

Description

@felinto-dev

Issue Description

When using --user-data-dir parameter with Browserless v2 in Docker, each new connection creates a new browser session instead of reusing the existing one with the same userDataDir. This prevents data persistence (localStorage, cookies, login sessions) between connections.

Environment

  • Browserless Version: v2 (self-hosted Docker)
  • Docker Setup: Standard containerized deployment
  • Connection Method: WebSocket via puppeteer-extra
  • Endpoint Format: ws://host/?token=xxx&--user-data-dir=/tmp/session-name&launch={...}

Expected Behavior

  1. First connection creates session with userDataDir /tmp/session-1
  2. Save data (localStorage, cookies) in the session
  3. Disconnect from session (session remains active)
  4. Second connection with same userDataDir should reuse existing session
  5. Data should persist and be accessible in the reconnected session

Actual Behavior

  1. First connection creates session ID-A with userDataDir /tmp/session-1
  2. Data is saved successfully ✅
  3. Disconnect from session ✅
  4. Second connection creates NEW session ID-B with same userDataDir ❌
  5. All data is lost - localStorage and cookies are empty ❌

Test Results

API Response Analysis

// First connection
{
  "id": "8056abdc-ffc2-4666-aafc-05ffabaae90f",
  "userDataDir": "/tmp/session-test",
  "isTempDataDir": false,
  "numbConnected": 1
}

// Second connection (same userDataDir)
{
  "id": "6da99604-27f1-4714-89d0-bdcd3143a9b1", // ← Different ID!
  "userDataDir": "/tmp/session-test",             // ← Same userDataDir
  "isTempDataDir": false,
  "numbConnected": 1
}

Data Persistence Test

// First session - save data
localStorage.setItem('test-data', JSON.stringify({
  timestamp: Date.now(),
  message: 'Login session data'
}));
document.cookie = 'session-token=abc123; path=/; max-age=3600';

// Second session - check data
localStorage.getItem('test-data'); // → null ❌
document.cookie;                   // → "" ❌

Tested Configurations

✅ Working (userDataDir accepted)

  • Path format: /tmp/custom-session
  • API shows: "isTempDataDir": false
  • UserDataDir appears correctly in sessions API

❌ Not Working (session reuse)

  • New session created each time
  • Data doesn't persist between sessions
  • No automatic session reuse

Code Example

const puppeteerExtra = require('puppeteer-extra');

const userDataDir = '/tmp/persistent-session';
const launchArgs = {
  stealth: true,
  args: ['--proxy-server=http://proxy:5432']
};

const queryParams = new URLSearchParams({
  token: 'your-token',
  timeout: '300000',
  '--user-data-dir': userDataDir,
  launch: JSON.stringify(launchArgs)
}).toString();

const endpoint = `ws://browserless-host/?${queryParams}`;

// First connection
const browser1 = await puppeteerExtra.connect({
  browserWSEndpoint: endpoint
});
// ... save data ...
await browser1.disconnect();

// Second connection - should reuse session but creates new one
const browser2 = await puppeteerExtra.connect({
  browserWSEndpoint: endpoint // Same endpoint, same userDataDir
});
// ... data is lost ...

Related Issues

This issue is related to:

Expected Solution

The system should:

  1. Check if an active session exists with the specified userDataDir
  2. If exists, reuse the existing session instead of creating new one
  3. Maintain data persistence (localStorage, cookies, session state)
  4. Allow reconnection to existing sessions via WebSocket

Use Case

This functionality is essential for:

  • Maintaining login sessions across script executions
  • Preserving user preferences and application state
  • Reducing resource usage by reusing browser instances
  • Supporting long-running automation workflows

Additional Information

  • The userDataDir path is correctly recognized and appears in the API
  • Sessions remain active after disconnection
  • The issue seems specific to Docker deployments
  • Manual reconnection via /devtools/browser/{id} endpoints return 404

Would appreciate any guidance on configuration changes or workarounds to enable proper session reuse with userDataDir.

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