Skip to content

fix(agent-server): remove the per-server TMUX_TMPDIR on shutdown - #5945

Merged
enyst merged 2 commits into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup
Oct 14, 2026
Merged

enyst merged 2 commits into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup

Conversation

@enyst

@enyst enyst commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

HUMAN:
This PR proposes a fix for a bug uncovered during the first full agentic run of verify-agent-server.


AGENT:

I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

Requested by Engel · project thread

Decision needed

The issue triage suggests gating removal on the env-restore guard (os.environ.get("TMUX_TMPDIR") == str(tmux_tmpdir)). This PR gates it instead on whether this server created the directory during this start (_ensure_server_tmux_tmpdir now returns a created flag).

  • Why: the env guard tracks who owns the variable, not who owns the directory. A pre-existing openhands-agent-server-<pid> directory (for example, /tmp shared across PID namespaces) would be deleted under the env guard alone. Nothing in the server changes TMUX_TMPDIR after startup.
  • The alternative: also require the env guard, so removal needs both conditions. That would leave the directory behind if the variable had been changed at runtime.

A maintainer should confirm which gate they prefer.

Why

When TMUX_TMPDIR is unset (or too long for a tmux socket path), the agent server creates <tempdir>/openhands-agent-server-<pid> for itself. On a graceful shutdown, api_lifespan only restored the environment variable and never deleted the directory, so every such start left one more directory in the temp dir (this sandbox had 241 of them).

Summary

  • At shutdown, api_lifespan removes the per-server tmux directory, but only when this process created it. Removal is best-effort: a failure is logged as a warning and shutdown continues. A user-provided TMUX_TMPDIR, or a default directory that already existed, is never removed. Env restoration is unchanged.
  • Tests: the lifespan test asserts the directory is gone after exit. New tests cover these cases:
    • a user-provided TMUX_TMPDIR is kept;
    • a pre-existing default directory is kept;
    • with a too-long TMUX_TMPDIR, the user's value is restored and kept and the substitute is removed;
    • a failed removal is logged and does not abort shutdown.
  • Map: drops the # bug marker and the "known bug" label from F31.tmux-dir-cleanup and rewrites its prose as the expected behaviour.

Issue Number

Fixes #5932

How to Test

  • uv run pytest -q tests/agent_server/test_api.py: 51 passed. The updated lifespan test fails on main (the directory still exists).
  • Map proof with AGENT_SERVER_VERIFY_HOME=/var/tmp/avs-tmux-dir-cleanup:
    • baseline (main at 6e7a853): control-agent-server map run --file F31 --only F31.tmux-dir-cleanup --fresh --repeat 2 --checkout <main checkout> gives xfail, xfail;
    • this branch, with the marker still in place: the same command without --checkout gives xpass, xpass;
    • with the marker removed: control-agent-server map check is ok, and map run --file F31 --only F31.tmux-dir-cleanup --fresh gives pass. It passed again after the second commit.
  • uv run pre-commit run --files <changed files>: all checks pass.

Video/Screenshots

n/a

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

A same-named directory that already existed at startup is left alone, so it is left behind as before. See "Decision needed".

🤖 Generated with Claude Code

https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:4cecf3a-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-4cecf3a-python \
  ghcr.io/openhands/agent-server:4cecf3a-python

All tags pushed for this build

ghcr.io/openhands/agent-server:4cecf3a-golang-amd64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-golang-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-golang-amd64
ghcr.io/openhands/agent-server:4cecf3a-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:4cecf3a-golang-arm64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-golang-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-golang-arm64
ghcr.io/openhands/agent-server:4cecf3a-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:4cecf3a-java-amd64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-java-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-java-amd64
ghcr.io/openhands/agent-server:4cecf3a-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:4cecf3a-java-arm64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-java-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-java-arm64
ghcr.io/openhands/agent-server:4cecf3a-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:4cecf3a-python-amd64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-amd64
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-amd64
ghcr.io/openhands/agent-server:4cecf3a-python-arm64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-arm64
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-arm64
ghcr.io/openhands/agent-server:4cecf3a-python-minimal-amd64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-minimal-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-minimal-amd64
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:4cecf3a-python-minimal-arm64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-minimal-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-minimal-arm64
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:4cecf3a-python-slim-amd64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-slim-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-slim-amd64
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:4cecf3a-python-slim-arm64
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-slim-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-slim-arm64
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:4cecf3a-golang
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-golang
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-golang
ghcr.io/openhands/agent-server:4cecf3a-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:4cecf3a-java
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-java
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-java
ghcr.io/openhands/agent-server:4cecf3a-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:4cecf3a-python-minimal
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-minimal
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-minimal
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-minimal
ghcr.io/openhands/agent-server:4cecf3a-python-slim
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python-slim
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python-slim
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime-slim
ghcr.io/openhands/agent-server:4cecf3a-python
ghcr.io/openhands/agent-server:4cecf3a490f0435be85a25ab2786ab536e529952-python
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup-python
ghcr.io/openhands/agent-server:4cecf3a-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 4cecf3a-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 4cecf3a-python-amd64) are also available if needed

