commit 0974467706f2067587546c466e799d484eed8dd9
parent 702c391f4f72ae7fa9c409e5d157a8a3e1c4b80c
Author: Greg Tatum <tatum.creative@gmail.com>
Date: Thu, 30 Oct 2025 19:45:48 +0000
Bug 1996291 - Adjust the flow for accessing the MLEngine to make the types correct r=firefox-ai-ml-reviewers,tarek
This changes the implementation a bit, but the flow should have the
exact same behavior. The ready status is not being applied in a promise
which should run before any other code. You can still check if the
engine is ready by awaiting on the engine. However, now when you need
the engine you just await the #engine property.
The documentation for the type was incorrect to what it actually was as
the #engine was original wrapped in a promise, then it was unwrapped,
which was a bit confusing to work with.
Differential Revision: https://phabricator.services.mozilla.com/D269981
Diffstat:
1 file changed, 18 insertions(+), 19 deletions(-)
diff --git a/toolkit/components/ml/actors/MLEngineChild.sys.mjs b/toolkit/components/ml/actors/MLEngineChild.sys.mjs
@@ -151,7 +151,7 @@ export class MLEngineChild extends JSProcessActorChild {
// NOTE: This is done after adding to #engineDispatchers to ensure other
// async calls see the new dispatcher.
if (!lazy.PipelineOptions.isMocked(pipelineOptions)) {
- await dispatcher.ensureInferenceEngineIsReady();
+ await dispatcher.isReady();
}
this.#engineStatuses.set(engineId, "READY");
@@ -314,8 +314,8 @@ class EngineDispatcher {
/** @type {PromiseWithResolvers} */
#modelRequest;
- /** @type {Promise<InferenceEngine> | null} */
- #engine = null;
+ /** @type {Promise<InferenceEngine>} */
+ #engine;
/** @type {string} */
#taskName;
@@ -429,9 +429,12 @@ class EngineDispatcher {
}
);
- // Trigger the keep alive timer.
this.#engine
- .then(() => void this.keepAlive())
+ .then(() => {
+ this.#status = "READY";
+ // Trigger the keep alive timer.
+ void this.keepAlive();
+ })
.catch(error => {
if (
// Ignore errors from tests intentionally causing errors.
@@ -455,14 +458,6 @@ class EngineDispatcher {
};
}
- /**
- * Resolves the engine to fully initialize it.
- */
- async ensureInferenceEngineIsReady() {
- this.#engine = await this.#engine;
- this.#status = "READY";
- }
-
handleInitProgressStatus(port, notificationsData) {
port.postMessage({
type: "EnginePort:InitProgress",
@@ -508,6 +503,13 @@ class EngineDispatcher {
}
/**
+ * Wait for the engine to be ready.
+ */
+ async isReady() {
+ await this.#engine;
+ }
+
+ /**
* @param {MessagePort} port
*/
#setupMessageHandler(port) {
@@ -544,7 +546,7 @@ class EngineDispatcher {
case "EnginePort:Run": {
const { requestId, request, engineRunOptions } = data;
try {
- await this.ensureInferenceEngineIsReady();
+ await this.isReady();
} catch (error) {
port.postMessage({
type: "EnginePort:RunResponse",
@@ -565,15 +567,12 @@ class EngineDispatcher {
this.keepAlive();
this.#status = "RUNNING";
+ const engine = await this.#engine;
try {
port.postMessage({
type: "EnginePort:RunResponse",
requestId,
- response: await this.#engine.run(
- request,
- requestId,
- engineRunOptions
- ),
+ response: await engine.run(request, requestId, engineRunOptions),
error: null,
});
} catch (error) {