Skip to content

fix(workspace): cap live Monaco models to stop the listener leak - #75

Closed
posthog[bot] wants to merge 3 commits into
mainfrom
posthog/cap-live-models-listener-leak
Closed

posthog[bot] wants to merge 3 commits into
mainfrom
posthog/cap-live-models-listener-leak

Conversation

@posthog

@posthog posthog Bot commented Aug 11, 2026

Copy link
Copy Markdown

Problem

  • Users editing large packages in the web editor trip Monaco's leak monitor: one shared emitter passes 200 listeners and Monaco throws its diagnostic Error. Reported across two browsers (Chromium and Firefox) and two deploys.
  • Root cause: MonacoWorkspaceModelManager keeps one live TextModel per workspace file. Each model adds a listener, so a workspace with a few hundred files reaches the threshold on its own. Model count grows with the file count, so this is a memory leak, not only a warning.
  • The disposal loop only disposed models the manager still tracked in its own Map. getModel fell back to Monaco's global registry, so any model that fell out of the map stayed alive with its listener attached.

Changes

  • Cap the live model count (DEFAULT_MAX_LIVE_MODELS = 100). The manager keeps the active file plus a recently-used set; the least-recently-used models past the cap are disposed. syncFiles now takes the active file and never evicts it.
  • Recreate cold files on demand. getModel rebuilds an evicted file's model from its last known content, so file open, navigation, and workspace search still resolve every file — search results and cross-file replace are unchanged.
  • Reconcile disposal with Monaco's global registry. Every dispose path now also disposes the registry twin for the URI, so a model that fell out of the map cannot linger.
  • The TypeScript worker prep now syncs only code files that still have a live model.

Trade-off

  • The TypeScript language service resolves cross-file imports from the hot set. For a package larger than the cap, diagnostics on the cold tail degrade until those files are opened. The active file and its recently-visited neighbours stay hot.

Verification

  • bun run typecheck passes.
  • bun test — 60 pass, 0 fail.
  • biome format clean.

Reported from tscircuit.com error tracking. A pin bump in tscircuit.com follows this merge.

posthog Bot added 2 commits August 11, 2026 20:34
Keep the active file plus a bounded, recently-used set of models and recreate
cold files on demand, so the workspace model count stays under Monaco's
200-listener leak monitor. Reconcile disposal against Monaco's global model
registry so a model that falls out of the manager's map cannot linger with its
listener attached.

Generated-By: PostHog Desktop
Task-Id: d961cb21-a653-45b3-8bf0-a4048d12da3c
Generated-By: PostHog Desktop
Task-Id: d961cb21-a653-45b3-8bf0-a4048d12da3c
@vercel

vercel Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
monaco-code-editor Ready Ready Preview Aug 11, 2026 8:58pm

Request Review

posthog Bot added a commit to tscircuit/tscircuit.com that referenced this pull request Aug 11, 2026
Bump @tscircuit/monaco-code-editor to the build that caps the number of live
Monaco text models, so large packages in the web editor no longer create one
model per file and cross Monaco's 200-listener leak monitor.

Pairs with tscircuit/monaco-code-editor#75.

Generated-By: PostHog Desktop
Task-Id: d961cb21-a653-45b3-8bf0-a4048d12da3c
The committed src/styles.compiled.css was stale relative to `bun run build:css`,
which failed the CI stylesheet check. Regenerate it. The drift predates this
branch and is unrelated to the model-manager change.

Generated-By: PostHog Desktop
Task-Id: d961cb21-a653-45b3-8bf0-a4048d12da3c
@posthog

posthog Bot commented Aug 16, 2026

Copy link
Copy Markdown
Author

🔕 Closing this PR because the linked PostHog report was suppressed.

If that wasn't intended, restore the report in PostHog and reopen this PR.

@posthog posthog Bot closed this Aug 16, 2026

This branch was successfully deployed

1 active deployment
Preview — 3173b7dc Deployed Aug 11, 2026 by vercel[bot]
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.

0 participants