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

[Web] Error when using Web Workers on Next.js #22113

Open
illbexyz opened this issue Sep 17, 2024 · 24 comments
Open

[Web] Error when using Web Workers on Next.js #22113

illbexyz opened this issue Sep 17, 2024 · 24 comments
Labels
platform:web issues related to ONNX Runtime web; typically submitted using template

Comments

@illbexyz
Copy link

Describe the issue

I get the following error when using onnxruntime-web on Next.js with web workers:

  • Error: no available backend found. ERR: [wasm] TypeError: Failed to fetch, [cpu] Error: multiple calls to 'initWasm()' detected.

I had no issues with [email protected].

I believe the issue is that Next.js is trying to load the file from the filesystem:

  • See Request URL in the following screenshot file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs

Additional notes:

  • I have no issues when not using web workers
  • I obtain a slightly different error when disabling the proxy but setting onnx.env.wasm.numThreads = 2
    • Error: no available backend found. ERR: [wasm] SecurityError: Failed to construct 'Worker': Script at 'file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs' cannot be accessed from origin 'http://localhost:3000'., [cpu] Error: previous call to 'initWasm()' failed..
    • In this case there are no network requests being made but I believe these issues are likely related.

Network requests screenshot:

image

To reproduce

The following reproductions are a bare Next.js 14 starter repository with [email protected].

Urgency

Kinda urgent: every Next.js project is impacted.

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.19.2

Execution Provider

'wasm'/'cpu' (WebAssembly CPU)

@illbexyz illbexyz added the platform:web issues related to ONNX Runtime web; typically submitted using template label Sep 17, 2024
@satyajandhyala
Copy link
Contributor

satyajandhyala commented Sep 18, 2024

@illbexyz Can you try using onnxruntime 1.19 in a clean environment?

@illbexyz
Copy link
Author

@satyajandhyala This is already the most minimal reproduction I could give. It's just onnxruntime-web added to a bare Next.js project initialized with npx create-next-app@latest.

@fs-eire
Copy link
Contributor

fs-eire commented Sep 18, 2024

Script at 'file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs' cannot be accessed from origin 'http://localhost:3000'.

This is a known issue. If you use webpack loading onnxruntime-web as ESM, you need this line in your webpack config:

module: { parser: { javascript: { importMeta: false } } },

