fix: make agent_thread daemon to prevent orphan CLI processes on tab close (#8557)

When a user closes a terminal tab, SIGHUP exits the main thread but
the non-daemon agent_thread kept the entire Python process alive —
stuck in the API call loop with no interrupt signal. Over many
conversations, these orphan processes accumulate and cause massive
swap usage (reported: 77GB on a 32GB M1 Pro).

Changes:
- Make agent_thread daemon=True so the process exits when the main
  thread finishes its cleanup. Under normal operation this changes
  nothing — the main thread already waits on agent_thread.is_alive().
- Interrupt the agent in the finally/exit path so the daemon thread
  stops making API calls promptly rather than being killed mid-flight.
This commit is contained in:
Teknium 2026-04-12 12:38:55 -07:00 committed by GitHub
parent 06290f6a2f
commit f295b17d92
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

15
cli.py
View file

@ -7617,8 +7617,10 @@ class HermesCLI:
"error": _summary,
}
# Start agent in background thread
agent_thread = threading.Thread(target=run_agent)
# Start agent in background thread (daemon so it cannot keep the
# process alive when the user closes the terminal tab — SIGHUP
# exits the main thread and daemon threads are reaped automatically).
agent_thread = threading.Thread(target=run_agent, daemon=True)
agent_thread.start()
# Monitor the dedicated interrupt queue while the agent runs.
@ -9626,6 +9628,15 @@ class HermesCLI:
raise
finally:
self._should_exit = True
# Interrupt the agent immediately so its daemon thread stops making
# API calls and exits promptly (agent_thread is daemon, so the
# process will exit once the main thread finishes, but interrupting
# avoids wasted API calls and lets run_conversation clean up).
if self.agent and getattr(self, '_agent_running', False):
try:
self.agent.interrupt()
except Exception:
pass
# Flush memories before exit (only for substantial conversations)
if self.agent and self.conversation_history:
try: