Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"TypeError: onigBinding.UTF8ToString is not a function" after some time / parallel requests in Next.js #693

Closed
3 of 5 tasks
hlubek opened this issue Jun 3, 2024 · 10 comments

Comments

@hlubek
Copy link

hlubek commented Jun 3, 2024

Validations

Describe the bug

We experienced crashes in production when using shikijs in a Next.js 14 application in React Server Components.

Initially the rendering works, but after a few (parallel?) requests we have the following stacktrace and errors are returned in the rendering:

TypeError: onigBinding.UTF8ToString is not a function
    at throwLastOnigError (file:///app/node_modules/@shikijs/core/dist/index.mjs:4677:38)
    at new OnigScanner (file:///app/node_modules/@shikijs/core/dist/index.mjs:4883:13)
    at createOnigScanner (file:///app/node_modules/@shikijs/core/dist/index.mjs:5024:12)
    at Object.createOnigScanner (file:///app/node_modules/@shikijs/core/dist/index.mjs:5358:20)
    at Grammar.createOnigScanner (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:2275:30)
    at new CompiledRule (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:1270:32)
    at RegExpSourceList._resolveAnchors (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:1260:16)
    at RegExpSourceList.compileAG (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:1237:56)
    at IncludeOnlyRule.compileAG (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:728:57)
    at prepareRuleSearch (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:2107:30)
TypeError: onigBinding.UTF8ToString is not a function
    at throwLastOnigError (file:///app/node_modules/@shikijs/core/dist/index.mjs:4677:38)
    at new OnigScanner (file:///app/node_modules/@shikijs/core/dist/index.mjs:4883:13)
    at createOnigScanner (file:///app/node_modules/@shikijs/core/dist/index.mjs:5024:12)
    at Object.createOnigScanner (file:///app/node_modules/@shikijs/core/dist/index.mjs:5358:20)
    at Grammar.createOnigScanner (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:2275:30)
    at new CompiledRule (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:1270:32)
    at RegExpSourceList.compile (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:1219:28)
    at RegExpSourceList.compileAG (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:1225:25)
    at IncludeOnlyRule.compileAG (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:728:57)
    at prepareRuleSearch (file:///app/node_modules/@shikijs/core/dist/textmate.mjs:2107:30)
[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance
  digest: '25876654'
}
[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance
  digest: '3589831450'
}

This is our code:

export default async function Code({ title, code, language }: CodeProps) {
  const html = await codeToHtml({
    code,
    language,
  });

  return <div
        dangerouslySetInnerHTML={{ __html: html }}
      />
}

Where our syntax definition comes from https://github.com/networkteam/vscode-neos-fusion/blob/master/syntaxes/fusion.tmLanguage.json .

import { bundledLanguages, getHighlighter } from 'shiki/bundle/web';
import fusion from '../data/fusion.tmLanguage.json';

export const codeToHtml = async ({ code, language }: { code: string; language: string }) => {
  const highlighter = await getHighlighter({
    themes: ['dark-plus'],
    langs: [...Object.keys(bundledLanguages), fusion as any],
  });

  return highlighter.codeToHtml(code, {
    lang: language,
    themes: {
      dark: 'dark-plus',
      light: 'dark-plus',
    },
    colorReplacements: {
      '#1e1e1e': '#282828',
    },
  });
};

We could reproduce the behaviour with different Next.js versions (>=14) and by just refreshing constantly with F5 in the browser.

Our workaround for now is to use unsable_cache to cache the generated highlighted HTML.

Reproduction

https://codesandbox.io/p/devbox/silly-grass-ngrp2h

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
@zhangyu1818
Copy link

Same error, but I'm using@shikijs/rehype in mdx, and I don't know how to solve it yet.

When I reverted to the code that I could deploy yesterday and redeployed it, it still failed.

I'm not sure if this problem is caused by Next.js or Shiki, but I have seen this error many times in the development environment, and I have to restart the development server every time.

By the way, I don't think this title describes the error very well😂

@hlubek hlubek changed the title Describe the bug here "TypeError: onigBinding.UTF8ToString is not a function" after some time / parallel requests Jun 4, 2024
@hlubek hlubek changed the title "TypeError: onigBinding.UTF8ToString is not a function" after some time / parallel requests "TypeError: onigBinding.UTF8ToString is not a function" after some time / parallel requests in Next.js Jun 4, 2024
@slax57
Copy link

slax57 commented Jun 12, 2024

FTR, I ran into the same issue while trying to use the shiki rehype plugin in a node script, to preprocess numerous markdown files to build the documentation website of our framework.

My investigations tend to show this issue is linked to the memory usage. Indeed the node process memory kept increasing (until around 2.3 GB) until this error was thrown.

By playing with the node_modules/@shikijs/core/dist/index.mjs source file, I also managed to get a RuntimeError: memory access out of bounds with a stacktrace pointing to some wasm code.

Finally, I managed to workaround this issue by splitting the files I had to process into several chunks, spawning a new node process for each chunk.
To me, this also gives credit to the memory management hypothesis.

@Neo-Zhixing
Copy link

Ran into the same problem in an application that uses @nuxtjs/mdc when preprocessing a large number of markdown files. I reverted shiki version as far back as 1.6.0 but that didn't make a difference.

@slax57
Copy link

slax57 commented Jun 17, 2024

@Neo-Zhixing I believe the code responsible for this (at least, according to the stacktrace) was introduced in shiki 1.0.0, so you'd have to downgrade to v0 to have a chance to get it working.
However in my case I couldn't do that because I'm using the rehype plugin, so can't be affirmative this will work.

@antfu
Copy link
Member

antfu commented Jun 17, 2024

This down to two problems

  • UTF8ToString was missing during throwing the error, where the error is fail to memory allocation
  • Memory leak was due to getHighlighter being called too many times, where the instance should be cached
    • improved the docs and added a runtime warning in 412fe10

@Neo-Zhixing
Copy link

Thanks! That was fast.

@slax57
Copy link

slax57 commented Jun 17, 2024

Thanks for the update!

For those using the @shikijs/rehype plugin just like me, note that you can pass a getHighlighter singleton instance to the plugin by following the instructions from the Fine-grained Bundle doc section:

import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'
import rehypeShikiFromHighlighter from '@shikijs/rehype/core'

import { getHighlighterCore } from 'shiki/core'

const highlighter = await getHighlighterCore({
  themes: [
    import('shiki/themes/vitesse-light.mjs')
  ],
  langs: [
    import('shiki/langs/javascript.mjs'),
  ],
  loadWasm: import('shiki/wasm')
})

const raw = await fs.readFile('./input.md')
const file = await unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeShikiFromHighlighter, highlighter, {
    //                             ^^^^^^^^^^^ singleton instance
    themes: {
      light: 'vitesse-light',
      dark: 'vitesse-dark',
    }
  })
  .use(rehypeStringify)
  .processSync(raw) // it's also possible to process synchronously

Btw, maybe it would be worth mentioning this good practice in the plugin's doc page (https://shiki.style/packages/rehype) too @antfu , wdyt?

@antfu
Copy link
Member

antfu commented Jun 17, 2024

Yeah sure, PR welcome. I also work on #702 to make it less confusing.

@Gerrit0
Copy link
Contributor

Gerrit0 commented Jun 17, 2024

Memory leak was due to getHighlighter being called too many times, where the instance should be cached

This raises the question... is there a way to ask Shiki to dispose of that memory?

renovate bot referenced this issue in r4ai/r4ai.dev Jun 18, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [shiki](https://togithub.com/shikijs/shiki)
([source](https://togithub.com/shikijs/shiki/tree/HEAD/packages/shiki))
| [`1.6.5` ->
`1.7.0`](https://renovatebot.com/diffs/npm/shiki/1.6.5/1.7.0) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/shiki/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/shiki/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/shiki/1.6.5/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/shiki/1.6.5/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>shikijs/shiki (shiki)</summary>

### [`v1.7.0`](https://togithub.com/shikijs/shiki/releases/tag/v1.7.0)

[Compare
Source](https://togithub.com/shikijs/shiki/compare/v1.6.5...v1.7.0)

#####    🚀 Features

- **core**: Warning on too many instances, close
[#&#8203;693](https://togithub.com/shikijs/shiki/issues/693)  -  by
[@&#8203;antfu](https://togithub.com/antfu) in
[https://github.com/shikijs/shiki/issues/693](https://togithub.com/shikijs/shiki/issues/693)
[<samp>(412fe)</samp>](https://togithub.com/shikijs/shiki/commit/412fe10e)

#####    🐞 Bug Fixes

- **core**: Add missing `UTF8ToString` function, close
[#&#8203;693](https://togithub.com/shikijs/shiki/issues/693)  -  by
[@&#8203;antfu](https://togithub.com/antfu) in
[https://github.com/shikijs/shiki/issues/693](https://togithub.com/shikijs/shiki/issues/693)
[<samp>(0c891)</samp>](https://togithub.com/shikijs/shiki/commit/0c89157f)

#####     [View changes on
GitHub](https://togithub.com/shikijs/shiki/compare/v1.6.5...v1.7.0)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/r4ai/r4ai.dev).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MTAuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQxMC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
diegohaz referenced this issue in ariakit/ariakit Jun 18, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [shiki](https://togithub.com/shikijs/shiki)
([source](https://togithub.com/shikijs/shiki/tree/HEAD/packages/shiki))
| [`1.6.4` ->
`1.7.0`](https://renovatebot.com/diffs/npm/shiki/1.6.4/1.7.0) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/shiki/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/shiki/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/shiki/1.6.4/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/shiki/1.6.4/1.7.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>shikijs/shiki (shiki)</summary>

### [`v1.7.0`](https://togithub.com/shikijs/shiki/releases/tag/v1.7.0)

[Compare
Source](https://togithub.com/shikijs/shiki/compare/v1.6.5...v1.7.0)

#####    🚀 Features

- **core**: Warning on too many instances, close
[#&#8203;693](https://togithub.com/shikijs/shiki/issues/693)  -  by
[@&#8203;antfu](https://togithub.com/antfu) in
[https://github.com/shikijs/shiki/issues/693](https://togithub.com/shikijs/shiki/issues/693)
[<samp>(412fe)</samp>](https://togithub.com/shikijs/shiki/commit/412fe10e)

#####    🐞 Bug Fixes

- **core**: Add missing `UTF8ToString` function, close
[#&#8203;693](https://togithub.com/shikijs/shiki/issues/693)  -  by
[@&#8203;antfu](https://togithub.com/antfu) in
[https://github.com/shikijs/shiki/issues/693](https://togithub.com/shikijs/shiki/issues/693)
[<samp>(0c891)</samp>](https://togithub.com/shikijs/shiki/commit/0c89157f)

#####     [View changes on
GitHub](https://togithub.com/shikijs/shiki/compare/v1.6.5...v1.7.0)

### [`v1.6.5`](https://togithub.com/shikijs/shiki/releases/tag/v1.6.5)

[Compare
Source](https://togithub.com/shikijs/shiki/compare/v1.6.4...v1.6.5)

#####    🐞 Bug Fixes

- **rehype**: Fix type issues  -  by
[@&#8203;remcohaszing](https://togithub.com/remcohaszing) in
[https://github.com/shikijs/shiki/issues/699](https://togithub.com/shikijs/shiki/issues/699)
[<samp>(0297c)</samp>](https://togithub.com/shikijs/shiki/commit/0297cd94)

#####    🏎 Performance

- Improve performance for the `includeExplanation` option  -  by
[@&#8203;thetarnav](https://togithub.com/thetarnav) in
[https://github.com/shikijs/shiki/issues/701](https://togithub.com/shikijs/shiki/issues/701)
[<samp>(966e7)</samp>](https://togithub.com/shikijs/shiki/commit/966e7e40)

#####     [View changes on
GitHub](https://togithub.com/shikijs/shiki/compare/v1.6.4...v1.6.5)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/ariakit/ariakit).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MTAuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQxMC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@antfu
Copy link
Member

antfu commented Jun 21, 2024

Found a proper way to dispose the instance #707, will landed in v1.8

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

No branches or pull requests

6 participants