Repository navigation
Description
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/markdown3.32.2 imports the module-levelmarked(dist/index.js:2).MarkdownManageruses it unless themarkedoption 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 holddocumentkeeps the oldDoc. The one whose options holdproviderkeeps 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
Markdownunconfigured (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)
- Give each pad editor its own
marked.@tiptap/markdownhas amarkedoption. Pass a newMarkedinstance per editor inTipTap.tsx, so tokenizers die with the editor. Check that Markdown paste, import and export still work. Check the chat composer and history editors too. CommandJump. Make sure its listener reads the current editor, not one captured at mount.- The hook. Destroy the old
Docafter the editor stops using it. Mind the teardown order: the editor binds toprovider.document. - Upstream. File a
@tiptap/markdownissue: the default sharedmarkedkeeps 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
markedpath 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
Measuredcomment exists, with the counts and the retainer paths. - A production-build before is posted.
- After the fix, the pass rule holds in
next devand in a production build, and the content-safety guard passes.
Activity
[-]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[/+]HMarzban commentedon Nov 13, 2026
Measured, 2026-11-13: #781 step 1 (live docs after pad switches).
Y.Docalive? Pass rule: at most 1 liveDocfrom the hook after 20 switches.3d90636a9.apps/webapp/src/hooks/useYdocAndProvider.tsis unchanged sincec9cd2c98a.next dev15.5.28 with Turbopack, so class names are kept. React StrictMode is on in development./new. Pad B was made in a tab that then closed. Pad A opened in the measured tab.HeapProfiler.collectGarbagetwice, thenHeapProfiler.takeHeapSnapshot.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).windowandperformance.timeOriginwere unchanged after the 20 switches.objectnamed exactlyDoc,HocuspocusProviderorIndexeddbPersistence. Retainer path: the shortest path from the GC root, over non-weak edges.Result (live objects, n = 1 run)
Doc(Y.Doc)HocuspocusProviderIndexeddbPersistenceAfter 20 switches, 20 more
Docs, 20 more providers and 20 more IndexedDB mirrors were live. All 21Docs carry a distinct UUIDguid, so each is a Y.Doc.Retainer paths (shortest strong path)
19 of the 21
Docs have the same path:@tiptap/markdown3.32.2 uses the module-levelmarkedunless amarkedoption is passed (dist/index.js:381). Each editor then callsmarked.use({ extensions: [...] })(dist/index.js:522). Read from code.markedobject, and nothing removes them. The tokenizer index grows by about 11 per editor (22 to 220 here).MarkdownManager, and that holds the editor's extensions. The extension whose options holddocumentkeeps the oldDoc.provider, keeps 20 of the 22 providers. Each provider'sconfiguration.tokenclosure keeps itsIndexeddbPersistence.The other 2
Docs:CommandJump's keydown listener →navigateclosure → editor →y-sync$1.HocuspocusProviderWebsockettimer →configuration.document.Which of these two is the live pad's
Docwas not checked. IfCommandJumpholds 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
abortlistener thatuseYdocAndProvideradds to the IndexedDB database (persistence._db.then(...)).dd2719445caf70537cc99d1a542fa9f9296f151251b016cd80d33e8d5dd7b846. After snapshot sha2566b309e37f4c000af5cc54577688df837ea4422238244bc4676df8ef37d219a7b.counts.json(counts and every path) sha256eb0fa080eb09b855d0b7265b2eeca1d047f841f93791a2df43a2f2666fb7156c. Harnessheap-781.tssha2562f0120de54936a8f3f8198114eaf08eeff96ba41bc59c90000dcea5869aa4e57. Every file is listed in aMANIFEST.sha256, itself sha256fa29e56faf6047b325eef81ec92fa5bdb8e647a986dc0da7b1a5613c227f763d.Doc. Both pads were fresh seed pads, so the leak per switch would grow with pad size.Doc. Only the shortest path is reported. The hook may also hold the old doc on a longer path.marked, so they may run on every later Markdown parse. Their behaviour and cost were not checked.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 sharedmarkedobject.Y.Doc.destroy()drops listeners, not the references frommarked.markedfirst, 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'smarkedoption.HMarzban commentedon Nov 13, 2026
Measured, 2026-11-13: #781 after the fix (live docs after pad switches).
Markedfix free the oldY.Docafter a pad switch? Pass rule: at most 1 liveDocafter 20 switches. Providers and IndexedDB mirrors must go back to the control count.TipTap.tsxnow passesMarkdown.configure({ marked: new Marked() }). So each editor gets its ownMarked, not the shared module-levelmarked.markedis now a direct webapp dependency, from the root catalog (^17.0.6).eeb1ab727, with the fix uncommitted in the working tree. Other uncommitted files: the prod workflow and 4 files inextension-hyperlink.TipTap.tsxplus themarkeddependency.next build(Next 15.5.28) throughbuild:ci,NODE_ENV=production, the same 6NEXT_PUBLIC_keys as before. Standalone server, with static files copied asDockerfile.bundoes.heap-781.ts, sha256b759845d…). The before dev row came from a different harness version (2f0120de…).HeapProfiler.collectGarbagetwice, then take a heap snapshot.window.next.router.push. Each switch waits for a new, editable editor on the right path. Then it waits 1 s.windowmarker andperformance.timeOriginwere unchanged.window.__prevEditorto detect a new editor. So the control path runs through it. The harness sets it tonullbefore the after snapshot.Doc,HocuspocusProviderandIndexeddbPersistencebecomeef,eCandb). On the dev snapshots it gives the same counts as the name counter.DevTools console → InjectedScriptis only the root of a path, not the retainer.Result: live objects, Doc / HocuspocusProvider / IndexeddbPersistence
markedfix only, n = 1)marked+CommandJumpfix, n = 1)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
Retainers after the fix (shortest paths, both runs)
markedInstanceor the sharedmarkedobject. In the before prod runs, 19 of the 20 extraDocs did.Doc1, the live pad: a DOM timer → a boundHocuspocusProviderWebsocket→configuration.document. This is the same path as the liveDocbefore the fix.Doc2: awindowevent listener closure → a stale editor →editorState→y-sync$1→doc.options.extensions→addOptions→provider.y-undo$1.undoManager→deleteFilter→ mirror.CommandJump.useEffect.onKeyDown→ its context → the stablenavigatecallback's render scope →editor. V8 shares one context per render scope, sonavigate(deps[router]) kept the first render'seditorfor the whole session.CommandJump.tsx:openJumpnow reads the editor from the store when it opens, so no render-scopeeditorexists. A dev re-run shows noCommandJumppath any more.WeakMapentry 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/editorserves for Cypress. That is the build shape of CI's standalone webapp test. It is not dev.NEXT_PUBLIC_E2Edoes not gate the pad provider or the persistence path (read from code).router.push. Fresh pads, two browser contexts, each with its own IndexedDB.x, digit in a random marker into×. Each×was found in the saved pad text, so no text was lost.x. Runs 3 and 4 used the new markers.extension-hyperlinkdist that does not hold the uncommittedautolink.tsedit. So these hyperlink results do not cover that edit.Raw data (local, not attached; sha256)
heap-781.ts(before prod and after prod)b759845dcf43c75cac8ce3de597108b9a81bfa8983251cc519363eee9a37f0d0counts.jsona6b5bc1c41d05b10ffa96b64413ddb50bd00e030a6e4b9f32d080b8e1214e749cf424f106599ae62fa0debcb5708247ba92a0b1d50cd99af5ab5daa1fb337457af3174d87fc43858f78689ef0fc536ebf52336f0a6df12791b134f5bf3d21fa9counts.json533e7c2c2570da6f27c4e760be008ae18830411c9861fa08e93a93b4903088039632fdef198825c23ca9d0a6ac3e3874168f9ff9186e4b9e9656eed8321ecffe28b0a2ed804c9817283433351f8e717f76c1db96c41036c4c04714b2e76ea0a3615d5fb4dc1802bf880fd538639cb62a8ebbe18ca4cacbb327cbdb1e220bf7121b236b1a664ddd693b317d6ef7a9a849aaa6ce6a59ad0d093fdf5778743248f6counts.jsonbdf97b5ee979e7d694a69ae30660258a693382ba37357e9aadbf42494b7327e8counts.jsonc9fb9a54d4919be1147c87e4f4c4447ff7eb9aca4243ca26402ed1ee77109457MANIFEST.sha25645505b1c8758d900d225aa51f0aafafffffacb77d5cef30803624f6e35d40239MANIFEST.sha25619f98c011732f6506e2a432c87180edbaf8280a24e9a033f1273f945d9824911Each 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
Doc. Both pads were fresh seed pads.Verdict
markedfix works. Extra objects after 20 switches fell from 20 to 1 for each class, in both runs. No retainer path uses the sharedmarkedobject any more.Doc, and providers and mirrors must be back at 1.CommandJumpretainer is fixed; at least one more remains (a hypermultimediaWeakMappath is the next shortest).fix(webapp): free a left pad by giving each editor its own marked
docs(agents): record the editor lifetime and bench method rules
docs(changelog): quote exact benchmark numbers with conditions
HMarzban commentedon Nov 16, 2026
Ruling (maintainer, 2026-11-16): close #781. #782 carries the last stale editor.
3b559da2d: 21/21/21 → 2/2/2 Docs, providers and mirrors after 20 switches (issuecomment-6860233240). It is live since the 2026-11-15 deploy (b7888dce1).MarkedinTipTap.tsxcan go.Release credit v2.3.8. Closing.