Skip to content

should_restore_from_r2() race condition skips workspace and skills restore #228

@cindyrui71

Description

@cindyrui71

Problem

After the workspace sync fix in #102, workspace and skills are still not restored on container startup in some cases.

should_restore_from_r2() compares timestamps between $BACKUP_DIR/.last-sync (R2) and $CONFIG_DIR/.last-sync (local). The function is called separately for each restore block — config, workspace, and skills.

The config restore runs first and copies .last-sync into $CONFIG_DIR:

cp -f "$BACKUP_DIR/.last-sync" "$CONFIG_DIR/.last-sync" 2>/dev/null || true

After this, subsequent calls to should_restore_from_r2() for workspace and skills see matching timestamps and return false, silently skipping the restore.

Affected code

start-openclaw.sh lines 58-121 — each restore block independently calls should_restore_from_r2.

Fix

Evaluate the function once before any restore operations:

DO_RESTORE=false
if should_restore_from_r2; then
    DO_RESTORE=true
fi

Then use [ "$DO_RESTORE" = true ] in each restore block instead of calling the function again.

Testing

Deployed and verified on a live Cloudflare Workers container. After fix, workspace files (IDENTITY.md, MEMORY.md, memory/) and skills are correctly restored from R2 on startup.

Co-authored with Claude Code (Opus 4.6).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions