Skip to content

fix(agent-server): make safe_rmtree report what it could not remove - #6597

Merged
VascoSch92 merged 1 commit into
mainfrom
vasco/safe-rmtree-report-failures
Nov 7, 2026
Merged

VascoSch92 merged 1 commit into
mainfrom
vasco/safe-rmtree-report-failures

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

just fixing a bug


AGENT:

Why

safe_rmtree passes an onerror handler to shutil.rmtree that, for any error, chmods the path to S_IWRITE, retries, and logs-and-swallows whatever still fails, so rmtree itself never raises and safe_rmtree returns True. That hides three failures:

  • A tree containing a read-only subdirectory stays on disk, but the result says it was removed.
  • On a symlink to a directory, rmtree refuses ("Cannot call rmtree on a symbolic link"), and the handler then chmods the link's target directory to write-only (os.chmod follows the link). The link remains and the result is True.
  • On a regular file, the handler chmods the file to write-only, leaves it in place, and returns True.

Callers rely on the result: remove_conversation_worktree skips git worktree prune and trash raises OSError when it is False.

Summary

  • The handler now only retries PermissionError (clear the read-only bit, call the function again), ignores FileNotFoundError (already gone, as the old exists guard did), and re-raises anything else. Anything that still fails propagates to the existing except in safe_rmtree, which logs and returns False.
  • Ports the three pinned tests from [Bug]: Edge-case tests expose ~70 bugs across the SDK, agent server, tools and workspaces #6348 into a new tests/agent_server/test_utils.py, with the file's needs_permission_checks / _restore_permissions helpers.

Issue Number

Part of #6348 (items: "safe_rmtree on a symlinked dir chmods the link target to write-only and returns True while the link remains", "safe_rmtree's onerror handler swallows failures, so it returns True while a read-only subdirectory keeps the tree on disk", "safe_rmtree on a regular file returns True, leaves the file, and chmods it to write-only").

How to Test

uv run pytest tests/agent_server/test_utils.py tests/agent_server/storage tests/agent_server/test_local_storage.py

The three new tests fail on main and pass here. The other safe_rmtree users' tests pass too (test_conversation_service.py, docker_runtime/test_routes.py, storage/: 179).

Type

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

🐳 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:65fd305-python

Run

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

All tags pushed for this build

ghcr.io/openhands/agent-server:65fd305-golang-amd64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-golang-amd64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-golang-amd64
ghcr.io/openhands/agent-server:65fd305-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:65fd305-golang-arm64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-golang-arm64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-golang-arm64
ghcr.io/openhands/agent-server:65fd305-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:65fd305-java-amd64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-java-amd64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-java-amd64
ghcr.io/openhands/agent-server:65fd305-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:65fd305-java-arm64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-java-arm64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-java-arm64
ghcr.io/openhands/agent-server:65fd305-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:65fd305-python-amd64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-amd64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-amd64
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-amd64
ghcr.io/openhands/agent-server:65fd305-python-arm64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-arm64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-arm64
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-arm64
ghcr.io/openhands/agent-server:65fd305-python-minimal-amd64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-minimal-amd64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-minimal-amd64
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:65fd305-python-minimal-arm64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-minimal-arm64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-minimal-arm64
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:65fd305-python-slim-amd64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-slim-amd64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-slim-amd64
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:65fd305-python-slim-arm64
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-slim-arm64
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-slim-arm64
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:65fd305-golang
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-golang
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-golang
ghcr.io/openhands/agent-server:65fd305-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:65fd305-java
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-java
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-java
ghcr.io/openhands/agent-server:65fd305-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:65fd305-python-minimal
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-minimal
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-minimal
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-minimal
ghcr.io/openhands/agent-server:65fd305-python-slim
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python-slim
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python-slim
ghcr.io/openhands/agent-server:65fd305-python-node-runtime-slim
ghcr.io/openhands/agent-server:65fd305-python
ghcr.io/openhands/agent-server:65fd30549c5c894d3171a775e746bb8128196f23-python
ghcr.io/openhands/agent-server:vasco-safe-rmtree-report-failures-python
ghcr.io/openhands/agent-server:65fd305-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 65fd305-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., 65fd305-python-amd64) are also available if needed

The rmtree error handler chmod-ed and retried on every error and then
swallowed whatever was left, so safe_rmtree returned True while a
read-only subdirectory kept the tree on disk, and for a symlink or a
plain file it chmod-ed the target to write-only and still reported
success. Retry only permission errors, ignore entries that are already
gone, and re-raise the rest so the caller gets False.
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   utils.py36294%33, 61
TOTAL541802072962% 

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@VascoSch92
VascoSch92 marked this pull request as ready for review November 7, 2026 16:24

@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).

Reviewed fresh at head 65fd3054 (re-fetched current PR state, reviews, and check results before deciding). This is a correct, well-scoped fix for the three pinned #6348 items; I found no material correctness, security, or compatibility defect.

What I verified:

  • Handler semantics on Python 3.13 (fd-based rmtree): PermissionError → clear the read-only bit and retry (the read-only-file dance is preserved); FileNotFoundError → ignored, which is equivalent to the old os.path.exists() tolerance; any other error re-raised and unwound out of rmtree in a bounded way (no hang), landing in safe_rmtree's existing except, which logs and returns False.
  • Before/after evidence through the real entry point: reproducing the three scenarios against main's implementation shows symlink → True while the link remains and the target dir is chmod'd; read-only subdir → True while the tree stays on disk; regular file → True while the file stays and is chmod'd to write-only. On this head, all three new tests pass and the result honestly matches whether the tree is gone.
  • All seven call sites traced (trash._remove, remove_conversation_worktree, remove_conversation, _create_conversation_worktree, fork cleanup, docker_runtime routers): none relies on the old swallow-and-True; trash and worktree removal specifically depend on the now-honest False.
  • No compatibility surface touched: safe_rmtree is used only inside openhands-agent-server; no REST/OpenAPI, TypeScript client, or persisted-state change.
  • Tests: the 3 new tests pass here; tests/agent_server/storage + test_local_storage.py + test_conversation_service.py (162 passed) and docker_runtime/test_routes.py (24 passed) are green; CI on this exact head passes (only the draft-PR description validation is skipped, which is a process gate unrelated to the code).

✅ APPROVED

@VascoSch92
VascoSch92 merged commit f35b816 into main Nov 7, 2026
52 of 53 checks passed
@VascoSch92
VascoSch92 deleted the vasco/safe-rmtree-report-failures branch November 7, 2026 17:17
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.

2 participants