{"record":{"id":"8a2609959f3a8ece","repo":"pallets/flask","slug":"cannot-pop-this-context-self-r-there-is-no-ac","errorCode":null,"errorMessage":"Cannot pop this context ({self!r}), there is no active context.","messagePattern":"Cannot pop this context \\((.+?)\\), there is no active context\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/flask/ctx.py","lineNumber":471,"sourceCode":"        This context must currently be the active context, otherwise a\n        :exc:`RuntimeError` is raised. In some situations, such as streaming or\n        testing, the context may have been pushed multiple times. It will only\n        trigger cleanup once it has been popped as many times as it was pushed.\n        Until then, it will remain the active context.\n\n        :param exc: An unhandled exception that was raised while the context was\n            active. Passed to teardown functions.\n\n        .. versionchanged:: 0.9\n            Added the ``exc`` argument.\n        \"\"\"\n        if self._cv_token is None:\n            raise RuntimeError(f\"Cannot pop this context ({self!r}), it is not pushed.\")\n\n        ctx = _cv_app.get(None)\n\n        if ctx is None or self._cv_token is None:\n            raise RuntimeError(\n                f\"Cannot pop this context ({self!r}), there is no active context.\"\n            )\n\n        if ctx is not self:\n            raise RuntimeError(\n                f\"Cannot pop this context ({self!r}), it is not the active\"\n                f\" context ({ctx!r}).\"\n            )\n\n        self._push_count -= 1\n\n        if self._push_count > 0:\n            return\n\n        collect_errors = _CollectErrors()\n\n        if self._request is not None:\n            with collect_errors:","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/ctx.py#L453-L489","documentation":"In pop(), after confirming self._cv_token is not None, Flask reads _cv_app.get(None). If that returns None (the context-local variable was reset/cleared) or the token is somehow None, it raises RuntimeError stating there is no active context. This indicates the context stack was emptied externally while a pop was attempted.","triggerScenarios":"The context var holding the active context was cleared (e.g. by another pop, garbage collection of the token, or running in a thread/async boundary where the context var does not propagate) at the moment pop() reads it. Distinct from error 30 (never pushed) and 32 (wrong context active).","commonSituations":"Mixing manual context management across threads or greenlets where the context var is thread/async-local. A double-managed context where a prior pop cleared the var. Test harnesses that reset context state between operations.","solutions":["Use the 'with ctx:' form so Flask manages the context var lifecycle and you never pop a context whose var was cleared.","Avoid sharing context objects across threads/greenlets; each worker should push its own context.","Ensure no code path clears the context var (via token reset) before your pop() runs."],"exampleFix":"# before\nctx.push()\n# ... something resets _cv_app (e.g. another pop or thread boundary)\nctx.pop()  # RuntimeError: no active context\n\n# after: keep push/pop on the same thread/flow\nctx.push()\ntry:\n    do_work()  # no nested pop here\nfinally:\n    ctx.pop()","handlingStrategy":"try-catch","validationCode":"from flask.globals import _cv_app\n\ndef has_active_context() -> bool:\n    return _cv_app.get(None) is not None","typeGuard":"null","tryCatchPattern":"try:\n    ctx.pop()\nexcept RuntimeError as e:\n    if 'no active context' in str(e):\n        # context var was cleared externally; reset local state\n        ctx._cv_token = None\n    else:\n        raise","preventionTips":["Do not share context objects across threads/greenlets.","Let Flask manage the context var via context-manager usage."],"tags":["flask","context","concurrency","lifecycle"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}