Implement cleanup guard to prevent multiple executions on exit
- Introduced a new cleanup function that ensures terminal and browser sessions are cleaned up only once during application exit. - Updated atexit registration to use the new cleanup function, enhancing resource management and preventing potential issues from multiple cleanup calls. - Modified terminal cleanup messaging to only display when environments are cleaned, improving user feedback.
This commit is contained in:
parent
45a8098d3a
commit
01a3a6ab0d
2 changed files with 23 additions and 15 deletions
35
cli.py
35
cli.py
|
|
@ -237,6 +237,24 @@ from cron import create_job, list_jobs, remove_job, get_job, run_daemon as run_c
|
||||||
from tools.terminal_tool import cleanup_all_environments as _cleanup_all_terminals
|
from tools.terminal_tool import cleanup_all_environments as _cleanup_all_terminals
|
||||||
from tools.browser_tool import _emergency_cleanup_all_sessions as _cleanup_all_browsers
|
from tools.browser_tool import _emergency_cleanup_all_sessions as _cleanup_all_browsers
|
||||||
|
|
||||||
|
# Guard to prevent cleanup from running multiple times on exit
|
||||||
|
_cleanup_done = False
|
||||||
|
|
||||||
|
def _run_cleanup():
|
||||||
|
"""Run resource cleanup exactly once."""
|
||||||
|
global _cleanup_done
|
||||||
|
if _cleanup_done:
|
||||||
|
return
|
||||||
|
_cleanup_done = True
|
||||||
|
try:
|
||||||
|
_cleanup_all_terminals()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
_cleanup_all_browsers()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# ASCII Art & Branding
|
# ASCII Art & Branding
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|
@ -1606,9 +1624,7 @@ class HermesCLI:
|
||||||
process_thread.start()
|
process_thread.start()
|
||||||
|
|
||||||
# Register atexit cleanup so resources are freed even on unexpected exit
|
# Register atexit cleanup so resources are freed even on unexpected exit
|
||||||
# (terminal VMs, browser sessions, etc.)
|
atexit.register(_run_cleanup)
|
||||||
atexit.register(_cleanup_all_browsers)
|
|
||||||
atexit.register(_cleanup_all_terminals)
|
|
||||||
|
|
||||||
# Run the application with patch_stdout for proper output handling
|
# Run the application with patch_stdout for proper output handling
|
||||||
try:
|
try:
|
||||||
|
|
@ -1618,15 +1634,7 @@ class HermesCLI:
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
self._should_exit = True
|
self._should_exit = True
|
||||||
# Explicitly clean up resources before exit
|
_run_cleanup()
|
||||||
try:
|
|
||||||
_cleanup_all_terminals()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
_cleanup_all_browsers()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
print("\nGoodbye! ⚕")
|
print("\nGoodbye! ⚕")
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1747,8 +1755,7 @@ def main(
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Register cleanup for single-query mode (interactive mode registers in run())
|
# Register cleanup for single-query mode (interactive mode registers in run())
|
||||||
atexit.register(_cleanup_all_browsers)
|
atexit.register(_run_cleanup)
|
||||||
atexit.register(_cleanup_all_terminals)
|
|
||||||
|
|
||||||
# Handle single query mode
|
# Handle single query mode
|
||||||
if query:
|
if query:
|
||||||
|
|
|
||||||
|
|
@ -1406,7 +1406,8 @@ def cleanup_all_environments():
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
print(f"[Terminal Cleanup] Cleaned {cleaned} environments")
|
if not os.getenv("HERMES_QUIET") and cleaned > 0:
|
||||||
|
print(f"[Terminal Cleanup] Cleaned {cleaned} environments")
|
||||||
return cleaned
|
return cleaned
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue