Textualize/textual · error · ActiveModeError

Can't remove active mode {mode!r}

Error message

Can't remove active mode {mode!r}

What it means

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.

Source

Thrown at src/textual/app.py:2712

                "add_mode() must be called with a Screen type, not an instance"
                f" (got instance of {type(base_screen).__name__})"
            )
        self._modes[mode] = base_screen

    def remove_mode(self, mode: str) -> AwaitComplete:
        """Removes a mode from the app.

        Screens that are running in the stack of that mode are scheduled for pruning.

        Args:
            mode: The mode to remove. It can't be the active mode.

        Raises:
            ActiveModeError: If trying to remove the active mode.
            UnknownModeError: If trying to remove an unknown mode.
        """
        if mode == self._current_mode:
            raise ActiveModeError(f"Can't remove active mode {mode!r}")
        elif mode not in self._modes:
            raise UnknownModeError(f"Unknown mode {mode!r}")
        else:
            del self._modes[mode]

        if mode not in self._screen_stacks:
            return AwaitComplete.nothing()

        stack = self._screen_stacks[mode]
        del self._screen_stacks[mode]

        async def remove_screens() -> None:
            """Remove screens."""
            for screen in reversed(stack):
                await self._replace_screen(screen)

        return AwaitComplete(remove_screens()).call_next(self)

View on GitHub (pinned to 06dbeef4bb)

Solutions

  1. Switch away first: await app.switch_mode("main") then await app.remove_mode("editor")
  2. When iterating, skip the current mode

Example fix

# before
for mode in list(app._modes):
    await app.remove_mode(mode)  # hits active mode

# after
for mode in list(app._modes):
    if mode != app._current_mode:
        await app.remove_mode(mode)
Defensive patterns

Strategy: validation

Validate before calling

if mode != app._current_mode:
    await app.remove_mode(mode)

Try / catch

try:
    await app.remove_mode(mode)
except ActiveModeError:
    await app.switch_mode('main')
    await app.remove_mode(mode)

Prevention

When it happens

Trigger: await app.remove_mode(app._current_mode), i.e. removing the mode you are currently in.

Common situations: Cleanup routines that iterate all modes; removing a mode from within one of its own screens.

Related errors


AI-assisted analysis of Textualize/textual@06dbeef4bb (2026-08-27). Data as JSON: /api/errors/9ced5385cc9cb8b5. Report an issue: GitHub.