Skip to content

Commit e65158e

Browse files
author
Peter Steinberger
committed
fix(ui): keep canvas lease renewals lifecycle-owned and timer-safe
1 parent fdcb321 commit e65158e

2 files changed

Lines changed: 108 additions & 9 deletions

File tree

‎ui/src/app/canvas-surface-lease.runtime.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Loaded after hello so capability renewal does not inflate the startup chunk.
2+
import { resolveSafeTimeoutDelayMs } from "@openclaw/gateway-client/browser";
3+
24
const RENEWAL_LEAD_MS = 15_000;
35
const MIN_RENEWAL_DELAY_MS = 1_000;
46
// Used only when a refresh response omits expiry; an early one-minute refresh
@@ -45,6 +47,7 @@ export function createCanvasSurfaceLease<
4547
let consecutiveFailures = 0;
4648
let generation = 0;
4749
let started = false;
50+
const ownsGeneration = (expected: number) => started && generation === expected;
4851

4952
const clearScheduledRenewal = () => {
5053
if (timer !== null) {
@@ -54,17 +57,24 @@ export function createCanvasSurfaceLease<
5457
};
5558

5659
const schedule = (delayMs: number, expectedGeneration: number) => {
60+
if (!ownsGeneration(expectedGeneration)) {
61+
return;
62+
}
5763
clearScheduledRenewal();
58-
timer = setTimer(() => {
59-
timer = null;
60-
if (started && generation === expectedGeneration) {
64+
timer = setTimer(
65+
() => {
66+
if (!ownsGeneration(expectedGeneration)) {
67+
return;
68+
}
69+
timer = null;
6170
renew(expectedGeneration);
62-
}
63-
}, delayMs);
71+
},
72+
resolveSafeTimeoutDelayMs(delayMs, { minMs: MIN_RENEWAL_DELAY_MS }),
73+
);
6474
};
6575

6676
const handleFailure = (expectedGeneration: number) => {
67-
if (!started || generation !== expectedGeneration) {
77+
if (!ownsGeneration(expectedGeneration)) {
6878
return;
6979
}
7080
consecutiveFailures += 1;
@@ -75,8 +85,7 @@ export function createCanvasSurfaceLease<
7585
const renew = (expectedGeneration: number) => {
7686
if (
7787
inFlight?.generation === expectedGeneration ||
78-
!started ||
79-
generation !== expectedGeneration ||
88+
!ownsGeneration(expectedGeneration) ||
8089
!currentUrl
8190
) {
8291
return;
@@ -85,7 +94,7 @@ export function createCanvasSurfaceLease<
8594
const request = Promise.resolve()
8695
.then(() => params.request("plugin.surface.refresh", { surface: "canvas", observedUrl }))
8796
.then((response) => {
88-
if (!started || generation !== expectedGeneration || currentUrl !== observedUrl) {
97+
if (!ownsGeneration(expectedGeneration) || currentUrl !== observedUrl) {
8998
return;
9099
}
91100
const refreshed = parseCanvasSurfaceRefresh(response);

‎ui/src/app/canvas-surface-lease.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,96 @@ describe("createCanvasSurfaceLease", () => {
185185
expect(clock.nextDelayMs).toBe(60_000);
186186
});
187187

188+
it.each(["stops", "reconnects without a canvas"] as const)(
189+
"does not schedule a retired generation when publishing a refreshed URL %s",
190+
async (transition) => {
191+
const clock = new FakeClock();
192+
const changes: Array<string | null> = [];
193+
const request = vi.fn(async () => ({
194+
surface: "canvas",
195+
pluginSurfaceUrls: { canvas: "https://canvas.test/__openclaw__/cap/refreshed" },
196+
expiresAtMs: 120_000,
197+
}));
198+
const lease = createCanvasSurfaceLease({
199+
request,
200+
onChange: (url) => {
201+
changes.push(url);
202+
if (url === "https://canvas.test/__openclaw__/cap/refreshed") {
203+
if (transition === "stops") {
204+
lease.stop();
205+
} else {
206+
lease.start(undefined);
207+
}
208+
}
209+
},
210+
now: clock.now,
211+
setTimer: clock.setTimer,
212+
clearTimer: clock.clearTimer,
213+
});
214+
215+
lease.start("https://canvas.test/__openclaw__/cap/original");
216+
await flushPromises();
217+
218+
expect(request).toHaveBeenCalledOnce();
219+
expect(changes).toEqual([
220+
"https://canvas.test/__openclaw__/cap/original",
221+
"https://canvas.test/__openclaw__/cap/refreshed",
222+
null,
223+
]);
224+
expect(clock.pendingCount).toBe(0);
225+
},
226+
);
227+
228+
it("keeps the reconnect renewal owned when a retired timer callback arrives late", async () => {
229+
const request = vi
230+
.fn<(method: string, params: unknown) => Promise<unknown>>()
231+
.mockResolvedValueOnce({
232+
surface: "canvas",
233+
pluginSurfaceUrls: { canvas: "https://canvas.test/__openclaw__/cap/first" },
234+
expiresAtMs: 120_000,
235+
})
236+
.mockResolvedValueOnce({
237+
surface: "canvas",
238+
pluginSurfaceUrls: { canvas: "https://canvas.test/__openclaw__/cap/reconnected" },
239+
expiresAtMs: 180_000,
240+
});
241+
const { clock, lease } = createLeaseHarness(request);
242+
243+
lease.start("https://canvas.test/__openclaw__/cap/original");
244+
await flushPromises();
245+
const retiredCallback = clock.takeNextCallback();
246+
expect(retiredCallback).toBeDefined();
247+
248+
lease.stop();
249+
lease.start("https://canvas.test/__openclaw__/cap/reconnect");
250+
await flushPromises();
251+
expect(clock.pendingCount).toBe(1);
252+
253+
retiredCallback?.();
254+
expect(request).toHaveBeenCalledTimes(2);
255+
lease.stop();
256+
257+
expect(clock.pendingCount).toBe(0);
258+
});
259+
260+
it.each([Number.MAX_SAFE_INTEGER, 2 ** 32 + 115_000])(
261+
"clamps an advertised canvas expiry of %d to a browser-safe native timer",
262+
async (expiresAtMs) => {
263+
const request = vi.fn(async () => ({
264+
surface: "canvas",
265+
pluginSurfaceUrls: { canvas: "https://canvas.test/__openclaw__/cap/refreshed" },
266+
expiresAtMs,
267+
}));
268+
const { clock, lease } = createLeaseHarness(request);
269+
270+
lease.start("https://canvas.test/__openclaw__/cap/original");
271+
await flushPromises();
272+
273+
expect(clock.nextDelayMs).toBe(2_147_483_647);
274+
lease.stop();
275+
},
276+
);
277+
188278
it("stop clears timers, ignores an in-flight result, and publishes null once", async () => {
189279
const pending = deferred<unknown>();
190280
const { changes, clock, connectionChanges, lease } = createLeaseHarness(() => pending.promise);

0 commit comments

Comments
 (0)