mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-12-02 08:59:29 +00:00
The Windows CI leg failed on the cross-process test with EPERM from renameSync. It is not a test artifact. Windows refuses a rename over a file another process holds open rather than waiting, with ERROR_ACCESS_DENIED or ERROR_SHARING_VIOLATION, and reads of these two files are deliberately lock-free on both sides. So any tray or app read that overlaps a writer's replace fails that write, and since this branch made writers report a failure instead of swallowing it, the outcome is exactly the lost update the lock exists to prevent, reached by another route. The test's reader thread only made it visible by reading the way both apps really do, continuously and without the lock. The replace is now retried on that error class, ten attempts about 20ms apart, on both sides: replace_file in settings.rs, used by settings patch and by the dock's write_state_at, and renameOnto behind writeFileAtomic in tray-settings.ts. The lock is already held at that point, so the wait can only ever be on a reader, never on another writer, and a read of one of these files is microseconds of open file. Once the budget is out the original error is returned unchanged and the caller cleans up as before. On POSIX, where a rename over an open file cannot fail this way, it compiles out in Rust and short-circuits on process.platform in Node. Reads stay lock-free, which is the right trade and the reason the files can be read at all without coordination; both read paths now say what it costs and where that cost is absorbed. The reader thread stays as it is, and is now the regression test for the retry as well: it is what puts a replace under a continuous reader. The retry itself is covered on the Node side by faking the platform and the rename, since a rename that fails this way cannot be produced on a Mac: a reader waited out and the write landing, the budget running out with the old file still whole, and an error a reader cannot cause not being waited out at all. The Rust half is compiled out off Windows, so CI is its only proof.
154 lines
6.5 KiB
TypeScript
154 lines
6.5 KiB
TypeScript
// @vitest-environment node
|
|
// How the tray app's settings files are written, as opposed to what goes in them.
|
|
//
|
|
// The tray app reads `windows-settings.json` and `windows-dock.json` on a cadence of its own
|
|
// and again on every `--reload-settings`, while this side writes them. A plain
|
|
// `writeFileSync` truncates the target and then fills it, so a read landing between the two
|
|
// sees an empty or half-written file, parses it as no preferences at all, and every setting
|
|
// in it collapses to a default.
|
|
//
|
|
// Its own file because pinning the write and the rename means mocking `node:fs` wholesale,
|
|
// which the rest of the tray-settings suite runs against for real.
|
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { tmpdir } from 'node:os'
|
|
import { dirname, join } from 'node:path'
|
|
|
|
const actualFs = await vi.importActual<typeof import('node:fs')>('node:fs')
|
|
const { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } = actualFs
|
|
|
|
// vi.mock factories are hoisted above the module body, so the spies they close over have to
|
|
// be hoisted with them. Each defaults to the real thing and is redirected per test.
|
|
const { writeMock, renameMock } = vi.hoisted(() => ({
|
|
writeMock: vi.fn(),
|
|
renameMock: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('node:fs', async orig => {
|
|
const actual = await orig<typeof import('node:fs')>()
|
|
writeMock.mockImplementation(actual.writeFileSync)
|
|
renameMock.mockImplementation(actual.renameSync)
|
|
return { ...actual, writeFileSync: writeMock, renameSync: renameMock }
|
|
})
|
|
|
|
const { patchTrayFile, writeFileAtomic } = await import('./tray-settings')
|
|
|
|
let home: string
|
|
const dockPath = () => join(home, '.config', 'codeburn', 'windows-dock.json')
|
|
|
|
/** Every path `writeFileSync` was given, which must never include the target itself. */
|
|
function written(): string[] {
|
|
return writeMock.mock.calls.map(call => String((call as [string])[0]))
|
|
}
|
|
|
|
beforeEach(() => {
|
|
home = mkdtempSync(join(actualFs.realpathSync(tmpdir()), 'tray-atomic-'))
|
|
mkdirSync(join(home, '.config', 'codeburn'), { recursive: true })
|
|
writeMock.mockClear()
|
|
renameMock.mockClear()
|
|
writeMock.mockImplementation(actualFs.writeFileSync)
|
|
renameMock.mockImplementation(actualFs.renameSync)
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(home, { recursive: true, force: true })
|
|
})
|
|
|
|
describe('writing a file the tray app reads', () => {
|
|
it('writes a temp file in the same directory and renames it over the target', () => {
|
|
patchTrayFile('dock', { scale: 1.1 }, home)
|
|
|
|
expect(written()).toHaveLength(1)
|
|
const temp = written()[0]!
|
|
// The target is never opened for writing, so it is never briefly empty.
|
|
expect(temp).not.toBe(dockPath())
|
|
// The same directory, because a rename across volumes is a copy, and a copy is not atomic.
|
|
expect(dirname(temp)).toBe(dirname(dockPath()))
|
|
expect(renameMock).toHaveBeenCalledWith(temp, dockPath())
|
|
expect(JSON.parse(readFileSync(dockPath(), 'utf8'))).toEqual({ scale: 1.1 })
|
|
expect(readdirSync(dirname(dockPath()))).toEqual(['windows-dock.json'])
|
|
})
|
|
|
|
it('still merges into what the tray app already wrote', () => {
|
|
writeFileSync(dockPath(), JSON.stringify({ enabled: true, placement: { docked: 'right' } }))
|
|
|
|
expect(patchTrayFile('dock', { scale: 1.1 }, home)).toEqual({
|
|
enabled: true,
|
|
placement: { docked: 'right' },
|
|
scale: 1.1,
|
|
})
|
|
expect(JSON.parse(readFileSync(dockPath(), 'utf8'))).toEqual({
|
|
enabled: true,
|
|
placement: { docked: 'right' },
|
|
scale: 1.1,
|
|
})
|
|
})
|
|
|
|
/// The whole point of the rename: everything up to it can be lost without the tray app ever
|
|
/// seeing anything but the file it had.
|
|
it('leaves the old file whole when the rename never happens', () => {
|
|
writeFileSync(dockPath(), JSON.stringify({ enabled: true, scale: 0.6 }))
|
|
renameMock.mockImplementation(() => { throw Object.assign(new Error('EPERM'), { code: 'EPERM' }) })
|
|
|
|
expect(() => patchTrayFile('dock', { scale: 1.1 }, home)).toThrow('EPERM')
|
|
|
|
expect(JSON.parse(readFileSync(dockPath(), 'utf8'))).toEqual({ enabled: true, scale: 0.6 })
|
|
// And no half-written temp file is left in the directory the tray app reads.
|
|
expect(readdirSync(dirname(dockPath()))).toEqual(['windows-dock.json'])
|
|
})
|
|
|
|
// Windows refuses to rename over a file another process has open, so a lock-free read on
|
|
// either side can fail a write on the other. The retry is Windows-only and this machine is
|
|
// not Windows, so the platform is what is faked here; the real API behaviour is CI's to prove.
|
|
describe('on Windows, where a rename over an open file is refused', () => {
|
|
const real = process.platform
|
|
const failWith = (code: string) => Object.assign(new Error(code), { code })
|
|
beforeEach(() => { Object.defineProperty(process, 'platform', { value: 'win32', configurable: true }) })
|
|
afterEach(() => { Object.defineProperty(process, 'platform', { value: real, configurable: true }) })
|
|
|
|
it('waits out a reader and lands the write', () => {
|
|
let refusals = 2
|
|
renameMock.mockImplementation((...args: Parameters<typeof actualFs.renameSync>) => {
|
|
if (refusals-- > 0) throw failWith('EPERM')
|
|
return actualFs.renameSync(...args)
|
|
})
|
|
|
|
writeFileAtomic(dockPath(), 'landed')
|
|
|
|
expect(renameMock).toHaveBeenCalledTimes(3)
|
|
expect(readFileSync(dockPath(), 'utf8')).toBe('landed')
|
|
})
|
|
|
|
it('gives up with the same error once the budget is out, leaving nothing behind', () => {
|
|
writeFileSync(dockPath(), 'old')
|
|
renameMock.mockImplementation(() => { throw failWith('EACCES') })
|
|
|
|
expect(() => writeFileAtomic(dockPath(), 'new')).toThrow('EACCES')
|
|
|
|
expect(renameMock).toHaveBeenCalledTimes(10)
|
|
expect(readFileSync(dockPath(), 'utf8')).toBe('old')
|
|
expect(readdirSync(dirname(dockPath()))).toEqual(['windows-dock.json'])
|
|
})
|
|
|
|
it('does not wait out an error a reader cannot cause', () => {
|
|
renameMock.mockImplementation(() => { throw failWith('ENOSPC') })
|
|
|
|
expect(() => writeFileAtomic(dockPath(), 'new')).toThrow('ENOSPC')
|
|
expect(renameMock).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
|
|
it('reports a failed write rather than letting it read as a save', () => {
|
|
writeMock.mockImplementation(() => { throw new Error('ENOSPC') })
|
|
|
|
expect(() => writeFileAtomic(dockPath(), 'x')).toThrow('ENOSPC')
|
|
expect(renameMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('creates the directory when nothing has written there yet', () => {
|
|
rmSync(join(home, '.config'), { recursive: true, force: true })
|
|
|
|
writeFileAtomic(dockPath(), 'hello')
|
|
|
|
expect(readFileSync(dockPath(), 'utf8')).toBe('hello')
|
|
})
|
|
})
|