Skip to content

fix(opencode-plugin): serialize saveState to prevent ENOENT rename race - #3885

Merged
qin-ctx merged 1 commit into
volcengine:mainfrom
kuangmi-bit:fix/opencode-plugin-savestate-race
Aug 10, 2026
Merged

qin-ctx merged 1 commit into
volcengine:mainfrom
kuangmi-bit:fix/opencode-plugin-savestate-race

Conversation

@kuangmi-bit

Copy link
Copy Markdown
Contributor

What

Fixes #3877. Concurrent saveState() calls in the OpenCode memory plugin
share the same ${statePath}.tmp temp file with no serialization, so one
rename can fail with ENOENT after another already moved the file —
observed as frequent Failed to save session state errors during
dispose/flushAll, where debounced saves race batch flushes.

Why it matters

The failure is swallowed inside saveState, leaving a stale snapshot on
disk. If the process exits between a failed save and the next successful one,
the last pending messages lose their local recovery record (server-side
commit is unaffected). The bigger the state file, the easier the race hits.

How

Serialize saves through a promise chain (enqueueSave()); at most one
write+rename runs at a time. Each queued save re-serializes the in-memory
sessions map at execution time, so the tail save always persists the
latest state — no update is lost. All four save entry points
(debouncedSaveState, flushAll, flushSession, handleSessionDeleted)
now go through the chain.

Test

New test concurrent saves never race the shared state file (#3877) in
tests/memory-session.test.mjs: slows writeFile/rename via
t.mock.method (shared CJS fs.promises singleton) to widen the race
window, fires concurrent flushAll() calls while a session is created
mid-flight, and asserts (1) the final state file contains all sessions and
(2) rename never observed a missing source (raceDetected === false).

@qin-ctx
qin-ctx merged commit c25a721 into volcengine:main Aug 10, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in OpenViking project Aug 10, 2026
frankyang2008-eng added a commit to frankyang2008-eng/OpenViking that referenced this pull request Oct 9, 2026
Sync upstream main (e546b79) - 19 commits, 315 files:

- refactor(client): remove Python embedded mode (volcengine#3712)
- perf(queue): stream semantic vectorization tasks (volcengine#3636)
- perf(queuefs): defer embedding content materialization (volcengine#3871)
- feat(memory): support event tag filtering (volcengine#3850)
- feat(pathlock): keep lease alive across handoff (volcengine#3903)
- feat(server): refresh read-replica API key index via store watcher (volcengine#3857)
- feat(agent-evolution): add trajectory date filters (volcengine#3915)
- feat(agent-evolution): add trajectory task query (volcengine#3856)
- feat(web-studio): add account deletion workflow (volcengine#3912)
- feat(web-studio): add scheduled resource sync management (volcengine#3909)
- fix(watch): avoid global lock during resource moves (volcengine#3833)
- fix(observer): ignore historical lock diagnostics in health (volcengine#3920)
- fix(session): validate archive names when loading history (volcengine#3922)
- fix(task-tracker): delete expired persisted tasks (volcengine#3913)
- fix(storage): exclude zero-byte files from index expectations (volcengine#3904)
- fix(semantic): reduce entity hallucinations in summaries (volcengine#3908)
- fix(retrieval): record first-turn session recalls (volcengine#3907)
- fix(opencode-plugin): serialize saveState to prevent ENOENT race (volcengine#3885)
- fix(examples): align quick start with server mode (volcengine#3923)

Reviewed: zero conflicts, all local dev-branch modifications preserved,
no references to deleted embedded client modules, config backward-compatible.
frankyang2008-eng pushed a commit to frankyang2008-eng/OpenViking that referenced this pull request Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug] 记忆插件 saveState 并发竞态导致本地状态文件 ENOENT(rename 失败)

2 participants