Skip to content

Handle 'already in use' error when R2 bucket is already mounted#209

Open
AnandChowdhary wants to merge 2 commits intocloudflare:mainfrom
AnandChowdhary:fix/handle-r2-already-mounted-error
Open

Handle 'already in use' error when R2 bucket is already mounted#209
AnandChowdhary wants to merge 2 commits intocloudflare:mainfrom
AnandChowdhary:fix/handle-r2-already-mounted-error

Conversation

@AnandChowdhary
Copy link

@AnandChowdhary AnandChowdhary commented Feb 8, 2026

Summary

Two bugs in R2 mount/sync handling that cause errors on fresh deployments and subsequent requests:

1. Handle "already in use" error when R2 bucket is already mounted

  • isR2Mounted() runs mount | grep "s3fs on /data/moltbot" inside the sandbox to detect existing mounts, but this check is unreliable — it can return false even when the bucket is already mounted (due to process polling timing, the grep not matching, etc.)
  • When isR2Mounted() incorrectly returns false, mountBucket() is called again, which throws InvalidMountConfigError: Mount path "/data/moltbot" is already in use by bucket "moltbot-data"
  • Added a check for the "already in use" substring in the error message before falling back to the unreliable mount | grep check

2. Ensure gateway is running before syncing to R2

  • syncToR2 only mounted R2 but never started the gateway
  • On a fresh container, openclaw onboard (which creates openclaw.json) only runs during gateway startup via start-openclaw.sh
  • If "Sync now" is triggered before any other route starts the gateway, the config file doesn't exist and sync fails with "no config file found"
  • Added ensureMoltbotGateway() call before syncing

Changes

  • src/gateway/r2.ts: Added early return in catch block when error contains "already in use"
  • src/gateway/r2.test.ts: Added test case for the InvalidMountConfigError scenario
  • src/gateway/sync.ts: Added ensureMoltbotGateway() call before sync, import from ./process
  • src/gateway/sync.test.ts: Added mock for ensureMoltbotGateway

Test plan

  • All 85 tests pass across 7 test files
  • New test verifies mountR2Storage returns true when mountBucket throws "already in use"
  • syncToR2 tests pass with mocked ensureMoltbotGateway

🤖 Generated with Claude Code

AnandChowdhary and others added 2 commits February 8, 2026 17:59
The isR2Mounted() check (which runs `mount | grep` inside the sandbox)
is unreliable and can return false even when the bucket is already
mounted. This causes mountBucket() to be called again on subsequent
requests, throwing InvalidMountConfigError. Rather than relying solely
on the unreliable mount check in the catch block, detect the "already
in use" error message and treat it as a successful mount.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
syncToR2 only mounted R2 but never started the gateway. On a fresh
container, openclaw onboard (which creates openclaw.json) only runs
during gateway startup. If "Sync now" is triggered before any other
route starts the gateway, the config file doesn't exist and sync fails
with "no config file found". Call ensureMoltbotGateway before syncing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant