fix(sdk): stop the browser provider stub from leaking long-poll waiters (#7315)

Part of #7291
This commit is contained in:
Dery Ferdika authored and GitHub committed 2026-11-21 18:03:46 +02:00
1 parent 5109c94bfa
commit cb0ffd0e2f
3 files changed
+67 -4

No files matched your search

@@ -584,7 +584,12 @@ var handleSurface = async (req, res, url) => {
if (surface.seq <= after) {
await new Promise((resolve) => {
surface.waiters.push(resolve);
setTimeout(resolve, wait);
setTimeout(() => {
const at = surface.waiters.indexOf(resolve);
if (at !== -1)
surface.waiters.splice(at, 1);
resolve();
}, wait);
});
}
if (surface.seq <= after) {
@@ -674,7 +679,7 @@ http.createServer((req, res) => {
}
if (url.pathname === "/state" && req.method === "GET") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ url: page.url, title: page.title, caller: lastCaller }));
res.end(JSON.stringify({ url: page.url, title: page.title, caller: lastCaller, waiters: surface.waiters.length }));
return;
}
if (url.pathname === "/navigate" && req.method === "POST") {
@@ -336,7 +336,15 @@ const handleSurface = async (req: http.IncomingMessage, res: http.ServerResponse
if (surface.seq <= after) {
await new Promise<void>((resolve) => {
surface.waiters.push(resolve);
setTimeout(resolve, wait);
// touch() would remove it, but a poll whose timer fires first has to
// remove its own resolver: the host polls again after a 204, so a
// resolver left behind accumulates one per timed-out poll for as long
// as the viewer stays attached, even on an idle page.
setTimeout(() => {
const at = surface.waiters.indexOf(resolve);
if (at !== -1) surface.waiters.splice(at, 1);
resolve();
}, wait);
});
}
if (surface.seq <= after) {
@@ -426,7 +434,9 @@ http.createServer((req, res) => {
}
if (url.pathname === '/state' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ url: page.url, title: page.title, caller: lastCaller }));
// waiters is the long-poll list: it belongs in the state a test reads, since
// a leftover resolver is invisible in the frame answer itself.
res.end(JSON.stringify({ url: page.url, title: page.title, caller: lastCaller, waiters: surface.waiters.length }));
return;
}
if (url.pathname === '/navigate' && req.method === 'POST') {
@@ -0,0 +1,48 @@
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import type { ChildProcess } from 'node:child_process';
import { randomBytes } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { startExampleService } from './fixtures/example-service';
import { SURFACE_FRAME_PATH, SURFACE_SEQ_HEADER } from '../src/service-surface';
// Runs the checked-in browser provider stub the way OpenChamber does, and reads
// the state endpoint the stub exposes. The host long-polls /surface/frame and
// polls again after every 204, so the stub's waiter list has to end each poll,
// timed out or answered, where it started.
const serviceEntry = fileURLToPath(new URL('../examples/browser-provider-stub/service/main.js', import.meta.url));
let service: ChildProcess;
let servicePort: number;
const serviceToken = randomBytes(16).toString('hex');
const authHeaders = () => ({ authorization: `Bearer ${serviceToken}` });
const frame = (query: string) => fetch(`http://127.0.0.1:${servicePort}${SURFACE_FRAME_PATH}?${query}`, { headers: authHeaders() });
beforeAll(async () => {
({ service, port: servicePort } = await startExampleService({ entry: serviceEntry, token: serviceToken }));
});
afterAll(() => {
service?.kill();
});
describe('browser provider stub surface', () => {
test('idle long-polls that time out leave no waiter behind', async () => {
const current = await frame('after=0&wait=0');
expect(current.status).toBe(200);
const seq = current.headers.get(SURFACE_SEQ_HEADER);
expect(seq).toMatch(/^\d+$/);
// A nonzero wait, so each poll parks a waiter until its timer fires.
const firstIdle = await frame(`after=${seq}&wait=200`);
const secondIdle = await frame(`after=${seq}&wait=200`);
expect(firstIdle.status).toBe(204);
expect(secondIdle.status).toBe(204);
const state = await fetch(`http://127.0.0.1:${servicePort}/state`, { headers: authHeaders() });
expect(await state.json()).toMatchObject({ waiters: 0 });
});
});