aio-libs/aiohttp · error · RuntimeError

Expected one of the following apps {self._apps!r}, got {app!

Error message

Expected one of the following apps {self._apps!r}, got {app!r}

What it means

Raised as RuntimeError by the current_app setter on UrlMappingMatchInfo, but only when aiohttp's DEBUG flag is on. It checks that the app being set as current is one of the apps already in the match-info stack. In production (DEBUG off) the check is skipped, so this is a development-time invariant guard for the middleware/sub-app dispatch machinery.

Source

Thrown at aiohttp/web_urldispatcher.py:264

    def add_app(self, app: "Application") -> None:
        if self._frozen:
            raise RuntimeError("Cannot change apps stack after .freeze() call")
        if self._current_app is None:
            self._current_app = app
        self._apps.insert(0, app)

    @property
    def current_app(self) -> "Application":
        app = self._current_app
        assert app is not None
        return app

    @current_app.setter
    def current_app(self, app: "Application") -> None:
        if DEBUG:
            if app not in self._apps:
                raise RuntimeError(
                    f"Expected one of the following apps {self._apps!r}, got {app!r}"
                )
        self._current_app = app

    def freeze(self) -> None:
        self._frozen = True

    def __repr__(self) -> str:
        return f"<MatchInfo {super().__repr__()}: {self._route}>"


class MatchInfoError(UrlMappingMatchInfo):

    __slots__ = ("_exception",)

    def __init__(self, http_exception: HTTPException) -> None:
        self._exception = http_exception
        super().__init__({}, SystemRoute(self._exception))

View on GitHub (pinned to c0ef574e29)

Solutions

  1. Treat this as a bug report candidate — check third-party middleware that touches match_info.current_app.
  2. Update aiohttp and middleware libraries to matching compatible versions.
  3. If reproducing in custom middleware, ensure you only set current_app to a value from match_info.apps.
  4. File an issue with a minimal reproducer if it occurs with stock aiohttp.
Defensive patterns

Strategy: try-catch

Try / catch

# Internal/debug guard — wrap suspect middleware dispatch in debug builds
try:
    await handler(request)
except RuntimeError as e:
    if 'Expected one of the following apps' in str(e):
        log.error("middleware set current_app to unknown app")
    raise

Prevention

When it happens

Trigger: Triggered only with AIOHTTP_DEBUG/DEBUG enabled, when internal middleware dispatch (set_current_app context) tries to set current_app to an Application not in the resolved app stack. This is almost always an aiohttp-internal or middleware bug, not user-facing API misuse.

Common situations: Running with debug mode on and a buggy middleware that manipulates request.match_info.current_app; third-party middleware that incorrectly swaps current_app; version mismatch between aiohttp core and a middleware library.

Related errors


AI-assisted analysis of aio-libs/aiohttp@c0ef574e29 (2026-08-04). Data as JSON: /data/errors/831e9463fb113e44.json. Report an issue: GitHub.