Skip to content

Every pad switch keeps the old pad's Y.Doc, provider and mirror alive #781

Closed

Description

@HMarzban

Confirmed on 2026-11-13 by the step 1 heap snapshot (local next dev). The first version of this issue blamed a missing ydoc.destroy() in useYdocAndProvider. That is not the main cause, and adding the call alone would free almost nothing.

Summary

Each pad switch keeps the old pad alive for the rest of the session: its Y.Doc, its HocuspocusProvider and its IndexedDB mirror. After 20 switches between two pads, 21 Docs, 22 providers and 22 mirrors were live. The rule is at most 1 Doc. The cost per switch grows with pad size, so a long session on large pads holds many whole documents.

Measured (step 1)

Class Control, 0 switches After 20 switches Change
Doc (Y.Doc) 1 21 +20
HocuspocusProvider 2 22 +20
IndexeddbPersistence 2 22 +20
Heap nodes 1,193,541 1,255,845 +62,304

Condition: local webapp, next dev 15.5.28 (Turbopack, React StrictMode on), Chrome for Testing 158, two fresh scratch pads, 20 client-side switches, GC forced before each snapshot, n = 1 run (each count is exact). The local hocuspocus processes ran another session's uncommitted code. The full result is in the Measured comment below.

Cause

Main retainer: the shared marked object (19 of 21 Docs). Shortest strong path:

module cache → marked exports → defaults.extensions.inline[n] → tokenizer closure
  → this: MarkdownManager → baseExtensions[45] → options.document → Doc
  • @tiptap/markdown 3.32.2 imports the module-level marked (dist/index.js:2). MarkdownManager uses it unless the marked option is set (dist/index.js:381).
  • Each editor registers its tokenizers with this.markedInstance.use({ extensions: [...] }) (dist/index.js:522). Nothing removes them when the editor is destroyed.
  • So every editor stays reachable from marked, with all its extensions. The extension whose options hold document keeps the old Doc. The one whose options hold provider keeps the provider, and the provider keeps its mirror.
  • The tokenizer list grew by about 11 per editor (22 to 220). Old tokenizers may also run on every later Markdown parse. Their cost and behaviour were not checked.
  • The pad loads Markdown unconfigured (apps/webapp/src/components/TipTap/TipTap.tsx:165).

Second retainer: CommandJump (1 Doc). Its keydown listener → navigate closure → an editor → y-sync$1. Whether that editor is stale or the live pad was not checked.

Third: a pending HocuspocusProviderWebsocket timer (1 Doc). It holds configuration.document.

The hook. useYdocAndProvider.ts:85 still replaces the doc without destroy(), and the cleanup (:291-308) destroys only the mirror and the provider. That is a real gap, but it is not on the shortest path for any of the 21 docs. The abort listener the hook adds to the mirror's database holds one extra mirror in the control (StrictMode double mount).

Fix (proposal, measure each step)

  1. Give each pad editor its own marked. @tiptap/markdown has a marked option. Pass a new Marked instance per editor in TipTap.tsx, so tokenizers die with the editor. Check that Markdown paste, import and export still work. Check the chat composer and history editors too.
  2. CommandJump. Make sure its listener reads the current editor, not one captured at mount.
  3. The hook. Destroy the old Doc after the editor stops using it. Mind the teardown order: the editor binds to provider.document.
  4. Upstream. File a @tiptap/markdown issue: the default shared marked keeps every destroyed editor alive. Do it after step 1 proves the cause.

Re-run step 1 after each fix. Only step 1's fix is expected to move the count a lot.

Benchmark

Field Value
Metric Live Doc, HocuspocusProvider and IndexeddbPersistence objects in a Chrome heap snapshot after 20 pad switches.
Before 21 / 22 / 22 (next dev, 2026-11-13). A production build: not measured yet. Agent in the repo reads it with build:ci and start:prod before the fix.
After not measured yet
Condition Local stack, desktop Chrome for Testing 158, two scratch pads the agent creates. Never /demo.
How to run Open pad A. Switch to pad B and back by client navigation, 20 switches. Force GC, take a heap snapshot, count by constructor name, and record the shortest retainer path.
Pass rule At most 1 live Doc, and providers and mirrors back to the control count.
Guard Content safety: every typed word survives a switch and a reload, in both pads. Markdown paste, import and export behave as before.
Owner Agent in the repo: the production before, the fix and the re-runs. Maintainer: the fix review.
Claim Internal: "A long session no longer keeps one whole document per pad switch: 21 live docs → 1 after 20 switches."