When TMUX_TMPDIR is unset (or too long for a tmux socket path), the
server creates <tempdir>/openhands-agent-server-<pid> for itself, but the
lifespan shutdown only restored the environment variable, so every such
start left a directory behind. Remove that directory at shutdown when
this process created it; a user-provided TMUX_TMPDIR is never touched.

Drops the known-bug marker from the F31.tmux-dir-cleanup map bullet.

Fixes #5932

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg
@enyst enyst self-assigned this Oct 14, 2026
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   api.py3622294%129, 173, 187–190, 260, 266, 322, 327, 340–342, 391, 397, 401, 422–423, 627, 693, 696, 702
TOTAL497461982560% 

@enyst
enyst marked this pull request as ready for review October 14, 2026 02:34
@enyst
enyst requested a review from all-hands-bot October 14, 2026 02:44
Address the issue triage: removal stays best-effort but a failure is now
logged instead of silently ignored, and tests cover the too-long
TMUX_TMPDIR fallback (user value restored and kept, substitute removed)
and the failure path.

Fixes #5932

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Scope: This change belongs in this repository. It touches agent-server lifecycle code (_ensure_server_tmux_tmpdir / api_lifespan in openhands-agent-server/openhands/agent_server/api.py), its unit tests, and the verify-agent-server feature map. It should not move repositories or be closed.

Why this stops for a decision: The PR body asks for it directly ("Decision needed ... A maintainer should confirm which gate they prefer"), and the two candidate gates are not behaviorally equivalent. Gating removal on "this process created the directory" leaves a pre-existing, same-named default directory behind, whereas the issue-triage suggestion (the env-restore guard) would remove it. That difference is real in the shared-/tmp-across-PID-namespaces case the linked issue (#5932) calls out, and it also decides whether acceptance criterion 1 of #5932 ("When the server defaulted its own TMUX_TMPDIR, a graceful shutdown removes that directory") is met as written or is being deliberately narrowed to directories this process created.

Choosing the removal-ownership policy is a maintainer call, so this review defers rather than recording an implementation verdict.

🛑 MAINTAINER DECISION REQUIRED

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands) on behalf of the requesting user.

Verdict: APPROVE.

Scope. Agent Server lifecycle/cleanup — belongs in this repo. shutil is stdlib, so no packaging change; _ensure_server_tmux_tmpdir is private with api_lifespan as its only caller, and no public API, default, or wire contract changes.

Decision gate resolved. The author's "Decision needed" (gate removal on a new created flag vs the env-restore guard) was answered by the maintainer (enyst, 2026-10-14 14:30): "Let's go with created though." The head implements that choice, so the open question is closed.

Verified at 4cecf3a4

  • uv run pytest -q tests/agent_server/test_api.py -> 51 passed (reproduced locally).
  • CI: all 53 check-runs on the head are success (agent-server-tests, smoke-test, cross-tests, pre-commit, REST API (OpenAPI), ...).
  • control-agent-server map check -> ok (34 files, 935 IDs, no problems); F31.tmux-dir-cleanup passes with its # bug marker removed.
  • Independent live before/after through the real entry point (python -m openhands.agent_server, graceful SIGTERM), reproducing the reported symptom:
    • baseline main@6e7a853: <tmpdir>/openhands-agent-server-<pid> still exists after "Application shutdown complete.";
    • head 4cecf3a4: same workflow, the directory is removed.
      This confirms the reported root cause and agrees with the PR's map-level xfail (baseline) -> pass (branch) proof.

Behavior notes (non-blocking). Removal runs in the lifespan finally after the conversation services close, gated on the process having created the directory, matching the maintainer's choice; a failure is logged as a warning and shutdown continues (tested). A pre-existing same-named directory is intentionally kept, as the description states. The startup stale-session cleanup is unchanged.

No material correctness, security, compatibility, or architecture findings.

@enyst
enyst merged commit d60b176 into main Oct 14, 2026
59 checks passed
@enyst
enyst deleted the claude/umbrella-bug-fixes-tgz9ai-tmux-dir-cleanup branch October 14, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Agent server leaves its per-process TMUX_TMPDIR (openhands-agent-server-PID) behind after a graceful shutdown

3 participants