{"record":{"id":"f3b81463bf8d5ad6","repo":"D4Vinci/Scrapling","slug":"context-manager-has-been-closed","errorCode":null,"errorMessage":"Context manager has been closed","messagePattern":"Context manager has been closed","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"scrapling/engines/_browsers/_controllers.py","lineNumber":127,"sourceCode":"        :param page_action: Added for automation. A function that takes the `page` object, runs after navigation, and does the automation you need.\n        :param page_setup: A function that takes the `page` object, runs before navigation. Use it to register event listeners or routes that must be set up before the page loads.\n        :param extra_headers: A dictionary of extra headers to add to the request. _The referer set by `google_search` takes priority over the referer set here if used together._\n        :param disable_resources: Drop requests for unnecessary resources for a speed boost.\n            Requests dropped are of type `font`, `image`, `media`, `beacon`, `object`, `imageset`, `texttrack`, `websocket`, `csp_report`, and `stylesheet`.\n        :param blocked_domains: A set of domain names to block requests to. Subdomains are also matched (e.g., ``\"example.com\"`` blocks ``\"sub.example.com\"`` too).\n        :param wait_selector: Wait for a specific CSS selector to be in a specific state.\n        :param wait_selector_state: The state to wait for the selector given with `wait_selector`. The default state is `attached`.\n        :param network_idle: Wait for the page until there are no network connections for at least 500 ms.\n        :param load_dom: Enabled by default, wait for all JavaScript on page(s) to fully load and execute.\n        :param selector_config: The arguments that will be passed in the end while creating the final Selector's class.\n        :param proxy: Static proxy to override rotator and session proxy. A new browser context will be created and used with it.\n        :return: A `Response` object.\n        \"\"\"\n        static_proxy = kwargs.pop(\"proxy\", None)\n\n        params = _validate(kwargs, self, PlaywrightConfig)\n        if not self._is_alive:  # pragma: no cover\n            raise RuntimeError(\"Context manager has been closed\")\n\n        request_headers_keys = {h.lower() for h in params.extra_headers.keys()} if params.extra_headers else set()\n        referer = (\n            \"https://www.google.com/\" if (params.google_search and \"referer\" not in request_headers_keys) else None\n        )\n\n        for attempt in range(self._config.retries):\n            proxy: Optional[ProxyType] = None\n            if self._config.proxy_rotator and static_proxy is None:\n                proxy = self._config.proxy_rotator.get_proxy()\n            else:\n                proxy = static_proxy\n\n            with self._page_generator(\n                params.timeout, params.extra_headers, params.disable_resources, proxy, params.blocked_domains\n            ) as page_info:\n                final_response: List = [None]\n                xhr_captured: List = []","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/D4Vinci/Scrapling/blob/5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f/scrapling/engines/_browsers/_controllers.py#L109-L145","documentation":"Raised by sync `DynamicSession.fetch` (scrapling/engines/_browsers/_controllers.py:127) when `self._is_alive` is False — i.e. `fetch()` is called outside the session's `with` block or after it exited. The flag is set True in `__enter__` and False in `__exit__`, so this guards against fetching with a torn-down Playwright instance. Marked `no cover` because normal usage inside the context manager never sees it.","triggerScenarios":"`s = DynamicSession(); s.fetch(url)` (no `with`), or `with DynamicSession() as s: ...` followed by `s.fetch(url)` after the block; also fetching after `__exit__` ran because an earlier exception unwound the block.","commonSituations":"Refactoring code out of a `with` block and forgetting the session dies with it; storing the session globally and calling fetch lazily (e.g. in a callback that fires later); exception paths that exit the context while queued work still runs.","solutions":["Move every `fetch` inside the `with DynamicSession() as session:` block.","If fetches happen lazily, restructure so the context manager wraps the whole workload (queue + worker).","For one-off fetches, use the function-style API (e.g. `fetcher` / `DynamicSession` alternatives like `scrapling.fetchers`) that manages lifecycle per call.","Add a liveness assert before fetch in debug builds to catch misuse early."],"exampleFix":"# before\nwith DynamicSession() as session:\n    pass\nresp = session.fetch(url)  # RuntimeError: closed\n\n# after\nwith DynamicSession() as session:\n    resp = session.fetch(url)","handlingStrategy":"type-guard","validationCode":"def is_session_alive(session) -> bool:\n    return bool(getattr(session, '_is_alive', False))","typeGuard":"def session_can_fetch(session) -> bool:\n    \"\"\"True while the session's context manager is active.\"\"\"\n    return bool(getattr(session, '_is_alive', False))","tryCatchPattern":"try:\n    resp = session.fetch(url)\nexcept RuntimeError as e:\n    if 'closed' in str(e):\n        with DynamicSession() as fresh:  # restart and retry once\n            resp = fresh.fetch(url)\n    else:\n        raise","preventionTips":["Keep all fetch calls inside the `with` block.","Don't let queued/deferred work outlive the context manager.","Use one-shot fetchers for single requests instead of session objects."],"tags":["browser","lifecycle","context-manager","scrapling"],"backgroundTag":null,"analyzedSha":"5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f","analyzedAt":"2026-08-14T22:23:09.440Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}