fix(fs): give each fs.write a unique temp file to avoid concurrent-save corruption

fs.write 的临时文件名只含 pid,同进程内对同一路径的并发保存共享同
一个 tmp:两个 writeFile 会交错/相互截断,先完成者 rename 走 tmp
后,另一个 rename 必然 ENOENT 报错,或目标文件最终是两份草稿的字
节混合,而编辑器仍显示"已保存"。"open to the side"会为同一路径
开第二个编辑器 tab,窗口期真实存在。

对齐 writeWorkspaceUpload 的既有方案(fs-operations.ts):隐藏文件
+ randomUUID + .tmp 后缀,并给 writeFile 加 wx 标志。失败路径的
rm 也因名字唯一不再可能误删他人正在写的 tmp。

新增契约测试:同路径并发 fs.write 全部成功且文件内容必为其中一份
完整草稿。
This commit is contained in:
Melvin committed 2026-09-25 03:02:30 +08:00
1 parent d641d2e211
commit 2e22b9004b
2 files changed
+32 -2

No files matched your search

+7 -2
View File
@@ -12,6 +12,7 @@
* All operations are conversation-scoped: requests carry a sessionId and the
* session's authoritative cwd comes from the session store.
*/
import { randomUUID } from 'node:crypto'
import { mkdir, open, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'
import { basename, dirname, extname, isAbsolute, join } from 'node:path'
import type { IncomingMessage } from 'node:http'
@@ -307,10 +308,14 @@ function buildApi(
const { cwd } = await cwdOf(payload)
const path = await ensureWorkspaceWritePath(cwd, requireString(payload, 'path'), fenceEnabledOf(getSettings))
const content = requireString(payload, 'content')
const tmp = `${path}.dsh-sidebar-tmp-${process.pid}`
// Per-request temp name (same pattern as writeWorkspaceUpload): a
// pid-suffixed name is shared by every concurrent save to the same
// path, letting two writers interleave into one temp file — and the
// failure-path rm could delete the other writer's in-flight temp.
const tmp = join(dirname(path), `.${basename(path)}.dsh-sidebar-tmp-${randomUUID()}.tmp`)
try {
await mkdir(dirname(path), { recursive: true })
await writeFile(tmp, content, 'utf8')
await writeFile(tmp, content, { encoding: 'utf8', flag: 'wx' })
await rename(tmp, path)
} catch (error) {
await rm(tmp, { force: true }).catch(() => {})
+25
View File
@@ -533,6 +533,31 @@ describe('session cwd resolution over the API route', () => {
}
})
it('concurrent fs.write calls to the same path do not corrupt each other', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-concurrent-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const target = join(workspace, 'notes.txt')
const draftA = 'A'.repeat(200000)
const draftB = 'B'.repeat(200000)
// Two editors of the same file ("open to the side" mints a second tab
// for one path) saving within the temp→rename window. Guards the
// contract: every concurrent save succeeds and the published file is
// one complete draft (never byte-mixed, never a failed rename).
const results = await Promise.allSettled([
invoke(route, 'fs.write', { sessionId: 'concurrent', path: target, content: draftA }),
invoke(route, 'fs.write', { sessionId: 'concurrent', path: target, content: draftB }),
])
for (const result of results) expect(result.status).toBe('fulfilled')
const written = readFileSync(target, 'utf8')
expect([draftA, draftB]).toContain(written)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('rejects media and HTML reads through a workspace symlink', async () => {
if (!canCreateSymlink) return
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-route-symlink-security-'))