From de49dd979f43cacfb7ecbb53477d3527d5af84b4 Mon Sep 17 00:00:00 2001 From: Neil Date: Sun, 15 Nov 2026 03:50:45 -0800 Subject: [PATCH] chore(sandbox-control): log the step a create waits in before it starts (CREATE-STEP-WATCH) --- cloud/apps/sandbox-control/src/index.ts | 41 ++++++++++++++++--- .../test/create-step-watch.test.ts | 38 +++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 cloud/apps/sandbox-control/test/create-step-watch.test.ts diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 435dfce40ae..ff17ff64f6f 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -891,6 +891,34 @@ function fail(error: string, status: number, reason?: string): Response { return json(reason ? { error, reason } : { error }, status) } +/** CREATE-STEP-WATCH: logs a step still pending after 10 s and, once the create starts, every step's time. */ +export function createStepWatch(enabled: boolean, now: () => number = Date.now) { + const started = now() + const steps: Record = {} + let reported = false + return { + async step(name: string, work: Promise): Promise { + if (!enabled) { + return work + } + const at = now() + const slow = setTimeout(() => console.log(JSON.stringify({ event: 'create_step_slow', step: name, ms: now() - at })), 10_000) + try { + return await work + } finally { + clearTimeout(slow) + steps[name] = now() - at + } + }, + done() { + if (enabled && !reported) { + reported = true + console.log(JSON.stringify({ event: 'create_steps', ms: now() - started, steps })) + } + } + } +} + async function readJson(request: Request, maxBytes = MAX_JSON_BYTES): Promise { const text = await request.text() if (text.length > maxBytes) { @@ -1809,11 +1837,13 @@ async function routeRequest(request: Request, env: Env, deps: Deps): Promise { + watch.done() // Desktops that show the clone as its own step ask for it in the background (GET .../clone). // Member identities install after the response; the VM's git holds commits and pushes until then. let ready: Ready diff --git a/cloud/apps/sandbox-control/test/create-step-watch.test.ts b/cloud/apps/sandbox-control/test/create-step-watch.test.ts new file mode 100644 index 00000000000..5c772704458 --- /dev/null +++ b/cloud/apps/sandbox-control/test/create-step-watch.test.ts @@ -0,0 +1,38 @@ +// CREATE-STEP-WATCH: creates hung 180 s with no log and no row (n73); the steps before the create log. +import { afterEach, describe, expect, it, vi } from 'vitest' +import { createStepWatch } from '../src/index' + +describe('CREATE-STEP-WATCH', () => { + afterEach(() => { + vi.useRealTimers() + vi.restoreAllMocks() + }) + + it('names a step still pending after 10 s, then reports every step once the create starts', async () => { + vi.useFakeTimers() + const log = vi.spyOn(console, 'log').mockImplementation(() => undefined) + const watch = createStepWatch(true, () => Date.now()) + let finish: (value: string) => void = () => undefined + const body = watch.step('body', new Promise((resolve) => (finish = resolve))) + await vi.advanceTimersByTimeAsync(10_000) + expect(log).toHaveBeenCalledWith(expect.stringContaining('"step":"body"')) + finish('{}') + await expect(body).resolves.toBe('{}') + watch.done() + watch.done() + const reports = log.mock.calls.filter(([line]) => String(line).includes('"create_steps"')) + expect(reports).toHaveLength(1) + expect(String(reports[0]![0])).toContain('"body":10000') + }) + + it('stays quiet for other routes', async () => { + vi.useFakeTimers() + const log = vi.spyOn(console, 'log').mockImplementation(() => undefined) + const watch = createStepWatch(false) + const pending = watch.step('auth', new Promise(() => undefined)) + await vi.advanceTimersByTimeAsync(20_000) + watch.done() + expect(log).not.toHaveBeenCalled() + void pending + }) +})