Skip to content

fix(primitive): dispose wasm memory for lazily embedded langs - #1322

Merged
antfu merged 1 commit into
shikijs:mainfrom
rgodha24:fix/dispose-lazily-embedded-grammars
Oct 1, 2026
Merged

antfu merged 1 commit into
shikijs:mainfrom
rgodha24:fix/dispose-lazily-embedded-grammars

Conversation

@rgodha24

@rgodha24 rgodha24 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor
  • <- Keep this line and put an x between the brackts.

Description

when you load a language that an already loaded grammar has in embeddedLangsLazy (eg loading haskell while markdown is loaded), Registry.loadLanguage drops the embedding grammar and rebuilds it, but never disposes the old one. with the oniguruma engine the old grammar's scanners just sit in wasm memory forever. they're also out of _grammars at that point, so highlighter.dispose() doesnt free them either

this causes issues on long lived servers running shiki, like mintlify's mdx compilation pipeline.

fix is to dispose the old grammar before dropping it. the rebuilt grammar is the same one shiki would use anyway so highlighting doesnt change

Linked Issues

fixes #1323

Additional context

added a test in packages/primitive/test/registry.test.ts that fails without the fix (old grammar's dispose never gets called). repro script + memory numbers are in #1323

workaround: mintlify/mdx#66

@netlify

netlify Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for shiki-matsu ready!

Name Link
🔨 Latest commit 9312bb8
🔍 Latest deploy log https://app.netlify.com/projects/shiki-matsu/deploys/6abd343867d25a00085ff14b
😎 Deploy Preview https://deploy-preview-1322--shiki-matsu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for shiki-next ready!

Name Link
🔨 Latest commit 9312bb8
🔍 Latest deploy log https://app.netlify.com/projects/shiki-next/deploys/6abd343842bf580008d24e2b
😎 Deploy Preview https://deploy-preview-1322--shiki-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Sep 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.82%. Comparing base (ac8e1c4) to head (9312bb8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1322   +/-   ##
=======================================
  Coverage   89.82%   89.82%           
=======================================
  Files          81       81           
  Lines        3587     3588    +1     
  Branches     1028     1028           
=======================================
+ Hits         3222     3223    +1     
  Misses        328      328           
  Partials       37       37           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rgodha24 rgodha24 changed the title fix(primitive): dispose grammars reloaded for lazily embedded langs fix(primitive): dispose wasm memory for lazily embedded langs Sep 30, 2026
@rgodha24
rgodha24 marked this pull request as ready for review September 30, 2026 16:16
@antfu
antfu merged commit b542364 into shikijs:main Oct 1, 2026
14 of 15 checks passed
antfu pushed a commit to SHUBHI-KUMARI/shiki that referenced this pull request Oct 1, 2026
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.

Grammars rebuilt for embeddedLangsLazy are never disposed (wasm memory leak)

2 participants