From 6da970f15d78d81dfc6287e54788acc2f869b64c Mon Sep 17 00:00:00 2001 From: analista Date: Sun, 26 Apr 2026 00:13:21 +0900 Subject: [PATCH] fix(tui): close AIAgent on session teardown to prevent FD leak session.close only closed the slash_worker subprocess but never called agent.close() on the AIAgent instance. In the long-lived TUI gateway process, this left httpx clients for GC to finalize. When the OS recycled a closed FD number for a new active connection, the stale finalizer would close the live socket, causing intermittent [Errno 9] Bad file descriptor on subsequent LLM API calls. Call agent.close() (which properly shuts down the httpx transport pool and TCP sockets) before closing the slash_worker. --- tui_gateway/server.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tui_gateway/server.py b/tui_gateway/server.py index fe66d3798..825822aad 100644 --- a/tui_gateway/server.py +++ b/tui_gateway/server.py @@ -2439,6 +2439,12 @@ def _(rid, params: dict) -> dict: unregister_gateway_notify(session["session_key"]) except Exception: pass + try: + agent = session.get("agent") + if agent and hasattr(agent, "close"): + agent.close() + except Exception: + pass try: worker = session.get("slash_worker") if worker: