{"record":{"id":"9ced5385cc9cb8b5","repo":"Textualize/textual","slug":"can-t-remove-active-mode-mode-r","errorCode":null,"errorMessage":"Can't remove active mode {mode!r}","messagePattern":"Can't remove active mode (.+?)","errorType":"exception","errorClass":"ActiveModeError","httpStatus":null,"severity":"error","filePath":"src/textual/app.py","lineNumber":2712,"sourceCode":"                \"add_mode() must be called with a Screen type, not an instance\"\n                f\" (got instance of {type(base_screen).__name__})\"\n            )\n        self._modes[mode] = base_screen\n\n    def remove_mode(self, mode: str) -> AwaitComplete:\n        \"\"\"Removes a mode from the app.\n\n        Screens that are running in the stack of that mode are scheduled for pruning.\n\n        Args:\n            mode: The mode to remove. It can't be the active mode.\n\n        Raises:\n            ActiveModeError: If trying to remove the active mode.\n            UnknownModeError: If trying to remove an unknown mode.\n        \"\"\"\n        if mode == self._current_mode:\n            raise ActiveModeError(f\"Can't remove active mode {mode!r}\")\n        elif mode not in self._modes:\n            raise UnknownModeError(f\"Unknown mode {mode!r}\")\n        else:\n            del self._modes[mode]\n\n        if mode not in self._screen_stacks:\n            return AwaitComplete.nothing()\n\n        stack = self._screen_stacks[mode]\n        del self._screen_stacks[mode]\n\n        async def remove_screens() -> None:\n            \"\"\"Remove screens.\"\"\"\n            for screen in reversed(stack):\n                await self._replace_screen(screen)\n\n        return AwaitComplete(remove_screens()).call_next(self)\n","sourceCodeStart":2694,"sourceCodeEnd":2730,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/app.py#L2694-L2730","documentation":"ActiveModeError raised by App.remove_mode when trying to remove the currently active mode. The active mode owns the live screen stack and cannot be deleted out from under the app.","triggerScenarios":"await app.remove_mode(app._current_mode), i.e. removing the mode you are currently in.","commonSituations":"Cleanup routines that iterate all modes; removing a mode from within one of its own screens.","solutions":["Switch away first: await app.switch_mode(\"main\") then await app.remove_mode(\"editor\")","When iterating, skip the current mode"],"exampleFix":"# before\nfor mode in list(app._modes):\n    await app.remove_mode(mode)  # hits active mode\n\n# after\nfor mode in list(app._modes):\n    if mode != app._current_mode:\n        await app.remove_mode(mode)","handlingStrategy":"validation","validationCode":"if mode != app._current_mode:\n    await app.remove_mode(mode)","typeGuard":null,"tryCatchPattern":"try:\n    await app.remove_mode(mode)\nexcept ActiveModeError:\n    await app.switch_mode('main')\n    await app.remove_mode(mode)","preventionTips":["Switch away before removing the current mode","Skip _current_mode in bulk cleanup loops"],"tags":["modes","lifecycle","textual"],"backgroundTag":"remove-active-resource","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}