{"record":{"id":"6c34ab45b2b01e3c","repo":"reflex-dev/reflex","slug":"type-self-name-must-be-entered-before-calli","errorCode":null,"errorMessage":"{type(self).__name__} must be entered before calling this method.","messagePattern":"(.+?) must be entered before calling this method\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/context/base.py","lineNumber":81,"sourceCode":"            msg = \"Context is already attached, cannot enter context manager.\"\n            raise RuntimeError(msg)\n        self._attached_context_token[self] = self._context_var.set(self)\n        return self\n\n    def __exit__(self, *exc_info):\n        \"\"\"Exit the context.\"\"\"\n        if (token := self._attached_context_token.pop(self, None)) is not None:\n            self._context_var.reset(token)\n\n    def ensure_context_attached(self):\n        \"\"\"Ensure that the context is attached to the current context variable.\n\n        Raises:\n            RuntimeError: If the context is not attached.\n        \"\"\"\n        if self._attached_context_token.get(self) is None:\n            msg = f\"{type(self).__name__} must be entered before calling this method.\"\n            raise RuntimeError(msg)\n","sourceCodeStart":63,"sourceCodeEnd":82,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/context/base.py#L63-L82","documentation":"A Context method that requires the context to be active was called before the context was entered. The class checks that _attached_context_token contains a token for this instance; if absent, the contextvar is not set and context-dependent operations cannot work, so it raises RuntimeError.","triggerScenarios":"Creating a Context object and calling methods on it (anything that calls ensure_context_attached) without an active `with ctx:` / `async with ctx:` block, or calling after the with-block has exited.","commonSituations":"Storing a Context instance on a state/module and using it in event handlers outside the with-block, or accessing it in a background task after the block ended.","solutions":["Wrap usage in `with ctx:` or `async with ctx:` before calling context methods","Move the call inside the with-block scope where the context is attached","Restructure so the context object isn't accessed after __exit__"],"exampleFix":"# before\nctx = SomeContext()\nresult = ctx.get_value()  # RuntimeError: must be entered\n# after\nwith SomeContext() as ctx:\n    result = ctx.get_value()\n","handlingStrategy":"validation","validationCode":"assert ctx._attached_context_token.get(ctx) is not None, \"enter the context before use\"","typeGuard":"def is_context_attached(ctx) -> bool:\n    return ctx._attached_context_token.get(ctx) is not None","tryCatchPattern":"null","preventionTips":["Keep all context usage lexically inside the with-block","Pass the entered context object (not the outer one) to helpers"],"tags":["context-manager","runtime","state"],"backgroundTag":"context-not-entered","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}