D4Vinci/Scrapling · error · RuntimeError

Session has been already started

Error message

Session has been already started

What it means

Raised by the sync StealthySession when start() (or entering the `with` block) is called on a session that is already alive. The session's browser/context are single-use until stop()/close is called, so a second start is treated as a programming error rather than a no-op.

Source

Thrown at scrapling/engines/_browsers/_stealth.py:105

                elif self._config.proxy_rotator:
                    self.browser = self.playwright.chromium.launch(**self._browser_options)
                else:
                    persistent_options = (
                        self._browser_options | self._context_options | {"user_data_dir": self._user_data_dir}
                    )
                    self.context = self.playwright.chromium.launch_persistent_context(**persistent_options)

                if self.context:
                    self.context = self._initialize_context(self._config, self.context)

                self._is_alive = True
            except Exception:
                # Clean up playwright if browser setup fails
                self.playwright.stop()
                self.playwright = None
                raise
        else:
            raise RuntimeError("Session has been already started")

    def _cloudflare_solver(self, page: Page) -> None:  # pragma: no cover
        """Solve the cloudflare challenge displayed on the playwright page passed

        :param page: The targeted page
        :return:
        """
        self._wait_for_networkidle(page, timeout=5000)
        challenge_type = self._detect_cloudflare(ResponseFactory._get_page_content(page))
        if not challenge_type:
            log.error("No Cloudflare challenge found.")
            return None
        else:
            log.info(f'The turnstile version discovered is "{challenge_type}"')
            if challenge_type == "non-interactive":
                while "<title>Just a moment...</title>" in (ResponseFactory._get_page_content(page)):
                    log.info("Waiting for Cloudflare wait page to disappear.")
                    page.wait_for_timeout(1000)

View on GitHub (pinned to 5d213a2d47)

Solutions

  1. Call session.stop() (or exit the `with` block) before starting the same session again
  2. Create a fresh StealthySession instance for each independent browsing session
  3. Use the session as a context manager exactly once: `with StealthySession(...) as s: s.fetch(...)`

Example fix

// before
session = StealthySession()
session.start()
... 
session.start()  # RuntimeError

// after
session = StealthySession()
session.start()
...
session.stop()
session.start()  # ok
Defensive patterns

Strategy: validation

Validate before calling

if getattr(session, '_is_alive', False):
    session.stop()  # or raise your own clear error
session.start()

Type guard

def is_session_idle(s) -> bool:
    return not getattr(s, '_is_alive', False)

Try / catch

try:
    session.start()
except RuntimeError as e:
    if 'already started' in str(e):
        session.stop(); session.start()
    else:
        raise

Prevention

When it happens

Trigger: Calling `session.start()` twice, re-entering `with StealthySession(...) as s:` after an earlier start without an intervening stop(), or wrapping an already-started session in a nested context manager.

Common situations: Reusing a session object across loop iterations, retries that re-enter the context manager, or mixing explicit start() calls with `with` usage on the same instance.

Related errors


AI-assisted analysis of D4Vinci/Scrapling@5d213a2d47 (2026-08-14). Data as JSON: /api/errors/39d5b5633a331eec. Report an issue: GitHub.