{"record":{"id":"39d5b5633a331eec","repo":"D4Vinci/Scrapling","slug":"session-has-been-already-started-39d5b5","errorCode":null,"errorMessage":"Session has been already started","messagePattern":"Session has been already started","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"scrapling/engines/_browsers/_stealth.py","lineNumber":105,"sourceCode":"                elif self._config.proxy_rotator:\n                    self.browser = self.playwright.chromium.launch(**self._browser_options)\n                else:\n                    persistent_options = (\n                        self._browser_options | self._context_options | {\"user_data_dir\": self._user_data_dir}\n                    )\n                    self.context = self.playwright.chromium.launch_persistent_context(**persistent_options)\n\n                if self.context:\n                    self.context = self._initialize_context(self._config, self.context)\n\n                self._is_alive = True\n            except Exception:\n                # Clean up playwright if browser setup fails\n                self.playwright.stop()\n                self.playwright = None\n                raise\n        else:\n            raise RuntimeError(\"Session has been already started\")\n\n    def _cloudflare_solver(self, page: Page) -> None:  # pragma: no cover\n        \"\"\"Solve the cloudflare challenge displayed on the playwright page passed\n\n        :param page: The targeted page\n        :return:\n        \"\"\"\n        self._wait_for_networkidle(page, timeout=5000)\n        challenge_type = self._detect_cloudflare(ResponseFactory._get_page_content(page))\n        if not challenge_type:\n            log.error(\"No Cloudflare challenge found.\")\n            return None\n        else:\n            log.info(f'The turnstile version discovered is \"{challenge_type}\"')\n            if challenge_type == \"non-interactive\":\n                while \"<title>Just a moment...</title>\" in (ResponseFactory._get_page_content(page)):\n                    log.info(\"Waiting for Cloudflare wait page to disappear.\")\n                    page.wait_for_timeout(1000)","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/D4Vinci/Scrapling/blob/5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f/scrapling/engines/_browsers/_stealth.py#L87-L123","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call session.stop() (or exit the `with` block) before starting the same session again","Create a fresh StealthySession instance for each independent browsing session","Use the session as a context manager exactly once: `with StealthySession(...) as s: s.fetch(...)`"],"exampleFix":"// before\nsession = StealthySession()\nsession.start()\n... \nsession.start()  # RuntimeError\n\n// after\nsession = StealthySession()\nsession.start()\n...\nsession.stop()\nsession.start()  # ok","handlingStrategy":"validation","validationCode":"if getattr(session, '_is_alive', False):\n    session.stop()  # or raise your own clear error\nsession.start()","typeGuard":"def is_session_idle(s) -> bool:\n    return not getattr(s, '_is_alive', False)","tryCatchPattern":"try:\n    session.start()\nexcept RuntimeError as e:\n    if 'already started' in str(e):\n        session.stop(); session.start()\n    else:\n        raise","preventionTips":["Use the context-manager form (`with StealthySession()`) so start/stop pairing is automatic","Never call start() twice; check session._is_alive before manual lifecycle calls","One session object per logical run; create a new instance instead of restarting"],"tags":["session-lifecycle","stealth","sync","state-machine"],"backgroundTag":null,"analyzedSha":"5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f","analyzedAt":"2026-08-14T22:23:09.440Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}