FoundationAgents/OpenManus · error · RuntimeError
Failed to write file: {e}
Error message
Failed to write file: {e} What it means
Catch-all RuntimeError from DockerSandbox.write_file (app/sandbox/core/sandbox.py:230): failures while creating the parent dir (run_command mkdir), building the tar stream (_create_tar_stream), or pushing it with container.put_archive. The underlying cause is in {e}. Note put_archive writes to parent_dir or "/" with only the basename in the tar, so path resolution mistakes surface here too.
Source
Thrown at app/sandbox/core/sandbox.py:230
resolved_path = self._safe_resolve_path(path)
parent_dir = os.path.dirname(resolved_path)
# Create parent directory
if parent_dir:
await self.run_command(f"mkdir -p {parent_dir}")
# Prepare file data
tar_stream = await self._create_tar_stream(
os.path.basename(path), content.encode("utf-8")
)
# Write file
await asyncio.to_thread(
self.container.put_archive, parent_dir or "/", tar_stream
)
except Exception as e:
raise RuntimeError(f"Failed to write file: {e}")
def _safe_resolve_path(self, path: str) -> str:
"""Safely resolves container path, preventing path traversal.
Args:
path: Original path.
Returns:
Resolved absolute path.
Raises:
ValueError: If path contains potentially unsafe patterns.
"""
# Check for path traversal attempts
if ".." in path.split("/"):
raise ValueError("Path contains potentially unsafe patterns")
resolved = (View on GitHub (pinned to 52a13f2a57)
Solutions
- Read {e} — it distinguishes mkdir failure vs put_archive failure.
- Write inside config.work_dir (the rw-mounted volume) rather than container-system paths.
- Check the volume bindings are mode 'rw' and the host dir is writable.
- Clean container disk (docker system prune inside the image's storage) or increase storage.
Defensive patterns
Strategy: try-catch
Validate before calling
writable = (await sandbox.run_command(f"test -w {shlex.quote(os.path.dirname(abs_path))} && echo 1")).strip() == "1" Try / catch
try:
await sandbox.write_file(path, content)
except RuntimeError as e:
log.error("write failed: %r", e)
raise Prevention
- Write only under config.work_dir (rw volume)
- Ensure volume bindings use mode 'rw'
- Avoid trailing slashes / empty basenames in paths
- Keep container disk below capacity
When it happens
Trigger: mkdir -p failing because the parent is read-only or owned by another user in the container; put_archive failing due to permissions on the target dir; tar creation errors (invalid name for very long basenames); Docker API errors (daemon restarting, disk full).
Common situations: Writing to system paths (/etc) as a non-root container user; container disk full after heavy use; basename/path mismatch when the caller passes a path ending in '/' (empty basename); volume mount is read-only (mode 'ro' in bindings).
Related errors
- Failed to read file: {e}
- Failed to create sandbox: {e}
- File not found: {path}
- Source file is empty: {src_path}
- Failed to copy file: {e}
AI-assisted analysis of FoundationAgents/OpenManus@52a13f2a57 (2026-08-15).
Data as JSON: /api/errors/491532d1f3e37a02.
Report an issue: GitHub.