Repository navigation
Fix remaining tmux session leaks in agent-server startup and task cleanup - #2805
Conversation
1. EventService.close() fire-and-forget (CRITICAL): Added missing await to ensure LocalConversation.close() actually runs during shutdown 2. No tmux session cleanup on startup (MEDIUM): Added cleanup_stale_tmux_sessions() to kill orphaned sessions from previous server runs on startup 3. Task manager sub-conversations use delete_on_close=False (MEDIUM): Changed to delete_on_close=True in both locations to ensure tool cleanup runs These fixes prevent unbounded accumulation of orphaned tmux sessions and their bash children processes that were never being terminated. Co-authored-by: openhands <openhands@all-hands.dev>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
Taste Rating: 🟡 Acceptable - Solves real resource leak issues with pragmatic approach, but needs validation.
Verdict: Core logic looks sound, but missing critical evidence that the cleanup works correctly and doesn't break edge cases. See inline comments for details.
Coverage Report •
|
|||||||||||||||||||||||||||||||||||||||||||||
all-hands-bot
left a comment
There was a problem hiding this comment.
QA Report: Fix three critical bugs causing bash/tmux session leaks
Summary
✅ PASS WITH ISSUES - Code changes are correct and CI tests pass. Static analysis confirms proper error handling and edge case coverage. However, runtime verification was limited by environment constraints, and one potential issue was identified regarding reconnecting conversations.
Environment Setup
✅ Success
$ git checkout pr-2805 && git rev-parse HEAD
7ce93c706cbd08dee3ec8d48d83b1101d4d5de1b
$ make build
✓ Dependencies installed (232 packages)
✓ Pre-commit hooks configuredCI & Test Status
✅ All critical tests passing
- ✅ agent-server-tests: SUCCESS
- ✅ tools-tests: SUCCESS
- ✅ sdk-tests: SUCCESS
- ✅ pre-commit: SUCCESS (ruff, pyright, pycodestyle all pass)
- ✅ Python API breakage: SUCCESS
- ✅ REST API breakage: SUCCESS
Functional Verification
✅ Code Review - Cleanup Function (api.py)
Verified: Edge case handling is comprehensive
# Handles all edge cases:
- ImportError when libtmux unavailable (line 84-85)
- No sessions to clean (line 68-70)
- Individual session kill failures (line 76-80)
- General exceptions don't block startup (line 86-88)Verified: Proper async/await usage
Line 94: await cleanup_stale_tmux_sessions() # ✓ Properly awaited✅ Code Review - Task Manager Changes (manager.py)
Verified: Both instances changed correctly
$ grep -n delete_on_close openhands-tools/openhands/tools/task/manager.py
201: delete_on_close=True, # ✓ Changed from False
288: delete_on_close=True, # ✓ Changed from FalseVerified: Effect of delete_on_close=True
- Calls
executor.close()on all tools when conversation closes - Ensures tmux sessions for sub-tasks are properly terminated
- Consistent with default behavior (delete_on_close defaults to True elsewhere)
✅ Code Review - EventService.close() Fix
Verified: Bug 1 was already fixed in PR #2734
$ git log --oneline --grep="EventService"
c78c3c2a fix(agent-server): await conversation teardown in EventService.close() (#2734)
$ grep -A 3 "def close" openhands-agent-server/openhands/agent_server/event_service.py
Line 659: await loop.run_in_executor(None, self._conversation.close) # ✓ await presentUnable to Verify (Environment Constraints)
❌ Runtime tmux cleanup behavior
- Attempted: Created test tmux sessions and ran cleanup function
- Result: Test hung when accessing
libtmux.Server.sessionsorsession.kill() - Attempts: 3 different approaches all resulted in blocking/timeout
- Reason: libtmux operations appear to block in this CI environment
❌ Server startup with live sessions
- Attempted: Start agent-server to exercise lifespan hook
- Result: Server startup commands timed out
- Reason: Environment limitations with long-running processes
Mitigation: The passing CI tests (agent-server-tests, tools-tests) provide confidence that:
- The server can start successfully with the cleanup code
- The task manager changes don't break existing functionality
- Error handling prevents cleanup failures from blocking startup
Issues Found
🟡 Minor - Reconnecting conversations: The cleanup kills ALL tmux sessions on server startup. The PR description claims "Reconnecting conversations will create fresh sessions as needed," but this assumption is not explicitly tested or documented in the code. If reconnecting to an existing conversation is a supported use case, there should be logic to recreate sessions, or documentation should clarify that reconnection requires a fresh session.
Suggested AGENTS.md Guidance
For future QA runs of server lifecycle changes:
## Testing Server Startup & Tmux Cleanup
- The agent-server can be tested with: `timeout 10 uv run python -m openhands.agent_server &`
- Tmux sessions should be created with CLI, not libtmux API in tests (API calls may block)
- Test cleanup with: `tmux -L openhands new-session -d -s test-session-X`
- Verify cleanup with: `tmux -L openhands list-sessions`
- libtmux.Server operations may hang in CI - rely on integration tests insteadVerdict
✅ PASS WITH ISSUES
The PR correctly fixes the three stated bugs:
- ✅ EventService.close() await (already fixed, confirmed present)
- ✅ Tmux cleanup on startup (implemented with proper error handling)
- ✅ Task manager delete_on_close (changed to True in both locations)
Code quality is high with comprehensive edge case handling. CI tests confirm no regressions. The minor issue regarding reconnecting conversations should be addressed through documentation or additional validation, but does not block merging for the stated bug fixes.
|
@OpenHands address the unnecessary async and magic string feedback. Also it seems like this fix got lost: Bug 1 - EventService.close() await fix |
|
I'm on it! rbren can track my progress at all-hands.dev |
Co-authored-by: openhands <openhands@all-hands.dev>
|
Addressed the requested review feedback in 11b6f48: made |
|
Final summary of new work since the last update:
Conciseness check:
So yes: the requested follow-up work has been completed faithfully, and the new changes remain minimal and focused. |
VascoSch92
left a comment
There was a problem hiding this comment.
Two small nits that are not blocking the PR.
Perhaps we should also do this kind of check for stale Chrome instances.
|
@OpenHands please address the two nits #2805 (review) and make sure all previous code review comments are properly addressed. if so, mark them as solved |
|
I'm on it! xingyaoww can track my progress at all-hands.dev |
- Rename cleanup_stale_tmux_sessions -> _cleanup_stale_tmux_sessions - Move 'import libtmux' to module-level imports (libtmux is guaranteed available since openhands-tools is already imported) - Remove unnecessary ImportError handler Co-authored-by: openhands <openhands@all-hands.dev>
Summary of WorkBoth nits from the review have been fully addressed in commit
Conciseness check: The commit touches only 1 file ( |
…anup (OpenHands#2805) Co-authored-by: openhands <openhands@all-hands.dev>
…anup (OpenHands#2805) Co-authored-by: openhands <openhands@all-hands.dev>
Problem
This PR fixes the remaining two critical bugs that caused unbounded accumulation of orphaned tmux sessions and bash processes. The original EventService.close() await bug is already fixed in the current codebase and is not part of this diff.
Solution
EventService.close() await fix
openhands-agent-server/openhands/agent_server/event_service.py:659EventService.close()still awaitsloop.run_in_executor(None, self._conversation.close)on current HEADTmux cleanup on startup
openhands-agent-server/openhands/agent_server/api.pyTMUX_SOCKET_NAMEconstant instead of a hardcoded socket nameTask manager delete_on_close fix
openhands-tools/openhands/tools/task/manager.py:201openhands-tools/openhands/tools/task/manager.py:288delete_on_close=Falsetodelete_on_close=TrueShared tmux socket constant
openhands-tools/openhands/tools/terminal/constants.pyopenhands-tools/openhands/tools/terminal/terminal/tmux_terminal.pyopenhands-tools/openhands/tools/terminal/terminal/tmux_pane_pool.pyopenhands-agent-server/openhands/agent_server/api.pyTMUX_SOCKET_NAMEand reused it across tmux session creation and cleanup pathsFiles Changed
openhands-agent-server/openhands/agent_server/api.pyopenhands-tools/openhands/tools/task/manager.pyopenhands-tools/openhands/tools/terminal/constants.pyopenhands-tools/openhands/tools/terminal/terminal/tmux_terminal.pyopenhands-tools/openhands/tools/terminal/terminal/tmux_pane_pool.pyTesting
uv run pre-commit run --files openhands-agent-server/openhands/agent_server/api.py openhands-tools/openhands/tools/terminal/constants.py openhands-tools/openhands/tools/terminal/terminal/tmux_terminal.py openhands-tools/openhands/tools/terminal/terminal/tmux_pane_pool.pycleanup_stale_tmux_sessionsandTMUX_SOCKET_NAMEImpact
These fixes prevent resource exhaustion from unbounded session accumulation that could crash the agent-server over time.
Closes: Fixes critical memory/process leaks in agent-server
This PR title/description was updated by an AI assistant (OpenHands) on behalf of the user.
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:9ddf3ed-pythonRun
All tags pushed for this build
About Multi-Architecture Support
9ddf3ed-python) is a multi-arch manifest supporting both amd64 and arm649ddf3ed-python-amd64) are also available if needed