A document over 50 KB can be left permanently on its preview slice, and every save is then refused
What was seen
Opening samples/stress-test.md (121,794 bytes) from the command line, on
macOS, in a dev build of current master:
- the editor pane is sometimes blank, and opening the same file again by hand shows it normally;
- once in that state, editing raises "Auto-save failed — unsaved changes still in memory" on every auto-save.
Both are intermittent. The file only recently crossed the threshold below — it was grown for #148's task-list fixtures — which is likely why this has not been reported before.
What the code says
A document larger than 50 KB is loaded in two stages
(documentSession.svelte.ts):
// :471 — preview mode reads only the head of the file
[, content, isFull, lossy, encoding] = await invoke('open_markdown_preview', { path, maxBytes: 50000 });
The tab is flagged truncated, and a second read fetches the whole file in the background. That second stage is allowed to land only if nothing about the tab has changed meanwhile:
// :489
const canApplyFullLoad = () => {
const targetTab = tabManager.tabs.find((item) => item.id === activeId);
return targetTab?.path === filePath
&& loadRevisionByTab.get(activeId) === fullLoadRevision
&& !targetTab.isDirty
&& targetTab.isEditing === initialIsEditing
&& targetTab.isSplit === initialIsSplit;
};
Both bail-outs (:500, :503) call updateLoading(activeId, false) and
return. Nothing retries and nothing records that the buffer is still a
slice. The tab keeps isTruncated, and from then on every write is refused:
// :560 and :630
options.onError('Refusing to save a partially loaded document', new Error(tab.path));
which is the "auto-save failed" toast, reported once per attempt.
toggleEdit re-reads the whole file when entering the editor — and only when
the tab is clean, because reading from disk over unsaved edits would lose them.
So a tab that is dirty when it enters the editor keeps the slice.
Why it matters more than the toast suggests
The refusal is the only thing standing between this state and a truncated
write. A reader in this state is editing what they believe is their document
and is in fact its first 50 KB, with no indication anywhere in the UI: the tab
looks normal, the text is real, and the failure is reported as a save problem
rather than a load problem. toast.partialDocument ("Cannot edit yet — this
file is not fully loaded") exists and is not what gets shown here.
What is not established
The exact sequence that abandons the second stage. isEditing, isSplit,
isDirty and the load revision are each capable of it, and the reporter's
recollection is that the window was in split view — but a tab opened from the
command line is created with isSplit: false (tabs.svelte.ts:457) while a
restored one carries saved.isSplit (:320), and I could not get from that to
a reproduction I trust. Filing on the symptom and the mechanism rather than
guessing at the trigger.
A fix probably wants to be structural rather than a fourth condition in that
guard — either retry the full load when the guard rejects it, or refuse to
enter an editable mode while the buffer is a slice, which is what
toast.partialDocument already says.
Device
- macOS, dev build of
masterat d67e850 - Reproduced on a 121 KB document; not attempted below the 50 KB threshold
Two PRs out of this, and neither is confirmed to be what was actually hit. Worth stating plainly before either lands.
#552 fixes a real race. The startup path delivers one filename on two channels that nothing dedupes, so two loads run on one tab, and an older preview read landing last overwrites the winner's complete buffer with its 50KB slice and re-raises isTruncated — after which its own second stage is correctly refused by the revision it just lost, nothing retries, and every save is refused from then on. That is the mechanism behind the second half of what was reported.
#553 is not a fix for the race; it is about what happens to a reader already in that state. Save As was refused too, so a buffer holding unsaved edits had no exit at all. A copy is a new file at a path the user chose and destroys nothing, so it is now allowed, completed first where possible, and the reader is told when it is short.
What is not established. The race was never reproduced in a packaged build — it needs the first load's preview read to land more than ~100ms after the second load's full read has already been applied, and once the file is in the page cache both reads are fast and the gap closes. Repeated launches by hand did not produce it. The evidence is a harness that reproduces the exact end state and interleaving deterministically.
And the blank pane is a separate cause, untouched by both. A tab also carries isTruncated with an empty buffer when session restore defers or fails a read (windowSession.svelte.ts:255, :283). Blank, flagged, unsaveable — same symptoms, different origin, cleared by reopening the file. It deserves its own issue.
So the honest summary: the defect surface here is smaller than it was, and the original incident remains unexplained and currently not reproducible. One useful side effect is that the symptom space is now partitioned — a tab that looks normal but will not save can no longer be this race, which points any future report at the restore path instead.
Two other things noticed while working, each its own issue rather than bundled here:
- The 50KB threshold has no recorded measurement. It arrives in
31adc64with a one-line message and no numbers. Readingsamples/stress-test.mdwhole is 0.032ms against 0.022ms for its first 50KB, and the two-stage path then reads it whole anyway and renders twice. The mechanism earns its keep on multi-MB documents, roughly two orders of magnitude above where it engages; the render cost is the part still unmeasured. For comparison, VS Code declines to display a file past its limit and offers a Configure Limit button, and Emacs prompts atlarge-file-warning-thresholdand offersfind-file-literally. Both degrade capability; neither degrades fidelity by showing less of the document. The preview slice does, which is safe only under the invariant that a partial buffer is never writable — the invariant this race broke. - The auto-save timer stacks "auto-save failed" behind the refusal. It already suppresses that for the lossy refusal, on the grounds that a refusal has explained itself; the same is true here, but the predicate it uses is also read by the eligibility gate with a different meaning.
根因确认
启动时同一个文件走了两条通道,没有任何去重:
| 通道 | 位置 | 触发方式 |
|---|---|---|
setup() emit |
lib.rs |
argv[1] → file-path 事件 |
send_markdown_path |
window_runtime.rs |
再次读 std::env::args() |
前端同时监听事件(MarkdownViewer.svelte)和排空 stash,两次 load 跑在同一个 tab 上。哪个先到取决于竞态。
为什么 dev build 下容易触发:npm run tauri dev 下 Vite 服务几千个模块 + Rust debug build 慢,预览读和完整读之间的时间窗口被显著拉大。生产构建下页面缓存让两次读都很快,窗口收窄到几乎不可复现。
两条修复的评估
PR #552 — 给第一阶段加 revision 守卫。当前只有第二阶段检查 revision,但第一阶段(预览片到达时)已经写入 buffer 了。修复后任何阶段的写入都先检查 revision,过期的直接跳过。这是根因修复。
PR #553 — 独立的逃生舱。当前状态下 Save As 也被拒绝,用户打了字没有出口。Save As 写新文件不破坏原文件,应该永远允许。完成后清掉 isTruncated。这是防御性修复,即使 #552 合了也值得有。
两者不冲突,可以独立合并。