Current Code
In src/parakeet.js, the fromUrls static method initializes the ONNX Runtime using initOrt before creating sessions. In webgpu-hybrid mode, it attempts to prevent races by creating sessions sequentially:
if (backend === 'webgpu-hybrid') {
// Avoid parallel create to prevent double initWasm race
encoderSession = await createSession(encoderUrl, encoderSessionOptions);
joinerSession = await createSession(decoderUrl, decoderSessionOptions);
} else {
[encoderSession, joinerSession] = await Promise.all([...]);
}
The Risk
While this handles sequential session creation within a single fromUrls call, it does not account for external concurrency.
If multiple components or service workers call fromUrls simultaneously, they will both enter the initialization phase at the same time. This leads to:
- WASM Re-initialization: Multiple attempts to initialize the WASM runtime.
- Provider Registration Conflicts: Potential crashes when registering the same backend provider simultaneously.
Proposed Fix
Implement a Singleton promise or a mutex/lock around the initialization logic. This ensures that the initialization and session creation logic runs exactly once globally, even if multiple model instances are requested at the same time.
Suggested Implementation Strategy:
// Use a module-level variable to track the initialization state
let initializationPromise = null;
function getInitializedSessions() {
if (!initializationPromise) {
initializationPromise = (async () => {
await initOrt();
// ... session creation logic
})();
}
return initializationPromise;
}
Current Code
In
src/parakeet.js, thefromUrlsstatic method initializes the ONNX Runtime usinginitOrtbefore creating sessions. Inwebgpu-hybridmode, it attempts to prevent races by creating sessions sequentially:The Risk
While this handles sequential session creation within a single
fromUrlscall, it does not account for external concurrency.If multiple components or service workers call
fromUrlssimultaneously, they will both enter the initialization phase at the same time. This leads to:Proposed Fix
Implement a Singleton promise or a mutex/lock around the initialization logic. This ensures that the initialization and session creation logic runs exactly once globally, even if multiple model instances are requested at the same time.
Suggested Implementation Strategy: