Skip to content

fix(storage): 修复 JSON 并发写入共享暂存文件的问题 - #109

Draft
Bluuok wants to merge 1 commit into
helsome:mainfrom
Bluuok:fix/json-concurrent-writes
Draft

Bluuok wants to merge 1 commit into
helsome:mainfrom
Bluuok:fix/json-concurrent-writes

Conversation

@Bluuok

@Bluuok Bluuok commented Sep 16, 2026

Copy link
Copy Markdown

改动说明

What

  • 为每次 JsonFileStore.write() 使用独立、排他创建的 UUID 暂存文件。
  • 仅对同一目标文件的最终替换做进程内串行化,避免 Windows 下多个 store 实例同时 rename 失败。
  • 失败时只清理当前写入者拥有的暂存文件,并增加同文件并发写入、跨实例写入和失败清理测试。

Why

原实现固定使用 <target>.tmp。并发写入同一 JSON 文件时,多个调用会互相覆盖暂存内容或争抢同一个 rename 源文件,可能返回 STORAGE_WRITE_FAILED,也可能遗留暂存文件。

关联 Issue

未发现对应的直接 Issue。本 PR 是针对 JsonFileStore 并发持久化边界的独立修复。

测试报告(正式审核前必填)

环境

  • Bun:1.4.0
  • OS:Microsoft Windows 11 家庭版 中文版

实际执行命令与结果

bun test --isolate packages/shared/src/storage/json-file-store.concurrent.test.ts packages/shared/src/storage/repositories.test.ts
→ 13 pass / 0 fail,26 expect() calls

bun test packages/shared --isolate
→ 913 pass / 0 fail,86 files,3577 expect() calls

bun run typecheck
→ core / i18n / shared / ui / electron 全部 exit 0

git diff --check
→ exit 0,无输出

已知失败 / Baseline(如有)

  • 无已知失败;以上全量共享包测试在本分支通过。

  • 仓库 CI 固定使用 Bun 1.4.2;本地实际使用 Bun 1.4.0,Bun 1.4.2 的 CI 结果待 GitHub Actions 复核。

  • 已提供实际测试命令与 pass/fail 结果

  • 已说明测试环境

  • 如果存在已知 baseline / 环境失败,已提供 main 对照或说明

  • 核心改动已有对应 focused test / smoke / integration 验证

UI 截图(仅可见 UI 变化时必填)

  • 本 PR 无可见 UI 变化(无需截图)

Scope / 后续

  • 本 PR 保持现有“写入暂存文件后替换目标”的持久化模型,只处理同一 Node 进程内的目标文件发布竞争。
  • 本 PR 不引入跨进程锁、read-modify-write 事务或额外 fsync 语义;这些属于后续存储层设计范围。

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