Not measured yet

  • A production build. The marked path does not depend on dev mode (read from code), but the root of the path differs.
  • Retained bytes per Doc. Both pads were small seed pads.
  • A signed-in user, the mobile layout and the chat composer.

Acceptance criteria

  • The step 1 Measured comment exists, with the counts and the retainer paths.
  • A production-build before is posted.
  • After the fix, the pass rule holds in next dev and in a production build, and the content-safety guard passes.

Activity

  1. added this to the v2.3.8 milestone on Nov 13, 2026
  2. added
    bug
    Editor
    Performance
    on Nov 13, 2026
  3. changed the title [-]Unconfirmed: the old Y.Doc is never destroyed on a pad switch[/-] [+]Every pad switch keeps the old pad's Y.Doc, provider and mirror alive[/+] on Nov 13, 2026
  4. HMarzban commented on Nov 13, 2026

    @HMarzban
    CollaboratorAuthor

    Measured, 2026-11-13: #781 step 1 (live docs after pad switches).

    • Question: does a pad switch leave the old Y.Doc alive? Pass rule: at most 1 live Doc from the hook after 20 switches.
    • Setup:
      • Working tree on 3d90636a9. apps/webapp/src/hooks/useYdocAndProvider.ts is unchanged since c9cd2c98a.
      • Local webapp under next dev 15.5.28 with Turbopack, so class names are kept. React StrictMode is on in development.
      • The local hocuspocus REST, WS and worker processes ran another session's uncommitted code.
      • Desktop headless Chrome for Testing 158.0.8127.18 (build 1254), through Playwright 1.64.1, viewport 1440 × 900. One Apple M5 Max laptop, Bun 1.4.4.
      • The laptop was not idle: another repository's dev server and Playwright run were active, load averages 4.5 to 5.3. Counts do not depend on load.
    • Method:
      • Two fresh anonymous scratch pads, A and B, from /new. Pad B was made in a tab that then closed. Pad A opened in the measured tab.
      • Control: wait 3 s on A, run HeapProfiler.collectGarbage twice, then HeapProfiler.takeHeapSnapshot.
      • 20 switches, B then A, through the app's Pages Router (window.next.router.push). Each switch waited for the new editor: right path, editable, seed blocks present, and a different editor object. Then it waited 1 s. A switch took 1,102 ms median (1,084–1,162).
      • No reload: a marker on window and performance.timeOrigin were unchanged after the 20 switches.
      • After: the same GC and snapshot, back on pad A.
      • Count: heap nodes of type object named exactly Doc, HocuspocusProvider or IndexeddbPersistence. Retainer path: the shortest path from the GC root, over non-weak edges.

    Result (live objects, n = 1 run)

    Class Control, 0 switches After 20 switches Change
    Doc (Y.Doc) 1 21 +20
    HocuspocusProvider 2 22 +20
    IndexeddbPersistence 2 22 +20
    Heap nodes 1,193,541 1,255,845 +62,304

    After 20 switches, 20 more Docs, 20 more providers and 20 more IndexedDB mirrors were live. All 21 Docs carry a distinct UUID guid, so each is a Y.Doc.

    Retainer paths (shortest strong path)

    19 of the 21 Docs have the same path:

    Window → (dev module cache) → marked/lib/marked.esm.js exports
      → Hooks → defaults.extensions.inline[n] → closure tokenizer
      → this: MarkdownManager → baseExtensions[45]: Extension
      → config.addOptions → options.document → Doc
    
    • @tiptap/markdown 3.32.2 uses the module-level marked unless a marked option is passed (dist/index.js:381). Each editor then calls marked.use({ extensions: [...] }) (dist/index.js:522). Read from code.
    • So every editor adds its tokenizers to the shared marked object, and nothing removes them. The tokenizer index grows by about 11 per editor (22 to 220 here).
    • Each tokenizer holds its MarkdownManager, and that holds the editor's extensions. The extension whose options hold document keeps the old Doc.
    • The extension at index 46, whose options hold provider, keeps 20 of the 22 providers. Each provider's configuration.token closure keeps its IndexeddbPersistence.

    The other 2 Docs:

    • One through CommandJump's keydown listener → navigate closure → editor → y-sync$1.
    • One through a pending HocuspocusProviderWebsocket timer → configuration.document.

    Which of these two is the live pad's Doc was not checked. If CommandJump holds a stale editor, that is a second retainer in the app.

    The control already shows one extra provider and one extra mirror. That fits a StrictMode double mount. The extra mirror is held by the abort listener that useYdocAndProvider adds to the IndexedDB database (persistence._db.then(...)).

    • Raw data: local, not attached. Control snapshot sha256 dd2719445caf70537cc99d1a542fa9f9296f151251b016cd80d33e8d5dd7b846. After snapshot sha256 6b309e37f4c000af5cc54577688df837ea4422238244bc4676df8ef37d219a7b. counts.json (counts and every path) sha256 eb0fa080eb09b855d0b7265b2eeca1d047f841f93791a2df43a2f2666fb7156c. Harness heap-781.ts sha256 2f0120de54936a8f3f8198114eaf08eeff96ba41bc59c90000dcea5869aa4e57. Every file is listed in a MANIFEST.sha256, itself sha256 fa29e56faf6047b325eef81ec92fa5bdb8e647a986dc0da7b1a5613c227f763d.
    • Not measured:
      • A production build. The marked path does not depend on dev mode, read from code. The root of the path differs in production.
      • Retained bytes per Doc. Both pads were fresh seed pads, so the leak per switch would grow with pad size.
      • Every retainer of each Doc. Only the shortest path is reported. The hook may also hold the old doc on a longer path.
      • A signed-in user, the mobile layout, and the chat composer editor.
      • A repeat run. n = 1, but each count is exact.
      • The content-safety guard from the issue.
      • Tokenizers from destroyed editors stay in the shared marked, so they may run on every later Markdown parse. Their behaviour and cost were not checked.
    • Verdict: leak confirmed. 21 live Docs after 20 switches, against a rule of at most 1. The main retainer is not the hook. It is @tiptap/markdown's use of the shared marked object.
      • Step 2 as written (destroy the old doc in the hook) would not free these objects. Y.Doc.destroy() drops listeners, not the references from marked.
      • Open question for the maintainer. Step 2 scope: stop each editor from registering on the shared marked first, then re-run step 1? Recommended: yes, because the hook fix alone cannot pass the rule. One option, read from code and not tried: pass a private instance through the Markdown extension's marked option.
      • The scratch pads were not deleted. Anonymous pads have no owner delete path in the app.
  5. HMarzban commented on Nov 13, 2026

    @HMarzban
    CollaboratorAuthor

    Measured, 2026-11-13: #781 after the fix (live docs after pad switches).

    Since this run, the fix is commit 3b559da2d (not pushed yet). It also holds the CommandJump change, which only the second dev run below covers; the production rows predate it.

    • Question: does the per-editor Marked fix free the old Y.Doc after a pad switch? Pass rule: at most 1 live Doc after 20 switches. Providers and IndexedDB mirrors must go back to the control count.
    • Fix under test: TipTap.tsx now passes Markdown.configure({ marked: new Marked() }). So each editor gets its own Marked, not the shared module-level marked. marked is now a direct webapp dependency, from the root catalog (^17.0.6).
    • Setup:
      • Build commit eeb1ab727, with the fix uncommitted in the working tree. Other uncommitted files: the prod workflow and 4 files in extension-hyperlink.
      • The webapp builds from the extension dist builds. All 6 extension dist sha256 values match the before build. So the only change in the build is TipTap.tsx plus the marked dependency.
      • Production build: next build (Next 15.5.28) through build:ci, NODE_ENV=production, the same 6 NEXT_PUBLIC_ keys as before. Standalone server, with static files copied as Dockerfile.bun does.
      • Local hocuspocus REST, WS and worker processes in dev mode.
      • Headless Chrome for Testing 158.0.8127.18 through Playwright, viewport 1440 × 900. One Apple M5 Max laptop (Mac17,6), Bun 1.4.4.
      • Load averages during the after runs were 3.15 to 3.46. Counts do not depend on load.
    • Method:
      • The same harness as the before prod run (heap-781.ts, sha256 b759845d…). The before dev row came from a different harness version (2f0120de…).
      • Two fresh anonymous scratch pads, A and B. Control: wait on A, run HeapProfiler.collectGarbage twice, then take a heap snapshot.
      • 20 switches, B then A, through window.next.router.push. Each switch waits for a new, editable editor on the right path. Then it waits 1 s.
      • No reload in either run: a window marker and performance.timeOrigin were unchanged.
      • The harness keeps the previous editor on window.__prevEditor to detect a new editor. So the control path runs through it. The harness sets it to null before the after snapshot.
      • Count: a shape counter, because production minifies the class names (Doc, HocuspocusProvider and IndexeddbPersistence become ef, eC and b). On the dev snapshots it gives the same counts as the name counter.
      • Retainer: the shortest strong path from a GC root. DevTools console → InjectedScript is only the root of a path, not the retainer.

    Result: live objects, Doc / HocuspocusProvider / IndexeddbPersistence

    Run Control (0 switches) After 20 switches
    Before, dev (earlier comment, n = 1) 1 / 2 / 2 21 / 22 / 22
    Before, prod, run 1 1 / 1 / 1 21 / 21 / 21
    Before, prod, run 2 1 / 1 / 1 21 / 21 / 21
    After, prod, run 1 1 / 1 / 1 2 / 2 / 2
    After, prod, run 2 1 / 1 / 1 2 / 2 / 2
    After, dev (marked fix only, n = 1) 1 / 2 / 2 2 / 2 / 2
    After, dev (marked + CommandJump fix, n = 1) 1 / 2 / 2 2 / 2 / 2

    The dev rows show one more provider and mirror, because React StrictMode mounts twice in development. Production has no StrictMode.

    Heap nodes and switch time

    Run Control nodes After nodes Median switch (min–max)
    Before, prod, run 1 637,104 696,713 1,064 ms (1,052–1,085)
    Before, prod, run 2 637,000 696,495 1,066 ms (1,046–1,073)
    After, prod, run 1 636,947 667,866 1,070 ms (1,046–1,087)
    After, prod, run 2 637,090 667,864 1,065 ms (1,044–1,076)

    Retainers after the fix (shortest paths, both runs)

    • No path in either run goes through markedInstance or the shared marked object. In the before prod runs, 19 of the 20 extra Docs did.
    • Doc 1, the live pad: a DOM timer → a bound HocuspocusProviderWebsocket → configuration.document. This is the same path as the live Doc before the fix.
    • Doc 2: a window event listener closure → a stale editor → editorState → y-sync$1 → doc.
    • The extra provider: the same stale editor → options.extensions → addOptions → provider.
    • The extra mirror: the same stale editor → y-undo$1.undoManager → deleteFilter → mirror.
    • The after dev run confirms the name: CommandJump.useEffect.onKeyDown → its context → the stable navigate callback's render scope → editor. V8 shares one context per render scope, so navigate (deps [router]) kept the first render's editor for the whole session.
    • Fixed in CommandJump.tsx: openJump now reads the editor from the store when it opens, so no render-scope editor exists. A dev re-run shows no CommandJump path any more.
    • The count did not move: the same stale editor has another retainer. The new shortest path is a hypermultimedia WeakMap entry keyed by that editor, whose value (onPointerOutside, media-resize-controls.ts) closes over the editor. An entry whose value points at its own key cannot keep the key alive alone, so at least one more retainer exists. It stays bounded at one editor; it does not grow with switches.

    Checks on a second build

    This build is the same production build with NEXT_PUBLIC_E2E=true, so /editor serves for Cypress. That is the build shape of CI's standalone webapp test. It is not dev. NEXT_PUBLIC_E2E does not gate the pad provider or the persistence path (read from code).

    • Cypress: 14 specs, 130 / 130 tests passed.
      • Markdown: export 12/12, media 9/9, paste 14/14, round trip 15/15. Document: paste title 3/3, hyperlink icon prefill 1/1.
      • Hyperlink: autolink 10/10, create 12/12, edge cases 10/10, internal chip 5/5, markdown 7/7, paste 6/6, picker 10/10, preview edit 16/16.
      • No import spec exists. The round-trip spec covers parse.
    • Content safety: items 1–4 from Commit one content-safety checklist and one Measured format to the release-product skill #762, plus 5 pad switches through router.push. Fresh pads, two browser contexts, each with its own IndexedDB.
      • Runs 3 and 4 passed all 5 items.
      • Runs 1 and 2 had 3 failures, and all 3 came from the harness. The Typography rule turned a digit, x, digit in a random marker into ×. Each × was found in the saved pad text, so no text was lost.
      • The markers now use letters only, with no x. Runs 3 and 4 used the new markers.
    • The webapp reads an extension-hyperlink dist that does not hold the uncommitted autolink.ts edit. So these hyperlink results do not cover that edit.

    Raw data (local, not attached; sha256)

    File sha256
    Harness heap-781.ts (before prod and after prod) b759845dcf43c75cac8ce3de597108b9a81bfa8983251cc519363eee9a37f0d0
    After, run 1 counts.json a6b5bc1c41d05b10ffa96b64413ddb50bd00e030a6e4b9f32d080b8e1214e749
    After, run 1 control snapshot cf424f106599ae62fa0debcb5708247ba92a0b1d50cd99af5ab5daa1fb337457
    After, run 1 snapshot after 20 switches af3174d87fc43858f78689ef0fc536ebf52336f0a6df12791b134f5bf3d21fa9
    After, run 2 counts.json 533e7c2c2570da6f27c4e760be008ae18830411c9861fa08e93a93b490308803
    After, run 2 control snapshot 9632fdef198825c23ca9d0a6ac3e3874168f9ff9186e4b9e9656eed8321ecffe
    After, run 2 snapshot after 20 switches 28b0a2ed804c9817283433351f8e717f76c1db96c41036c4c04714b2e76ea0a3
    After retainer paths 615d5fb4dc1802bf880fd538639cb62a8ebbe18ca4cacbb327cbdb1e220bf712
    Content-safety harness, letters-only version (runs 3 and 4; runs 1 and 2 used an earlier file) 1b236b1a664ddd693b317d6ef7a9a849aaa6ce6a59ad0d093fdf5778743248f6
    Before, run 1 counts.json bdf97b5ee979e7d694a69ae30660258a693382ba37357e9aadbf42494b7327e8
    Before, run 2 counts.json c9fb9a54d4919be1147c87e4f4c4447ff7eb9aca4243ca26402ed1ee77109457
    Before MANIFEST.sha256 45505b1c8758d900d225aa51f0aafafffffacb77d5cef30803624f6e35d40239
    After MANIFEST.sha256 19f98c011732f6506e2a432c87180edbaf8280a24e9a033f1273f945d9824911

    Each manifest lists every file of its run, with the snapshots, logs and build notes. Every listed hash was checked again before this comment.

    Not measured

    • Every retainer of the last stale editor. Only shortest paths are reported.
    • Retained bytes per Doc. Both pads were fresh seed pads.
    • Every retainer of each object. Only the shortest path is reported.
    • A signed-in user, the mobile layout, and the chat composer editor.
    • Counts after fewer or more than 20 switches. So it is not shown whether the 1 extra is always the last pad's editor.

    Verdict

    • The marked fix works. Extra objects after 20 switches fell from 20 to 1 for each class, in both runs. No retainer path uses the shared marked object any more.
    • The pass rule still fails. After 20 switches, prod shows 2 / 2 / 2. The rule allows at most 1 Doc, and providers and mirrors must be back at 1.
    • All 3 extras hang from one stale editor. It is bounded: it does not grow with more switches. The CommandJump retainer is fixed; at least one more remains (a hypermultimedia WeakMap path is the next shortest).
    • Next step: a follow-up issue for the last stale editor, so this fix can ship now. After 20 switches, 19 of the 20 pads left behind are now freed.
    • The scratch pads were not deleted. Anonymous pads have no owner delete path in the app.
  6. HMarzban commented on Nov 16, 2026

    @HMarzban
    CollaboratorAuthor

    Ruling (maintainer, 2026-11-16): close #781. #782 carries the last stale editor.

    Release credit v2.3.8. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    EditorPerformancebug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions