Skip to content

fix: resolve race condition in singleton highlighter (issue #1009)#1189

Open
maynkxx wants to merge 1 commit intoshikijs:mainfrom
maynkxx:fix/issue-1009-singleton-race-condition
Open

fix: resolve race condition in singleton highlighter (issue #1009)#1189
maynkxx wants to merge 1 commit intoshikijs:mainfrom
maynkxx:fix/issue-1009-singleton-race-condition

Conversation

@maynkxx
Copy link
Copy Markdown

@maynkxx maynkxx commented Dec 3, 2025

Description

This PR addresses a race condition encountered when using singleton highlighters in concurrent environments.

Previously, internal-sync.ts included an optimization that skipped synchronizing the registry if the requested theme matched the last used theme. While efficient in single-threaded contexts, this caused issues during concurrent requests. If multiple requests hit the highlighter simultaneously, the internal state could become stale, leading to the wrong theme being applied or tokenization errors.

I have removed this optimization in packages/core/src/constructors/internal-sync.ts. Now, the registry explicitly synchronizes the theme for every request. This ensures that the state is always consistent, preventing stale usage regardless of concurrency.

Linked Issues

Fixes #1009

Additional context

This change trades a small micro-optimization for significantly better stability in server-side or high-concurrency usage (e.g., when Shiki is used in an API route handling multiple requests at once).

)

Always synchronize the registry theme in internal-sync.ts to prevent stale state usage during concurrent requests in singleton highlighters.
@netlify
Copy link
Copy Markdown

netlify bot commented Dec 3, 2025

Deploy Preview for shiki-matsu ready!

Name Link
🔨 Latest commit 1dfc213
🔍 Latest deploy log https://app.netlify.com/projects/shiki-matsu/deploys/692fe7d548bf0c00085a9cdd
😎 Deploy Preview https://deploy-preview-1189--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
Copy link
Copy Markdown

netlify bot commented Dec 3, 2025

Deploy Preview for shiki-next ready!

Name Link
🔨 Latest commit 1dfc213
🔍 Latest deploy log https://app.netlify.com/projects/shiki-next/deploys/692fe7d5e91d2c00085c7dca
😎 Deploy Preview https://deploy-preview-1189--shiki-next.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.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.20%. Comparing base (61c87e8) to head (1dfc213).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1189      +/-   ##
==========================================
- Coverage   95.20%   95.20%   -0.01%     
==========================================
  Files          92       92              
  Lines        7922     7920       -2     
  Branches     1689     1688       -1     
==========================================
- Hits         7542     7540       -2     
  Misses        374      374              
  Partials        6        6              

☔ View full report in Codecov by Sentry.
📢 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.

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.

Highlighting is wrong when run in a Vercel React Server Component

1 participant