(see https://github.com/Microsoft/onnxruntime/blob/abdc31de401262bcb03f538423389c2eb264a0ce/js/web/test/e2e/webpack.config.esm-js.js)

Explaination: onnxruntime-web use import.meta.url at runtime to determine the script path so that it is able to create worker with correct path. However the default behavior of Webpack rewrites import.meta.url into a static local file path ( in your case file:///Users/alberto.nicoletti/node_modules/onnxruntime-web/dist/ort.bundle.min.mjs) and it won't work at runtime. the highlighted line in the config disabled this specific behavior of webpack.

May be a good idea to update our document/examples.

@illbexyz
Copy link
Author

Hey @fs-eire, thanks for your time!

I tried adding the following to next.config.mjs:

webpack: (config) => {
  config.module.parser.javascript.importMeta = false;

  return config;
},

But now I get a different error:

SyntaxError: Cannot use 'import.meta' outside a module

I updated the reproductions if you wanna have a look.

image

@fs-eire
Copy link
Contributor

fs-eire commented Sep 24, 2024

I made a PR #22196. Hope to workaround this issue without having to modify webpack config from user side.

Copy link
Contributor

This issue has been automatically marked as stale due to inactivity and will be closed in 30 days if no further activity occurs. If further support is needed, please provide an update and/or more details.

@github-actions github-actions bot added the stale issues that have not been addressed in a while; categorized by a bot label Oct 24, 2024
@fs-eire
Copy link
Contributor

fs-eire commented Oct 24, 2024

The issue is not resolved. I am still working on it.

@github-actions github-actions bot removed the stale issues that have not been addressed in a while; categorized by a bot label Oct 25, 2024
@arkniazi
Copy link

arkniazi commented Nov 5, 2024

@fs-eire I'm also facing similar issue. Were you able to create a fix for it?

@fs-eire
Copy link
Contributor

fs-eire commented Nov 7, 2024

It is still on my list. However I have multiple tasks to do so it may take a while. #22196 is an attempt but testing result shows that it does not fix everything. #22615 is the issue that tracks and explains why it is hard to fix.

I would be appreciated if anyone can help contribute or provider detailed information/clues to the issue.

@illbexyz
Copy link
Author

@fs-eire until the issue is fixed for good, it would great to have a way to patch onnxruntime-web to work for the specific case of Next.js/Webpack. Can you please point me in the right direction?

@fs-eire
Copy link
Contributor

fs-eire commented Nov 26, 2024

@illbexyz if you are OK with loading the .mjs file and .wasm file dynamically from CDN, here is the steps:

  • try to import the non .bundle file. you can either patch the package.json of onnxruntime-web, or add a resolve rule in your webpack config to achieve this. Or you can import onnxruntime-web as UMD instead of ESM.
  • set ort.env.wasm.wasmPaths:
    ort.env.wasm.wasmPaths = {
      mjs: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/ort-wasm-simd-threaded.mjs',
      wasm: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/ort-wasm-simd-threaded.wasm'
    };

@364199013
Copy link

Hello, have you solved this problem? I encountered the same issue when setting up the web demo for Segment Anything.

@fs-eire
Copy link
Contributor

fs-eire commented Jan 14, 2025

@364199013 which version of onnxruntime-web are you using? A recent change (#23257) should have fixed major use cases with nextjs so please let me know:

  • the version of onnxruntime-web
  • the setup (reproduce) steps that get into the issue

@364199013
Copy link

364199013 commented Jan 14, 2025

@fs-eire 1.20.1,At web demo for Segment Anything. this project url is https://github.com/facebookresearch/segment-anything.git.

Image

@364199013
Copy link

I resolved the issue after building the project. @fs-eire

@sehy0803
Copy link

@364199013 Hello, I also had the same issue when setting up the web demo for Segment Anything, so I built the project. However, the build failed with this error message 'Failed to load./.env.' Do you happen to know a solution? Please help. Thank you.

@ysh-art
Copy link

ysh-art commented Jan 15, 2025

I am getting this error for onnxruntime-web: ^1.20.1

@fs-eire
Copy link
Contributor

fs-eire commented Jan 15, 2025

Please use a dev version including change (#23257). For example, "1.21.0-dev.20250114-228dd16893"

@ysh-art
Copy link

ysh-art commented Jan 16, 2025

Please use a dev version including change (#23257). For example, "1.21.0-dev.20250114-228dd16893"

Now I'm gettting the following error
"Uncaught (in promise) DataCloneError: Failed to execute 'postMessage' on 'Worker': ArrayBuffer at index 3 is already detached."

@fs-eire
Copy link
Contributor

fs-eire commented Jan 18, 2025

This error message is likely caused by accessing a detached ArrayBuffer. For example, you have an instance of ArrayBuffer that transferred from main thread to worker thread using postMessage() with the array buffer in parameter transfer, and then use the ArrayBuffer in main thread. This will trigger the error.

@kevinjosethomas
Copy link

kevinjosethomas commented Jan 24, 2025

hey, I was running into a similar issue a few weeks ago and recently ported to 1.21.0-dev.20250114-228dd16893, which fixed the issue in development. However, when building my NextJS project, I run into another issue:

% npm run build

> build
> next build


./src/pages/analysis/[...id].tsx
455:45  Warning: Unexpected any. Specify a different type.  @typescript-eslint/no-explicit-any

info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
 ✓ Linting and checking validity of types    
Failed to compile.

static/media/ort.bundle.min.69c3a2ff.mjs from Terser
  x 'import.meta' cannot be used outside of module code.
   ,-[3:1]
 3 |  * Copyright (c) Microsoft Corporation. All rights reserved.
 4 |  * Licensed under the MIT License.
 5 |  */
 6 | var Un=Object.defineProperty;var Vp=Object.getOwnPropertyDescriptor;var Wp=

This is followed with the script and quite a long traceback. Any idea how I can tackle this? I'm using [email protected] and [email protected]

@fs-eire
Copy link
Contributor

fs-eire commented Jan 26, 2025

@kevinjosethomas could you share the repo with this issue? I can take a look

@kevinjosethomas
Copy link

Of course, thank you. The codebase is quite large but this is where all my onnx model inference occurs: https://github.com/CSSLab/maia-platform-frontend/blob/v1/src/utils/maia2/model.ts

@fs-eire
Copy link
Contributor

fs-eire commented Jan 30, 2025

Of course, thank you. The codebase is quite large but this is where all my onnx model inference occurs: https://github.com/CSSLab/maia-platform-frontend/blob/v1/src/utils/maia2/model.ts

I cloned the repo, checked out the branch v1, and I can reproduce the build failure.

To fix the failure, I updated 3 packages to latest:

  • next: v15 fixes the failure.
  • typescript: v5 is required, otherwise compile will fail with next v15
  • onnxruntime-web: include fixes related to package export.

There are other errors, but I think they are not related to onnxruntime.

ReferenceError: window is not defined
    at window (src\hooks\useLocalStorage\useLocalStorage.ts:10:19)
    at useState (src\hooks\useLocalStorage\useLocalStorage.ts:8:48)
    at THEME (src\hooks\useTheme\useTheme.ts:8:51)
    at useTheme (src\providers\ThemeContextProvider\ThemeContextProvider.tsx:10:39)
   8 |   const [storedValue, setStoredValue] = useState<T>(() => {
   9 |     try {
> 10 |       const item = window.localStorage.getItem(key)
     |                   ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform:web issues related to ONNX Runtime web; typically submitted using template
Projects
None yet
Development

No branches or pull requests

8 participants