fix(slides): serialize concurrent saves to prevent file corruption (#155)
* fix(slides): serialize concurrent saves to prevent file corruption The save() function had no in-flight guard: double-tap Ctrl+S, a user-initiated save during the 30s autosave, or a close-save during autosave all issued overlapping slides:save IPC calls. The main-process handler writes via createWriteStream directly to the target path — two concurrent writes interleave zip streams, truncate the pptx, or fail with EPERM/EBUSY on Windows, then both completions race on the dirty flags. Queue saves behind a promise-chain tail (the same pattern as the docs save serializer and PR #149's markdown fix) so only one write runs at a time. * fix(slides): mock UI-only imports in save-serialization test (fix canvas in CI) --------- Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
co-authored by
Aniruddha Adak
parent
eec2737dd0
commit
4846ace175
@@ -39,25 +39,45 @@ export function adoptSavedSlides(ctx: ActionCtx, next: RenderSlide[]): void {
|
||||
ctx.setSlides(next)
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes save passes: a call that arrives while a save is in flight waits
|
||||
* for it instead of running concurrently. Two overlapping saves write the
|
||||
* same file with two `createWriteStream` pipes — interleaved zip streams,
|
||||
* truncated pptx, or EPERM/EBUSY on Windows. The queue is a simple promise
|
||||
* chain: each caller awaits the previous tail, then runs its own pass.
|
||||
*/
|
||||
let saveTail: Promise<boolean> | null = null
|
||||
|
||||
export async function save(ctx: ActionCtx, quiet = false): Promise<boolean> {
|
||||
await flushActiveEdit(ctx)
|
||||
await ctx.flushNotes()
|
||||
const r = await window.slidesApi.save()
|
||||
if (r.ok) {
|
||||
if (r.slides) adoptSavedSlides(ctx, r.slides)
|
||||
if (r.path) ctx.setPath(r.path)
|
||||
ctx.setDirty(false)
|
||||
const saved = t('appStatusSaved')
|
||||
ctx.setStatus(saved)
|
||||
if (!quiet) showToast(saved)
|
||||
} else {
|
||||
const failed = t('appStatusSaveFailed', { error: r.error ?? t('appErrorCanceled') })
|
||||
ctx.setStatus(failed)
|
||||
// quiet suppresses the success toast only — a failed save (incl. the 30s
|
||||
// auto-save) must surface, or edits silently stop reaching disk
|
||||
showToast(failed, 'error')
|
||||
}
|
||||
return r.ok
|
||||
const prior = saveTail
|
||||
const pass = (async () => {
|
||||
if (prior) await prior.catch(() => false)
|
||||
await flushActiveEdit(ctx)
|
||||
await ctx.flushNotes()
|
||||
const r = await window.slidesApi.save()
|
||||
if (r.ok) {
|
||||
if (r.slides) adoptSavedSlides(ctx, r.slides)
|
||||
if (r.path) ctx.setPath(r.path)
|
||||
ctx.setDirty(false)
|
||||
const saved = t('appStatusSaved')
|
||||
ctx.setStatus(saved)
|
||||
if (!quiet) showToast(saved)
|
||||
} else {
|
||||
const failed = t('appStatusSaveFailed', { error: r.error ?? t('appErrorCanceled') })
|
||||
ctx.setStatus(failed)
|
||||
// quiet suppresses the success toast only — a failed save (incl. the 30s
|
||||
// auto-save) must surface, or edits silently stop reaching disk
|
||||
showToast(failed, 'error')
|
||||
}
|
||||
return r.ok
|
||||
})()
|
||||
saveTail = pass
|
||||
pass
|
||||
.catch(() => false)
|
||||
.then(() => {
|
||||
if (saveTail === pass) saveTail = null
|
||||
})
|
||||
return pass
|
||||
}
|
||||
|
||||
export async function saveAs(ctx: ActionCtx): Promise<void> {
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
// file-actions pulls export-render → pptx-render → konva → native `canvas`
|
||||
// (missing in CI). Mock the UI-only modules so this unit test exercises
|
||||
// only the save-serialization queue.
|
||||
vi.mock('../src/renderer/export-render', () => ({ renderSlidesToPngBase64: vi.fn() }))
|
||||
vi.mock('../src/renderer/components/toast-bus', () => ({ showToast: vi.fn() }))
|
||||
vi.mock('../src/renderer/i18n/locale', () => ({ t: (k: string) => k }))
|
||||
|
||||
import { save } from '../src/renderer/file-actions'
|
||||
import type { ActionCtx } from '../src/renderer/action-context'
|
||||
|
||||
function ctx(): ActionCtx {
|
||||
return {
|
||||
flushActiveEditRef: { current: () => Promise.resolve() },
|
||||
editingActiveRef: { current: false },
|
||||
flushNotes: () => Promise.resolve(),
|
||||
slides: [],
|
||||
current: 0,
|
||||
setSlides: () => {},
|
||||
setSelectedIds: () => {},
|
||||
setEnteredGroupId: () => {},
|
||||
setEditing: () => {},
|
||||
setEditingCell: () => {},
|
||||
setDirty: () => {},
|
||||
setPath: () => {},
|
||||
setStatus: () => {},
|
||||
path: '/test/deck.pptx',
|
||||
} as unknown as ActionCtx
|
||||
}
|
||||
|
||||
describe('slides save serialization', () => {
|
||||
it('queues concurrent saves so only one IPC write runs at a time', async () => {
|
||||
let inFlight = 0
|
||||
let maxConcurrent = 0
|
||||
const mockSave = vi.fn(async () => {
|
||||
inFlight += 1
|
||||
maxConcurrent = Math.max(maxConcurrent, inFlight)
|
||||
// Simulate a slow write.
|
||||
await new Promise((resolve) => setTimeout(resolve, 50))
|
||||
inFlight -= 1
|
||||
return { ok: true }
|
||||
})
|
||||
vi.stubGlobal('window', { slidesApi: { save: mockSave } })
|
||||
|
||||
// Fire three saves simultaneously — they must queue, not overlap.
|
||||
const [a, b, c] = await Promise.all([save(ctx()), save(ctx(), true), save(ctx())])
|
||||
|
||||
expect(a).toBe(true)
|
||||
expect(b).toBe(true)
|
||||
expect(c).toBe(true)
|
||||
expect(mockSave).toHaveBeenCalledTimes(3)
|
||||
expect(maxConcurrent).toBe(1)
|
||||
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('reports failure without blocking the queue for the next caller', async () => {
|
||||
let calls = 0
|
||||
const mockSave = vi.fn(async () => {
|
||||
calls += 1
|
||||
if (calls === 1) return { ok: false, error: 'disk full' }
|
||||
return { ok: true }
|
||||
})
|
||||
vi.stubGlobal('window', { slidesApi: { save: mockSave } })
|
||||
|
||||
const first = save(ctx(), true)
|
||||
const second = save(ctx(), true)
|
||||
const [r1, r2] = await Promise.all([first, second])
|
||||
|
||||
expect(r1).toBe(false)
|
||||
expect(r2).toBe(true)
|
||||
expect(mockSave).toHaveBeenCalledTimes(2)
|
||||
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user