Skip to content

fix(muya): drop pending op batch on setContent to prevent cross-document corruption (#2938) - #4658

Merged
Jocs merged 4 commits into
developfrom
fix/issue-2938-setcontent-clear-opcache
Jun 23, 2026
Merged

Jocs merged 4 commits into
developfrom
fix/issue-2938-setcontent-clear-opcache

Conversation

@Jocs

@Jocs Jocs commented Jun 23, 2026

Copy link
Copy Markdown
Member

Fixes #2938

Problem

Switching files could leave the switched-to file unsaveable / corrupted. If you typed in tab A and clicked tab B within the same animation frame, tab A's not-yet-flushed edit was applied to tab B's document.

Root cause

packages/muya/src/state/index.ts — operations are deferred: editOperation/insertOperation/… push to _operationCache and _emitStateChange() schedules a requestAnimationFrame flush. setContent (the tab-switch path) replaced _state but never cleared _operationCache or cancelled the scheduled flush. When the rAF fired, it composed and applied the previous document's op to the new state — corrupting it, or throwing and leaving _isGoing = true (which permanently froze all later deferred ops).

Fix

Track the scheduled flush's rAF handle. On setContent, cancel a pending flush, clear _operationCache, and reset _isGoing before swapping the state.

Verification

  • New setContentClearsOpCache.spec.ts: a pending insert against doc A does not appear in doc B after setContent (RED→GREEN: was ['B1','STALE','B2']); a fresh edit after the switch still flushes (not frozen).
  • Full muya unit suite: 1236 tests pass.
  • CommonMark + GFM conformance: 1347 pass (unchanged).
  • lint:types + ESLint clean.

🤖 Generated with Claude Code

Jocs and others added 4 commits June 23, 2026 14:48
…ocument corruption (#2938)

`JSONState.setContent` replaced `_state` but left any deferred operation batch
in `_operationCache` (with its scheduled requestAnimationFrame). Switching
files within the same frame as a pending edit then flushed the outgoing
document's op against the new document's state — corrupting it, or throwing
and leaving `_isGoing` stuck so all further edits froze. Either way the
switched-to file could no longer be saved correctly.

Track the scheduled flush's rAF handle; on `setContent` cancel it, clear
`_operationCache`, and reset `_isGoing` before swapping the state.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`_isGoing` (boolean "a flush is scheduled") and `_rafId` (the rAF handle)
were always set and cleared together, so they tracked the same state
redundantly. Drop `_isGoing` and use `_rafId !== null` as the batching
guard directly. No behavior change — the existing setContent/op-batching
tests stay green.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The op cache flushes on a requestAnimationFrame, so a same-frame document
swap (a tab switch calls setContent right after the last keystroke) drops
the pending edit. Add a public flush() that runs the queued batch now and
emits json-change, so a caller can make the outgoing edit durable before
swapping. Extracted the rAF body into _flushOperationCache, shared by the
frame callback and flush().

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
On tab switch the store sets currentFile then synchronously reloads the
engine via setContent, so the previous tab's last same-frame keystroke
(still queued in the engine) was either lost or applied to the incoming
document. Flush the engine while currentFile still points at the outgoing
tab (via a new flush-active-editor bus event), so the edit is applied and
persisted under the right tab before the swap.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Build artifacts for PR #4658:

Run: https://github.com/marktext/marktext/actions/runs/28019618842

Artifact Size Link
marktext-windows-arm64 275.8 MB Download
marktext-macos-x64 293.1 MB Download
marktext-linux 627.5 MB Download
marktext-windows-x64 283.9 MB Download
marktext-macos-arm64 282.8 MB Download

@Jocs
Jocs merged commit 0d7bf91 into develop Jun 23, 2026
16 checks passed
@Jocs
Jocs deleted the fix/issue-2938-setcontent-clear-opcache branch June 23, 2026 10:40
Jocs pushed a commit that referenced this pull request Sep 11, 2026
…5103)

An edit made in the same frame as the WYSIWYG -> source switch (e.g. a
task-checkbox click) was missing from the CodeMirror snapshot, and the
swap back out of source mode cancels the engine's scheduled op flush
(#4658), silently reverting the edit. Flush the rAF batch in the sync
sourceCode watcher so the tab holds fresh markdown before sourceCode.vue
mounts — the same guard saving (#3803/#4859) and tab switching (#2938)
already use.

Fixes #5102

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't save when i shift the file

1 participant