mirror of
https://github.com/iflytek/skillhub.git
synced 2026-11-17 09:31:39 +00:00
fix(cli): retry transient Windows inventory replacement failures
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
This commit is contained in:
parent
754cc0ca3c
commit
c875941bae
5 changed files with 86 additions and 4 deletions
16
cli/src/platform/atomic-rename.ts
Normal file
16
cli/src/platform/atomic-rename.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { rename } from 'node:fs/promises'
|
||||
|
||||
/** Retry only Windows sharing failures; never remove the destination or replay a transaction. */
|
||||
export async function renameAtomicFile(from: string, to: string,
|
||||
renameFile: typeof rename = rename, platform: NodeJS.Platform = process.platform): Promise<void> {
|
||||
const delays = [20, 40, 80, 160, 320]
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try { await renameFile(from, to); return }
|
||||
catch (error) {
|
||||
const code = (error as NodeJS.ErrnoException).code
|
||||
const delay = delays[attempt]
|
||||
if (platform !== 'win32' || !['EPERM', 'EBUSY'].includes(code ?? '') || delay === undefined) throw error
|
||||
await new Promise(resolve => setTimeout(resolve, delay))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
import { renameAtomicFile } from '../platform/atomic-rename'
|
||||
import { createHash } from 'node:crypto'
|
||||
import { z } from 'zod'
|
||||
import { isDeepStrictEqual } from 'node:util'
|
||||
import { lstat, open, readFile, rename, rm } from 'node:fs/promises'
|
||||
import { lstat, open, readFile, rm } from 'node:fs/promises'
|
||||
import { dirname, isAbsolute, relative, resolve, join } from 'node:path'
|
||||
import { lock } from 'proper-lockfile'
|
||||
import { acquireAcquisitionGate } from '../services/skill-target-lock'
|
||||
|
|
@ -285,7 +286,7 @@ export class InventoryStore {
|
|||
const file = await open(tmpPath, 'wx', 0o600)
|
||||
try { await file.writeFile(payload); await file.sync() } finally { await file.close() }
|
||||
parseInventory(JSON.parse(await readFile(tmpPath, 'utf-8')))
|
||||
await rename(tmpPath, this.path)
|
||||
await this.replaceInventoryFile(tmpPath)
|
||||
if (process.platform !== 'win32') {
|
||||
const directory = await open(dirname(this.path), 'r')
|
||||
try { await directory.sync() } finally { await directory.close() }
|
||||
|
|
@ -295,6 +296,10 @@ export class InventoryStore {
|
|||
}
|
||||
}
|
||||
|
||||
protected async replaceInventoryFile(temporaryPath: string): Promise<void> {
|
||||
await renameAtomicFile(temporaryPath, this.path)
|
||||
}
|
||||
|
||||
private async acquireLock(): Promise<() => Promise<void>> {
|
||||
// proper-lockfile stale stat/remove is not atomic: serialize acquisition so a delayed
|
||||
// recoverer cannot remove another process's newly acquired inventory lock.
|
||||
|
|
|
|||
29
cli/test/unit/platform/atomic-rename.test.ts
Normal file
29
cli/test/unit/platform/atomic-rename.test.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { expect, test } from 'bun:test'
|
||||
import { mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { renameAtomicFile } from '../../../src/platform/atomic-rename'
|
||||
|
||||
test.each(['EPERM', 'EBUSY'])('Windows atomic rename retries transient %s without removing the destination', async code => {
|
||||
const root = await mkdtemp(join(tmpdir(), 'skillhub-atomic-rename-'))
|
||||
const from = join(root, 'temporary'); const to = join(root, 'inventory')
|
||||
try {
|
||||
await writeFile(from, 'next'); await writeFile(to, 'original')
|
||||
let calls = 0
|
||||
await renameAtomicFile(from, to, async (source, destination) => {
|
||||
expect(await readFile(to, 'utf8')).toBe('original')
|
||||
expect(await readFile(from, 'utf8')).toBe('next')
|
||||
if (++calls < 3) throw Object.assign(new Error('Sharing failure'), { code })
|
||||
await rename(source, destination)
|
||||
}, 'win32')
|
||||
expect(calls).toBe(3)
|
||||
expect(await readFile(to, 'utf8')).toBe('next')
|
||||
} finally { await rm(root, { recursive: true, force: true }) }
|
||||
})
|
||||
|
||||
test.each([['linux', 'EPERM'], ['darwin', 'EBUSY'], ['win32', 'EACCES'], ['win32', 'ENOENT']] as const)(
|
||||
'%s does not retry %s outside the Windows sharing-failure boundary', async (platform, code) => {
|
||||
const error = Object.assign(new Error('Permanent failure'), { code }); let calls = 0
|
||||
await expect(renameAtomicFile('source', 'destination', async () => { calls++; throw error }, platform)).rejects.toBe(error)
|
||||
expect(calls).toBe(1)
|
||||
})
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { renameAtomicFile } from '../../../src/platform/atomic-rename'
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import { mkdir, rm, utimes, writeFile } from 'node:fs/promises'
|
||||
import { mkdir, readFile, readdir, rm, utimes, writeFile } from 'node:fs/promises'
|
||||
import { mkdtemp } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join, dirname } from 'node:path'
|
||||
|
|
@ -165,3 +166,31 @@ describe('InventoryStore', () => {
|
|||
])
|
||||
})
|
||||
})
|
||||
|
||||
test('exhausted Windows rename retries preserve inventory, clean temporary files, and do not replay the mutation', async () => {
|
||||
const home = await makeTempHome()
|
||||
const original = new InventoryStore(home)
|
||||
let attempts = 0
|
||||
const failure = Object.assign(new Error('Persistent sharing failure'), { code: 'EPERM' })
|
||||
class FailingRenameStore extends InventoryStore {
|
||||
protected override async replaceInventoryFile(temporaryPath: string): Promise<void> {
|
||||
await renameAtomicFile(temporaryPath, this.path, async () => { attempts++; throw failure }, 'win32')
|
||||
}
|
||||
}
|
||||
try {
|
||||
await original.writeAtomic({ items: [] })
|
||||
const bytes = await readFile(original.path)
|
||||
const failing = new FailingRenameStore(home)
|
||||
let mutations = 0
|
||||
await expect(failing.mutateAtomic(inventory => {
|
||||
mutations++
|
||||
inventory.items.push({ registry: 'https://registry.example', namespace: 'team', slug: 'writer', targets: [] })
|
||||
})).rejects.toBe(failure)
|
||||
expect(attempts).toBe(6)
|
||||
expect(mutations).toBe(1)
|
||||
expect(await readFile(original.path)).toEqual(bytes)
|
||||
expect((await readdir(dirname(original.path))).filter(name => name.endsWith('.tmp'))).toEqual([])
|
||||
await original.mutateAtomic(inventory => { inventory.items.push({ registry: 'https://registry.example', namespace: 'team', slug: 'other', targets: [] }) })
|
||||
expect((await original.read()).items.map(item => item.slug)).toEqual(['other'])
|
||||
} finally { await rm(home, { recursive: true, force: true }) }
|
||||
})
|
||||
|
|
|
|||
|
|
@ -113,7 +113,10 @@ test('independent prepared upgrades commit without losing siblings', async () =>
|
|||
try {
|
||||
for (let i = 0; i < 3; i++) await f.install(i, '1.0.0')
|
||||
const first = await f.prepare(0); const second = await f.prepare(1)
|
||||
expect((await Promise.all([f.runtime.executeUpgrade(first), f.runtime.executeUpgrade(second)])).map(result => result.outcome)).toEqual(['committed', 'committed'])
|
||||
const results = await Promise.allSettled([f.runtime.executeUpgrade(first), f.runtime.executeUpgrade(second)])
|
||||
const failures = results.filter(result => result.status === 'rejected')
|
||||
if (failures.length) throw new AggregateError(failures.map(result => result.reason), 'Independent upgrades failed')
|
||||
expect(results.map(result => result.status === 'fulfilled' ? result.value.outcome : undefined)).toEqual(['committed', 'committed'])
|
||||
const targets = (await f.store.read()).items[0]!.targets
|
||||
expect(f.roots.map(root => targets.find(target => target.rootDir === root.root)?.version)).toEqual(['3.0.0', '3.0.0', '1.0.0'])
|
||||
} finally { await f.close() }
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue