{"id":"489014cb915a8f4c","repo":"aio-libs/aiohttp","slug":"added-route-will-never-be-executed-method-route","errorCode":null,"errorMessage":"Added route will never be executed, method {route.method} is already registered","messagePattern":"Added route will never be executed, method (.+?) is already registered","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_urldispatcher.py","lineNumber":323,"sourceCode":"            raise HTTPExpectationFailed(text=\"Unknown Expect: %s\" % expect)\n\n\nclass Resource(AbstractResource):\n    def __init__(self, *, name: str | None = None) -> None:\n        super().__init__(name=name)\n        self._routes: dict[str, ResourceRoute] = {}\n        self._any_route: ResourceRoute | None = None\n        self._allowed_methods: set[str] = set()\n\n    def add_route(\n        self,\n        method: str,\n        handler: type[AbstractView] | Handler,\n        *,\n        expect_handler: _ExpectHandler | None = None,\n    ) -> \"ResourceRoute\":\n        if route := self._routes.get(method, self._any_route):\n            raise RuntimeError(\n                \"Added route will never be executed, \"\n                f\"method {route.method} is already \"\n                \"registered\"\n            )\n\n        route_obj = ResourceRoute(method, handler, self, expect_handler=expect_handler)\n        self.register_route(route_obj)\n        return route_obj\n\n    def register_route(self, route: \"ResourceRoute\") -> None:\n        assert isinstance(\n            route, ResourceRoute\n        ), f\"Instance of Route class is required, got {route!r}\"\n        if route.method == hdrs.METH_ANY:\n            self._any_route = route\n        self._allowed_methods.add(route.method)\n        self._routes[route.method] = route\n","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_urldispatcher.py#L305-L341","documentation":"Raised as RuntimeError by Resource.add_route when a route for the same HTTP method (or ANY) is already registered on that resource. Because route resolution returns the first registered match, a duplicate would be silently unreachable, so aiohttp rejects it to surface the likely configuration mistake.","triggerScenarios":"Calling resource.add_route('GET', handler1) then resource.add_route('GET', handler2) on the same Resource. Also when add_get is called twice on the same path/resource, or mixing add_route('GET') with an ANY route already present (the ANY route shadows it).","commonSituations":"Two blueprint/blueprint registrations applying to the same prefix; importing a routes module twice; copy-paste creating duplicate add_get calls; registering both a method-specific route and a '*' (ANY) route on the same resource where ANY was registered first.","solutions":["Remove the duplicate registration — keep a single handler per method per resource.","If you need different handlers, give them distinct paths or distinct methods.","Guard against double-import by registering routes in a function called once, or use an idempotent setup.","If you intended to override, first remove/replace the existing route rather than adding alongside."],"exampleFix":"# before\nresource.add_route('GET', h1)\nresource.add_route('GET', h2)  # raises\n\n# after\nresource.add_route('GET', h1)\nresource.add_route('POST', h2)","handlingStrategy":"validation","validationCode":"def register_unique(resource, method, handler):\n    if method in resource._routes or (resource._any_route and method == '*'):\n        raise RuntimeError(f\"method {method} already registered on {resource}\")\n    return resource.add_route(method, handler)","typeGuard":null,"tryCatchPattern":"try:\n    resource.add_route(method, handler)\nexcept RuntimeError as e:\n    if 'already registered' in str(e):\n        log.warning(\"skipping duplicate route for %s\", method)\n    else:\n        raise","preventionTips":["Register each method/path pair exactly once.","Make route registration idempotent by keying on (path, method).","Audit blueprints/plugins for overlapping route definitions."],"tags":["aiohttp","routing","duplicate","registration","runtime-error"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}