Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(engine): sampleOnce() joins an in-flight sample instead of skipping it
Vance Ingallsa35168edddab
diff --git a/packages/engine/src/services/chromeMemorySampler.test.ts b/packages/engine/src/services/chromeMemorySampler.test.tsindex ca784ccf4c..7f62629829 100644--- a/packages/engine/src/services/chromeMemorySampler.test.ts+++ b/packages/engine/src/services/chromeMemorySampler.test.ts@@ -125,6 +125,38 @@ describe("createChromeMemorySampler", () => {expect(maxInFlight).toBe(1);});+ it("stop(); await sampleOnce() joins an in-flight tick: stats current at close, no late onSample", async () => {+ // Mirrors the closeCaptureSession sequence. The tick at 100 ms is still+ // waiting on getPids (350 ms) when close runs; the close-time sample must+ // resolve after that tick has merged, not before.+ vi.useFakeTimers();+ const onSample = vi.fn();+ const sampler = createChromeMemorySampler({+ getPids: async () => {+ await new Promise((r) => setTimeout(r, 350));+ return { browser: 1, renderers: [], gpu: [] };+ },+ sampleRss: async () => [{ pid: 1, rssMb: 1 }],+ intervalMs: 100,+ onSample,+ });+ sampler.start();+ await vi.advanceTimersByTimeAsync(150);+ sampler.stop();+ let samplesAtClose = -1;+ let onSampleCallsAtClose = -1;+ const closing = sampler.sampleOnce().then(() => {+ samplesAtClose = sampler.stats().samples;+ onSampleCallsAtClose = onSample.mock.calls.length;+ });+ await vi.advanceTimersByTimeAsync(1000);+ await closing;+ expect(samplesAtClose).toBe(1);+ expect(onSampleCallsAtClose).toBe(1);+ await vi.advanceTimersByTimeAsync(1000);+ expect(onSample).toHaveBeenCalledTimes(1);+ });+it("does not invoke onSample when a sample returned no rows", async () => {const onSample = vi.fn();const sampler = createChromeMemorySampler({diff --git a/packages/engine/src/services/chromeMemorySampler.ts b/packages/engine/src/services/chromeMemorySampler.tsindex ea0d356e8a..b02c4432ed 100644--- a/packages/engine/src/services/chromeMemorySampler.ts+++ b/packages/engine/src/services/chromeMemorySampler.ts@@ -45,7 +45,12 @@ export interface ChromeMemorySampler {start(): void;stop(): void;stats(): ChromeMemoryStats;- /** One synchronous-to-the-caller sample; used by tests and by close(). */+ /**+ * One sample, awaited by the caller; used by tests and by close(). If a+ * sample is already in flight this resolves when that sample finishes+ * instead of starting another, so `stop(); await sampleOnce()` always sees+ * the running tick's result and `onSample` cannot fire after it resolves.+ */sampleOnce(): Promise<void>;}@@ -84,11 +89,9 @@ export function createChromeMemorySampler(deps: ChromeMemorySamplerDeps): Chromeconst clearIntervalFn: ClearIntervalFn = deps.clearIntervalFn ?? clearInterval;let stats: ChromeMemoryStats = { samples: 0 };let timer: NodeJS.Timeout | null = null;- let inFlight = false;+ let inFlight: Promise<void> | null = null;- const sampleOnce = async (): Promise<void> => {- if (inFlight) return;- inFlight = true;+ const runSample = async (): Promise<void> => {try {const pids = await deps.getPids();const all = [pids.browser, ...pids.renderers, ...pids.gpu].filter(@@ -103,11 +106,22 @@ export function createChromeMemorySampler(deps: ChromeMemorySamplerDeps): Chrome} catch {// Sampling is observability only; a CDP or ps failure must never reach// the capture loop. The next tick retries.- } finally {- inFlight = false;}};+ const sampleOnce = (): Promise<void> => {+ // Reentrancy guard: an interval tick that lands while a sample is still+ // running joins it rather than stacking a second CDP/ps round-trip, and a+ // close-time call waits for the running tick instead of skipping it.+ // runSample never rejects, so the .finally reset always runs; it runs on+ // a microtask, after the assignment below.+ if (inFlight !== null) return inFlight;+ inFlight = runSample().finally(() => {+ inFlight = null;+ });+ return inFlight;+ };+return {start() {if (timer !== null) return;