perf(daemon): async snapshot worker — debounced off-main writes#49
Merged
Conversation
Closes #36. Snapshot encoding (gzip+bincode for `persist_scrollback = true`), serde_json serialization, and disk I/O previously all ran synchronously on the daemon main loop. A 4-pane workspace at the per-pane cap could stall the loop for 1+ seconds per detach. Rapid attach/detach (10 cycles in 1 s) hammered disk every cycle. * New `src/daemon/snapshot_worker.rs` (~290 LoC): - `SnapshotWorker` thread named `ezpn-snapshot`. - Bounded `mpsc::sync_channel(4)`; `try_send` returning `Full` is a deliberate drop (debounce coalesces on the next idle). - 150 ms debounce window: a freshly arrived `Auto` job replaces any pending one; the worker only writes when the window elapses. - User-initiated `Save` jobs bypass the debounce and signal completion via an ack channel (30 s timeout). - Atomic write: `path.with_file_name("{name}.tmp.{pid}")` + `rename`. - `Drop` impl drains any pending `Auto` and joins the thread within a 5 s deadline; on timeout the handle is `mem::forget`ed and a single warn line is emitted. * `src/daemon/event_loop.rs`: - `SnapshotWorker::spawn()` near other init. - Three `workspace::auto_save(...)` sites → `submit(SnapshotJob::Auto)`. - IPC `Save` site → `submit(SnapshotJob::UserSave)` + bounded ack `recv_timeout`; saturation / timeout return structured errors via the existing `IpcResponse::error(...)` path. 3 new unit tests (atomic user save with no `.tmp.*` leftovers, shutdown drains pending Auto, debounce coalesces 5 rapid Auto jobs into one final file). All 180 tests pass; clippy clean. Wire protocol unchanged (`PROTOCOL_VERSION = 1`). On-disk snapshot schema unchanged (`v3`). No new external deps. Per SPEC docs/spec/v0.10.0/04-snapshot-pipeline-async.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #36. Fourth v0.10.0 stability fix.
Snapshot writes (and gzip+bincode for
persist_scrollback = true) move off the daemon main loop into a dedicatedezpn-snapshotworker thread. Per SPEC 04.Changes
A. New worker module —
src/daemon/snapshot_worker.rs(~290 LoC)SnapshotWorkerthread namedezpn-snapshot.mpsc::sync_channel(4);try_sendreturningFullis a deliberate drop (debounce coalesces on the next idle).Autojob replaces any pending one; the worker only writes when the window elapses.Savejobs bypass the debounce and signal completion via an ack channel (30 s timeout).path.with_file_name("{name}.tmp.{pid}")+rename.Dropimpl drains any pendingAutoand joins the thread within a 5 s deadline; on timeout the handle ismem::forgeted and a single warn line is emitted.B. Wiring —
src/daemon/event_loop.rsSnapshotWorker::spawn()near other init.workspace::auto_save(...)sites →submit(SnapshotJob::Auto).Savesite →submit(SnapshotJob::UserSave)+ bounded ackrecv_timeout; saturation/timeout return structured errors viaIpcResponse::error(...).Acceptance criteria (from #36)
SnapshotWorkerspawned at daemon bring-up; one named threadezpn-snapshot.mpsc::sync_channel(4)andtry_send.Autojobs.Savejobs bypass debounce and surface result via an ack channel.run()return the worker drains pending captures within a 5 s deadline (viaDrop).cargo testgreen.Test plan
cargo buildcleancargo clippy --all-targets -- -D warningscleancargo test— 180 / 180 passing (3 new unit tests):worker_writes_user_save_atomically(no.tmp.*leftovers)worker_shutdown_drains_pending_autoworker_debounces_rapid_auto_jobsWire protocol & schema
Wire protocol unchanged. On-disk snapshot schema unchanged (
v3). No new external deps.Files
+349 / −5 across 4 files. New:
src/daemon/snapshot_worker.rs(~290 LoC).🤖 Generated with Claude Code