Repository navigation
Description
I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.
Is there an existing issue for the same bug?
- I have searched existing issues and this is not a duplicate.
Bug Description
POST /api/file/upload truncates the destination before it streams the new content into it. If the write fails part-way (disk full, a quota or file-size limit, the server process killed), the previous file is already gone and the destination holds a partial file. Consumers that overwrite files through the upload route (the SDK's and the TypeScript client's RemoteWorkspace.file_upload / fileUpload, and tools built on them) can lose a file that existed before the upload started.
Expected Behavior
The overwrite is atomic: the new content is written to a temporary file beside the destination and moved into place only when the whole stream has been written. A failed upload still returns an error, but the destination keeps its previous content and no temporary file is left behind. A successful overwrite keeps the file's permissions.
Actual Behavior
The destination is truncated as soon as the upload starts. With the server's file-size limit lowered to 500 kB, a 900 kB upload over a small file fails with 500 ... [Errno 27] File too large, and the destination now holds the first 500000 bytes of the new blob: the old content is gone.
Steps to Reproduce
- In a checkout of main:
uv sync, thenSESSION_API_KEY=dev-key uv run python -m openhands.agent_server --host 127.0.0.1 --port 8000 & PID=$!. - Create the files and a first, successful upload:
D=$(mktemp -d)
printf 'previous content\n' > "$D/v2.txt"
head -c 900000 /dev/urandom > "$D/big.bin"
curl -s -H 'X-Session-API-Key: dev-key' -F "file=@$D/v2.txt" "http://127.0.0.1:8000/api/file/upload?path=$D/target.txt"
cmp "$D/v2.txt" "$D/target.txt" && echo intact- Make writes fail mid-stream (Linux, util-linux):
prlimit --pid "$PID" --fsize=500000: - Overwrite with the large file:
curl -s -H 'X-Session-API-Key: dev-key' -F "file=@$D/big.bin" "http://127.0.0.1:8000/api/file/upload?path=$D/target.txt"returns 500File too large(expected). prlimit --pid "$PID" --fsize=unlimited:, thenwc -c "$D/target.txt"prints500000, andcmp "$D/v2.txt" "$D/target.txt"fails: the previous file is lost.
With the verify-agent-server skill: control-agent-server map run --file F14 --only F14.upload-atomic --fresh reports the known-bug bullet as an expected failure while the bug lasts.
Acceptance Criteria
- An upload that fails part-way through leaves an existing destination file with its previous content, and leaves no temporary file in the destination directory.
- A successful upload still creates missing parent directories and replaces the content; an overwrite keeps the existing file's permissions, and a new file gets the usual umask-based permissions.
- Uploading to a symlink still writes the file the link points to (the link itself is kept).
-
tests/agent_server/test_file_router.pycovers a failed overwrite. - The map bullet
F14.upload-atomicpasses with its# bugmarker removed.
Installation Method
uv sync in a checkout of main
Version
main at 6e7a853
Additional Context
Root cause, in openhands-agent-server/openhands/agent_server/file_router.py: _upload_file (line 85) opens target_path with "wb" (line 104), which truncates it, and then streams 8 KiB chunks into it (lines 105-106). Any exception during the loop is mapped to 500 (lines 113-118), but nothing restores the old content.
Suggested fix: write to a temporary file in the same directory (so the rename stays on one filesystem), os.replace it onto the destination when the stream completes, and remove the temporary file on any failure.
Related: #5856 changes the same function to report file_size; the upload-onto-a-directory error mapping (F14.upload-onto-directory) is a separate item.
Found by the first full agentic run of verify-agent-server; tracked in #5653.
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
The report is reproducible against the real agent-server upload route and the root cause is confirmed in openhands-agent-server/openhands/agent_server/file_router.py: _upload_file opens the destination with "wb" (truncating it) and then streams 8 KiB chunks, so any mid-stream failure (disk full, quota/size limit, process death) destroys the previous file and leaves a partial one. The bug template's required sections (Bug Description, Expected Behavior, Actual Behavior, Steps to Reproduce, Acceptance Criteria, Installation Method, Version) are all present, so no additional readiness sections are required.
Scope: make _upload_file atomic — write the incoming stream to a temporary file in the destination's directory and move it into place (os.replace) only on full success, removing the temporary file on any failure. The repository already has one authoritative mechanism for this pattern to reuse: atomic_write_text in openhands-sdk/openhands/sdk/utils/files.py writes a sibling .<name>.<random>.tmp file and os.replaces it, and is_temp_file treats any *.tmp name as in-progress; follow that convention so the scratch file is not mistaken for a real one. This applies to both the global /api/file/upload route and the conversation-scoped upload that shares the helper.
Non-goals (tracked separately, not part of this issue): the F14.upload-onto-directory error mapping (a masked 500 becoming a 4xx), the file_size upload-response change in PR #5856 (F14.upload-size), and any client-side (clients/typescript, SDK RemoteWorkspace) changes.
Acceptance Criteria
- A failed overwrite (the stream errors after the destination was opened) leaves the destination holding its previous content unchanged; the request still returns an error (HTTP 500 with the failure detail, as today).
- A failed upload leaves no temporary file behind in the destination directory.
- A successful upload to a new path creates missing parent directories and writes the exact bytes, with the usual umask-based permissions.
- A successful overwrite replaces the destination's content and preserves the destination's existing permissions.
- Uploading to a path that is a symlink writes the file the link points to and keeps the link itself (it does not replace the symlink with a regular file).
-
tests/agent_server/test_file_router.pycovers a failed overwrite: it exercises a mid-write failure and asserts the previous content survives and no temporary file remains. - The verify-agent-server feature-map bullet
F14.upload-atomicpasses with its# bugmarker removed (the recipe'ssame eq trueassertion holds).
Activity
enyst commentedon Oct 14, 2026
I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.
On the triage points, as implemented in #5902:
atomic_write_text's convention: the upload's temp file is named.<name>.<random>.tmpbeside the destination, sois_temp_filetreats it as in progress. The function itself is not reused, because it writes a whole string in text mode with owner-only (0600) permissions, while the upload streams bytes and must keep the destination's permissions; the new helper is_atomic_writeinfile_router.py._upload_file, so both are covered.500with the failure detail; the upload-onto-directory mapping and thefile_sizeresponse (fix(agent-server): report the uploaded file_size in the upload response #5856) are left alone.F14.upload-atomicreportsxfailtwice on main andxpasstwice on the branch, and passes once its# bugmarker is removed in the same PR.None of these needs a maintainer decision.
Generated by Claude Code