Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Drop a WebGPU request whose caller aborts while the offscreen document starts

Claudeecc9d2fe3b85
diff --git a/src/chrome/src/providers/webgpu.js b/src/chrome/src/providers/webgpu.js
index 42c13d936..2d2ae6389 100644
--- a/src/chrome/src/providers/webgpu.js
+++ b/src/chrome/src/providers/webgpu.js
@@ -254,6 +254,10 @@ class WebGPUOffscreenProvider extends BaseLLMProvider {
async _dispatch(message, { timeoutMs = 0, signal = null, onAbort = null } = {}) {
if (signal?.aborted) throw responseAbortError(signal);
await ensureOffscreen();
+ // Starting the offscreen document can take a while; an abort that landed meanwhile
+ // has no listener to fire (a listener added to an aborted signal never runs), so
+ // look again before anything is sent.
+ if (signal?.aborted) throw responseAbortError(signal);
return await new Promise((resolve, reject) => {
let settled = false;
const finish = (callback, value) => {
diff --git a/test/run.js b/test/run.js
index 9c8e478f4..dd906dc74 100644
--- a/test/run.js
+++ b/test/run.js
@@ -52059,6 +52059,55 @@ test('WebGPU provider stops waiting and cancels the worker when its caller abort
}
});
+test('WebGPU provider drops a request whose caller aborts while the offscreen document is starting', async () => {
+ const previousChrome = globalThis.chrome;
+ const sent = [];
+ let gateDocument = false;
+ let releaseDocument = null;
+ try {
+ globalThis.chrome = {
+ offscreen: {
+ hasDocument: () => {
+ if (!gateDocument) return Promise.resolve(true);
+ return new Promise(resolve => { releaseDocument = () => resolve(true); });
+ },
+ },
+ runtime: {
+ lastError: undefined,
+ sendMessage(message, callback) {
+ sent.push(message);
+ if (message.type === 'webgpu-download-status') {
+ gateDocument = true; // the next dispatch is the chat itself
+ callback?.({ ok: true, ready: true });
+ } else {
+ callback?.({ ok: true, content: 'should not be asked' });
+ }
+ },
+ },
+ };
+ const provider = new WebGPUProvider({ model: WEBGPU_MODEL_ID });
+ const controller = new AbortController();
+ const pending = provider.chat([{ role: 'user', content: 'Translate this.' }], {
+ maxTokens: 50,
+ signal: controller.signal,
+ });
+ for (let attempt = 0; attempt < 200 && !releaseDocument; attempt++) {
+ await new Promise(resolve => setTimeout(resolve, 2));
+ }
+ assert.equal(typeof releaseDocument, 'function', 'the chat should be waiting for the offscreen document');
+ controller.abort(new Error('The card was closed.'));
+ releaseDocument();
+ await assert.rejects(pending, /The card was closed\./);
+ assert.equal(sent.some(message => message.type === 'webgpu-chat'), false,
+ 'a request aborted while the offscreen document starts must never be sent');
+ assert.equal(sent.some(message => message.type === 'webgpu-chat-cancel'), false,
+ 'nothing was started, so there is nothing to cancel');
+ } finally {
+ if (previousChrome === undefined) delete globalThis.chrome;
+ else globalThis.chrome = previousChrome;
+ }
+});
+
test('WebGPU host and workers carry the cancel request to the generation they stop', () => {
const host = fs.readFileSync(path.join(ROOT, 'src/chrome/src/offscreen/vision-inference-host.js'), 'utf8');
const worker = fs.readFileSync(path.join(ROOT, 'src/chrome/src/offscreen/inference-worker.js'), 'utf8');