Avoid duplicated opencv/ort fetches#6494
Conversation
| // before build/init resolve, so we don't leak the in-flight worker. | ||
| signal.addEventListener('abort', worker.terminate, { once: true }); | ||
| // | ||
| // CRITICAL: pass an arrow function, NOT `worker.terminate` directly. |
There was a problem hiding this comment.
this is the fix for the duplication.
| headers: { | ||
| 'Cross-Origin-Embedder-Policy': 'credentialless', | ||
| 'Cross-Origin-Opener-Policy': 'same-origin', | ||
| 'Cache-Control': 'public, max-age=31536000, immutable', |
There was a problem hiding this comment.
this should be done in production (already asked backend) but it also improves DX. Basically it tells the browser that these files will never change (opencv, wasm, etc) so it's all good to cache them, even if hashed (e.g. ort23478234823.wasm)
There was a problem hiding this comment.
Pull request overview
This PR adjusts annotator web-worker lifecycle management to avoid repeated ORT/OpenCV/model fetches by keeping workers alive across intra-dataset navigation and terminating them only when leaving the dataset section.
Changes:
- Move annotator-tool worker teardown from annotator unmount to the dataset-section boundary via a new
useTerminateAnnotatorWorkersOnUnmounthook. - Keep the Segment Anything worker query alive indefinitely (
gcTime: Infinity) and fix its abort handler to reliably terminate on cancellation. - Add a global
Cache-Controlheader in the Rsbuild dev server config.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
application/ui/src/router.tsx |
Introduces DatasetSection wrapper to own worker lifetime across dataset routes. |
application/ui/src/features/annotator/tools/use-preload-webworkers.hook.ts |
Removes per-annotator unmount teardown so workers can persist within the dataset section. |
application/ui/src/features/annotator/tools/terminate-annotator-workers-on-unmount.hook.ts |
Adds dataset-section unmount cleanup to terminate workers and remove worker queries. |
application/ui/src/features/annotator/tools/segment-anything-tool/use-segment-anything.hook.ts |
Improves worker caching and abort termination behavior; sets gcTime: Infinity. |
application/ui/rsbuild.config.ts |
Adds a global Cache-Control response header for the dev server. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
🐳 Docker image sizes
|
📊 Test coverage report
|
| // for no functional gain (encoder/decoder always run sequentially anyway). | ||
| // | ||
| // `gcTime: Infinity` is critical: the default 5-min gc would evict the worker | ||
| // entry whenever SAM is unmounted (switching tools/projects), causing the |
There was a problem hiding this comment.
unmounted and not used for 5 min I guess, not right away after unmunting
Summary
Explanation:
onnx spawns X workers, where X depends on the amount of cpu cores your machine has, and each worker imports ort, hence why we would see all these imports. Same goes for opencv. We could limit the threads but concurrency is what we want, so we have a tradeoff between bandwith/memory and performance.
Before:


After:


How to test
Checklist