{"record":{"id":"34eeff23cd12e462","repo":"aio-libs/aiohttp","slug":"site-site-is-not-registered-in-runner-self","errorCode":null,"errorMessage":"Site {site} is not registered in runner {self}","messagePattern":"Site (.+?) is not registered in runner (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_runner.py","lineNumber":393,"sourceCode":"                # remove_signal_handler is not implemented on Windows\n                pass\n\n    @abstractmethod\n    async def _make_server(self) -> Server[_Request]:\n        \"\"\"Return a new server for the runner to serve requests.\"\"\"\n\n    @abstractmethod\n    async def _cleanup_server(self) -> None:\n        \"\"\"Run any cleanup steps after the server is shutdown.\"\"\"\n\n    def _reg_site(self, site: BaseSite) -> None:\n        if site in self._sites:\n            raise RuntimeError(f\"Site {site} is already registered in runner {self}\")\n        self._sites.append(site)\n\n    def _check_site(self, site: BaseSite) -> None:\n        if site not in self._sites:\n            raise RuntimeError(f\"Site {site} is not registered in runner {self}\")\n\n    def _unreg_site(self, site: BaseSite) -> None:\n        if site not in self._sites:\n            raise RuntimeError(f\"Site {site} is not registered in runner {self}\")\n        self._sites.remove(site)\n\n\nclass ServerRunner(BaseRunner[BaseRequest]):\n    \"\"\"Low-level web server runner\"\"\"\n\n    __slots__ = (\"_web_server\",)\n\n    def __init__(\n        self,\n        web_server: Server[BaseRequest],\n        *,\n        handle_signals: bool = False,\n        **kwargs: Any,","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/d041d4d0fd48c3f0832084d33be16cf1c4835f85/aiohttp/web_runner.py#L375-L411","documentation":"Raised by BaseRunner._check_site (called from BaseSite.stop()) when the given site is not in the runner's `_sites` list. stop() needs the registration to exist so it can deregister and close the underlying server; an unregistered site means the stop call is out of order.","triggerScenarios":"Calling `await site.stop()` on a site that was never started (or already stopped). Also triggered by cleanup code that calls stop() in a finally block when start() was skipped due to an earlier exception.","commonSituations":"finally/cleanup handlers that unconditionally call stop() even when start() failed; shutdown sequences that stop sites twice; test teardown running against a site that the test never started.","solutions":["Only call stop() on sites that were successfully started; track a `started` flag per site.","In cleanup, guard with try/except RuntimeError or check membership first.","Use the runner's cleanup() which handles the registered set atomically.","Avoid double-stop by clearing the site reference after stop()."],"exampleFix":"// before\nsite = TCPSite(runner, host, port)\ntry:\n    await maybe_start(site)\nfinally:\n    await site.stop()  # if start() failed, stop() -> RuntimeError\n\n// after\nsite = TCPSite(runner, host, port)\nstarted = False\ntry:\n    await site.start()\n    started = True\nfinally:\n    if started:\n        await site.stop()","handlingStrategy":"validation","validationCode":"async def stop_if_started(site, started):\n    if site not in started:\n        return\n    await site.stop()\n    started.discard(site)","typeGuard":null,"tryCatchPattern":"try:\n    await site.stop()\nexcept RuntimeError:\n    pass  # site was never started or already stopped","preventionTips":["Only stop() sites that were successfully started; track a started flag.","Use runner.cleanup() for atomic teardown of all registered sites.","Guard finally-block stop() calls with a started check.","Clear site references after stop() to prevent double-stop."],"tags":["aiohttp","web-runner","site","lifecycle","cleanup"],"backgroundTag":null,"analyzedSha":"d041d4d0fd48c3f0832084d33be16cf1c4835f85","analyzedAt":"2026-08-11T20:44:15.550Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}