DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix: close CLI runtime before event loop shutdown
Bingxi Zhao (Frank)efc6604c44df
diff --git a/deeptutor/app/container.py b/deeptutor/app/container.pyindex 36aa50cdd..76c8c6afe 100644--- a/deeptutor/app/container.py+++ b/deeptutor/app/container.py@@ -407,10 +407,24 @@ def set_application_container(container: ApplicationContainer | None) -> None:_default_container = container+async def close_application_container() -> None:+ """Close an existing default runtime without constructing one for cleanup."""+ global _default_container+ container = _default_container+ if container is None:+ return+ try:+ await container.close()+ finally:+ if _default_container is container:+ _default_container = None++__all__ = ["ApplicationContainer","RuntimeRegistry","StoreProvider","get_application_container","set_application_container",+ "close_application_container",]diff --git a/deeptutor_cli/common.py b/deeptutor_cli/common.pyindex 00fb5e579..cf24f100b 100644--- a/deeptutor_cli/common.py+++ b/deeptutor_cli/common.py@@ -855,8 +855,17 @@ def build_turn_request(def maybe_run(coro): # noqa: ANN001+ async def run_and_close():+ from deeptutor.app.container import close_application_container++ try:+ return await coro+ finally:+ # Release turns, subprocesses and module leases on the owning loop.+ await close_application_container()+try:- return asyncio.run(coro)+ return asyncio.run(run_and_close())except KeyboardInterrupt:console.print("\n[dim]Interrupted.[/]")return Nonediff --git a/tests/cli/test_runtime_cleanup.py b/tests/cli/test_runtime_cleanup.pynew file mode 100644index 000000000..63795990d--- /dev/null+++ b/tests/cli/test_runtime_cleanup.py@@ -0,0 +1,58 @@+"""CLI completion releases the runtime before closing its event loop."""++import asyncio++import pytest+from typer.testing import CliRunner++from deeptutor.app import container as containers+from deeptutor.modules.lifecycle import environment_lease+from deeptutor_cli.common import maybe_run+from deeptutor_cli.main import app++[email protected]("outcome", ["success", "error", "cancel"])+def test_cli_releases_runtime_lease_on_every_exit(outcome):+ container = containers.get_application_container()++ async def command():+ await container.start()+ if outcome == "error":+ raise ValueError("command failed")+ if outcome == "cancel":+ raise asyncio.CancelledError+ return "done"++ if outcome == "success":+ assert maybe_run(command()) == "done"+ else:+ error = ValueError if outcome == "error" else asyncio.CancelledError+ with pytest.raises(error):+ maybe_run(command())+ assert not container._started+ assert container._module_lease is None+ assert containers._default_container is None+ with environment_lease(exclusive=True):+ pass+++def test_invalid_cli_capability_does_not_block_module_changes():+ result = CliRunner().invoke(app, ["run", "auto", "hello"])+ assert isinstance(result.exception, ValueError)+ # Retain the traceback (and its container references): no GC is needed.+ assert "Unknown capability" in str(result.exception)+ with environment_lease(exclusive=True):+ pass+++def test_cleanup_does_not_construct_an_unused_container(monkeypatch):+ monkeypatch.setattr(+ containers.ApplicationContainer,+ "build",+ lambda: pytest.fail("Cleanup constructed a runtime"),+ )++ async def command():+ return 42++ assert maybe_run(command()) == 42