{"record":{"id":"0918783234736171","repo":"pallets/flask","slug":"cannot-pop-this-context-self-r-it-is-not-push","errorCode":null,"errorMessage":"Cannot pop this context ({self!r}), it is not pushed.","messagePattern":"Cannot pop this context \\((.+?)\\), it is not pushed\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/flask/ctx.py","lineNumber":466,"sourceCode":"        call teardown functions and signals.\n\n        Typically, this is not used directly. Instead, use a ``with`` block\n        to manage the context.\n\n        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","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/ctx.py#L448-L484","documentation":"RequestContext/AppContext.pop() checks self._cv_token; if it is None the context was never pushed (the token is set during push()). Calling pop() on an un-pushed context raises RuntimeError naming the context. This catches unbalanced pop() calls and context-manager misuse.","triggerScenarios":"Manually calling ctx.pop() before ctx.push(), or calling pop() twice after only one push (the second pop sees _cv_token cleared on final pop). Using the context object outside its 'with' block after it has already been popped.","commonSituations":"Manual context push/pop management with a typo in ordering. Teardown logic that pops unconditionally. A context already exited via 'with' then popped again in a finally clause.","solutions":["Use the context as a context manager (with ctx:) so push/pop are balanced automatically.","If pushing manually, ensure every push() has exactly one matching pop() and pop() is never called first.","Track push state yourself or check ctx._cv_token is not None before calling pop()."],"exampleFix":"# before\nctx = app.test_request_context()\nctx.pop()  # never pushed -> RuntimeError\n\n# after\nctx = app.test_request_context()\nctx.push()\ntry:\n    ...\nfinally:\n    ctx.pop()\n# or simply: with app.test_request_context(): ...","handlingStrategy":"validation","validationCode":"def can_pop(ctx) -> bool:\n    return getattr(ctx, '_cv_token', None) is not None","typeGuard":"null","tryCatchPattern":"try:\n    ctx.pop()\nexcept RuntimeError as e:\n    if 'not pushed' in str(e):\n        pass  # already popped / never pushed; ignore\n    else:\n        raise","preventionTips":["Prefer 'with ctx:' over manual push/pop to guarantee balance.","Never call pop() before push(); track state explicitly if managing manually."],"tags":["flask","context","lifecycle","resource-management"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}