fix: make ffmpeg loader concurrency-safe for concurrent load calls#1545
Open
smirk-dev wants to merge 1 commit into
Open
fix: make ffmpeg loader concurrency-safe for concurrent load calls#1545smirk-dev wants to merge 1 commit into
smirk-dev wants to merge 1 commit into
Conversation
Concurrent loadFFmpeg() calls could each trigger the worker initialization
flow, double-loading the same WASM runtime and registering duplicate
listeners. Replace the brittle isFirstLoad/workerReady sentinels with an
explicit shared in-flight promise (ffmpegLoadingPromise) plus an ffmpegLoaded
flag so:
- The worker is created and {type:"load"} is posted at most once per init.
- Concurrent callers join the same promise instead of starting a second load.
- A failed load resets state (resetWorker) so later calls can retry cleanly.
- Aborting one caller rejects only that caller and leaves the shared load
intact for others.
Adds regression coverage for concurrent init, retry-after-failure, and abort.
Closes magic-peach#1014
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
@smirk-dev is attempting to deploy a commit to the magic-peach1's projects Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
✅ PR Format Check Passed — @smirk-devBasic format checks passed. A maintainer will review your code changes. This does not mean the PR is approved — it just means the format is correct. |
Contributor
👋 Thanks for your PR, @smirk-dev!Welcome to Reframe — a browser-based video editor built for everyone 🎬 What happens next
Quick checklist
Useful links
Happy coding! 🎉 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1014
Summary
Add a Promise-based guard around the FFmpeg loader so concurrent
loadFFmpeg()calls share the same initialization and never double-initialize the WASM runtime.Before this change, the loader relied on an
isFirstLoadcapture plus aworkerReady/workerReadyResolve === nullsentinel to decide whether to trigger initialization. That scheme is fragile under concurrency: overlapping callers can each drive part of the init flow, which risks posting the workerloadcommand more than once, registering the runtime's progress/log listeners more than once, and leaving shared state half-initialized if a load fails. (In this projectffmpeg.load()and its listeners live inside the Web Worker — the{ type: "load" }message is what triggers them, so "load once" means "post that message once".)The fix introduces a single shared in-flight promise that all concurrent callers await, plus an explicit
ffmpegLoadedflag for the already-initialized fast path. The publicloadFFmpeg(signal?, onProgress?)signature and the normal (non-racy) export flow are unchanged.Technical details
ffmpegLoaded: booleanandffmpegLoadingPromise: Promise<void> | null(withresolveLoad/rejectLoadsettle handles).ffmpegLoadedis already true,loadFFmpeg()returns immediately and reports 100%.startLoad()(which creates the worker and posts{ type: "load" }exactly once); any caller that arrives while the promise is in flight simplyawaits the same promise instead of starting a second load.loadcommand is posted at most once per init cycle, the worker-sideffmpeg.on("progress", …)registration inloadCore()runs once per runtime — no duplicate listeners from overlapping callers.error(oronerror),failLoad()rejects the shared promise andresetWorker()clearsffmpegWorker,ffmpegLoaded, andffmpegLoadingPromise. Because the in-flight promise reference is cleared, the nextloadFFmpeg()call starts a clean new initialization and can succeed.Promise.raceagainst an abort promise) and leaves the shared load intact for other in-flight callers, rather than rejecting the shared readiness promise for everyone.Testing
Commands run (all green):
bun run test— full Vitest suite, 127 tests passing (14 files), including the new cases.bun run lint—next lint, no warnings or errors.bun run build—next buildstatic export succeeds.New regression tests in
src/lib/tests/ffmpegLoader.test.tsstub the globalWorkerand assert the concurrency invariants:loadFFmpeg()calls create one worker and post theloadcommand once, and all callers resolve together onready.load).readyreports100%to the caller's progress callback exactly once.ready; an already-aborted signal rejects immediately without creating a worker.Compatibility
No change to the exported
loadFFmpeg/exportVideoAPI or to the normal non-racy export path — the only behavioral change is that overlapping initializations now share one runtime load instead of racing.