mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-04 08:28:36 +00:00
fix(exec): retain stale sessions after cleanup failure
Only remove idle exec sessions after process termination succeeds so later cleanup and shutdown paths can retry failed kills.
This commit is contained in:
parent
274613f064
commit
648fc92673
@ -370,8 +370,9 @@ class ExecSessionManager:
|
|||||||
if now - session.last_access > self.idle_timeout
|
if now - session.last_access > self.idle_timeout
|
||||||
]
|
]
|
||||||
for session_id in stale:
|
for session_id in stale:
|
||||||
session = self._sessions.pop(session_id)
|
session = self._sessions[session_id]
|
||||||
await session.kill()
|
await session.kill()
|
||||||
|
self._sessions.pop(session_id, None)
|
||||||
|
|
||||||
async def _spawn(
|
async def _spawn(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@ -702,6 +702,30 @@ def test_terminate_by_owner_retains_failed_sessions():
|
|||||||
asyncio.run(run())
|
asyncio.run(run())
|
||||||
|
|
||||||
|
|
||||||
|
def test_stale_cleanup_retains_session_when_kill_fails():
|
||||||
|
async def run() -> None:
|
||||||
|
manager = ExecSessionManager(idle_timeout=1)
|
||||||
|
session = SimpleNamespace(
|
||||||
|
session_id="stale-failed",
|
||||||
|
owner_session_key="cli:a",
|
||||||
|
last_access=time.monotonic() - 10,
|
||||||
|
kill=AsyncMock(side_effect=OSError("termination failed")),
|
||||||
|
)
|
||||||
|
manager._sessions[session.session_id] = session
|
||||||
|
|
||||||
|
with pytest.raises(OSError, match="termination failed"):
|
||||||
|
await manager.list(owner_session_key="cli:a")
|
||||||
|
|
||||||
|
assert manager._sessions == {session.session_id: session}
|
||||||
|
session.kill.assert_awaited_once()
|
||||||
|
|
||||||
|
session.kill.side_effect = None
|
||||||
|
assert await manager.list(owner_session_key="cli:a") == []
|
||||||
|
assert manager._sessions == {}
|
||||||
|
|
||||||
|
asyncio.run(run())
|
||||||
|
|
||||||
|
|
||||||
def test_terminate_by_owner_skips_sessions_without_owner_key(tmp_path):
|
def test_terminate_by_owner_skips_sessions_without_owner_key(tmp_path):
|
||||||
async def run() -> None:
|
async def run() -> None:
|
||||||
manager = ExecSessionManager()
|
manager = ExecSessionManager()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user