From ec3e2ead368d3b7372a703c28c6dd9c903b24a00 Mon Sep 17 00:00:00 2001 From: Pipeline Agent Date: Tue, 1 Sep 2026 23:22:22 +0000 Subject: [PATCH] fix(load3d): invalidate abandoned thumbnail loads and bound the whole render MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #16485's approve-with-comments review (Cursor 8-reviewer panel, 1 High / 1 Medium / 3 Low findings, none addressed before merge): - High: a timed-out load's finally block called remove() (disposes the model/loader managers) but never bumped LoaderManager.currentLoadId, so the abandoned in-flight load still passed its own staleness guard and called setupModel() on an already-disposed manager — leaking GPU resources and risking a spurious errorLoadingModel toast. Added LoaderManager.invalidate() (bumps currentLoadId) and call it before remove() in the thumbnail module's finally block. - Medium: withTimeout() only wrapped load3d.loadModel(); the dynamic import(), createLoad3d() (allocates a WebGL context), and captureThumbnail() were unbounded. A stall in any of them would wedge the module-global queue for the rest of the session — the exact head-of-line failure this module exists to prevent. Now the whole renderThumbnail body is bounded. - Low: a timeout now reports through reportError() with a stable errorType (agent_crdt... no — load3d_thumbnail_generation_failed) instead of silently degrading to null. - Low (fake timers not restored in the test): verified moot — the project's global Vitest setup already restores real timers automatically (oxlint's no-redundant-vitest-cleanup rule confirms this). - Nit (es-toolkit's withTimeout leaves a pending setTimeout on the winning race branch): upstream library behavior, out of scope for this repo; left as-is per the original reviewer's own 'non-blocking' call. New regression test pins invalidate() running before remove() and proves an abandoned load can no longer reach captureThumbnail after timeout. Quality gates: focused Vitest 6/6, vue-tsc clean, eslint clean, oxlint clean, oxfmt clean, knip clean. --- src/components/load3d/modelThumbnail.test.ts | 45 ++++++++++++++++++++ src/components/load3d/modelThumbnail.ts | 44 +++++++++++++------ src/extensions/core/load3d/LoaderManager.ts | 12 ++++++ 3 files changed, 87 insertions(+), 14 deletions(-) diff --git a/src/components/load3d/modelThumbnail.test.ts b/src/components/load3d/modelThumbnail.test.ts index a77c8b025d0..21648086698 100644 --- a/src/components/load3d/modelThumbnail.test.ts +++ b/src/components/load3d/modelThumbnail.test.ts @@ -9,14 +9,19 @@ vi.mock('@/platform/assets/utils/assetPreviewUtil', () => ({ persistThumbnail })) +const reportError = vi.hoisted(() => vi.fn()) +vi.mock('@/platform/telemetry/reportError', () => ({ reportError })) + const createLoad3d = vi.hoisted(() => vi.fn()) vi.mock('@/extensions/core/load3d/createLoad3d', () => ({ createLoad3d })) function mockInstance(overrides: Record = {}) { + const invalidate = vi.fn() return { loadModel: vi.fn().mockResolvedValue(undefined), captureThumbnail: vi.fn().mockResolvedValue('data:image/png;base64,thumb'), remove: vi.fn(), + getLoaderManager: vi.fn(() => ({ invalidate })), ...overrides } } @@ -26,6 +31,7 @@ describe('generateModelThumbnail', () => { createLoad3d.mockReset() isAssetPreviewSupported.mockReset().mockReturnValue(false) persistThumbnail.mockReset() + reportError.mockReset() }) it('renders offscreen, returns the data url, and disposes the instance', async () => { @@ -109,5 +115,44 @@ describe('generateModelThumbnail', () => { await expect(nextRun).resolves.toBe('data:image/png;base64,thumb') expect(stuck.remove).toHaveBeenCalledTimes(1) expect(next.loadModel).toHaveBeenCalledWith('/next.glb') + expect(reportError).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + errorType: 'load3d_thumbnail_generation_failed' + }) + ) + }) + + it('invalidates the loader before disposing on timeout, so a load that resolves afterward cannot publish', async () => { + vi.useFakeTimers() + const invalidate = vi.fn() + const order: string[] = [] + const stuck = mockInstance({ + loadModel: vi.fn(() => new Promise(() => {})), + remove: vi.fn(() => order.push('remove')), + getLoaderManager: vi.fn(() => ({ + invalidate: () => { + order.push('invalidate') + invalidate() + } + })), + captureThumbnail: vi.fn(() => { + // Would only run if the abandoned load were (incorrectly) awaited + // past the timeout; asserting it never fires is the regression + // check for the resource leak this fix closes. + throw new Error('must not capture a thumbnail for an invalidated load') + }) + }) + createLoad3d.mockReturnValueOnce(stuck) + + const stuckRun = generateModelThumbnail('/stuck.glb', 'stuck.glb') + await vi.waitFor(() => expect(createLoad3d).toHaveBeenCalledTimes(1)) + await vi.advanceTimersByTimeAsync(15_000) + + await expect(stuckRun).resolves.toBeNull() + expect(invalidate).toHaveBeenCalledTimes(1) + expect(stuck.remove).toHaveBeenCalledTimes(1) + expect(order).toEqual(['invalidate', 'remove']) + expect(stuck.captureThumbnail).not.toHaveBeenCalled() }) }) diff --git a/src/components/load3d/modelThumbnail.ts b/src/components/load3d/modelThumbnail.ts index fc874dd40fc..8df111e0905 100644 --- a/src/components/load3d/modelThumbnail.ts +++ b/src/components/load3d/modelThumbnail.ts @@ -1,9 +1,11 @@ import { withTimeout } from 'es-toolkit' +import type Load3d from '@/extensions/core/load3d/Load3d' import { isAssetPreviewSupported, persistThumbnail } from '@/platform/assets/utils/assetPreviewUtil' +import { reportError } from '@/platform/telemetry/reportError' let queue: Promise = Promise.resolve() const MODEL_LOAD_TIMEOUT_MS = 15_000 @@ -27,17 +29,24 @@ async function renderThumbnail( modelUrl: string, assetName: string ): Promise { + // Bounds the whole offscreen render (dynamic import, WebGL context + // creation, load, and capture), not just the load step — a stall in any + // of them would otherwise wedge `queue` (module-global, never reset) for + // the rest of the session, which is the exact head-of-line failure this + // module exists to prevent. + const holder: { current: Load3d | null } = { current: null } try { - const { createLoad3d } = - await import('@/extensions/core/load3d/createLoad3d') - const load3d = createLoad3d(document.createElement('div'), { - width: 256, - height: 256, - isViewerMode: true - }) - try { - await withTimeout(() => load3d.loadModel(modelUrl), MODEL_LOAD_TIMEOUT_MS) - const dataUrl = await load3d.captureThumbnail(256, 256) + return await withTimeout(async () => { + const { createLoad3d } = + await import('@/extensions/core/load3d/createLoad3d') + const instance = createLoad3d(document.createElement('div'), { + width: 256, + height: 256, + isViewerMode: true + }) + holder.current = instance + await instance.loadModel(modelUrl) + const dataUrl = await instance.captureThumbnail(256, 256) if (isAssetPreviewSupported()) { void fetch(dataUrl) .then((response) => response.blob()) @@ -45,10 +54,17 @@ async function renderThumbnail( .catch(() => {}) } return dataUrl - } finally { - load3d.remove() - } - } catch { + }, MODEL_LOAD_TIMEOUT_MS) + } catch (error) { + reportError(error, { errorType: 'load3d_thumbnail_generation_failed' }) return null + } finally { + // The timeout only rejects the wrapper — it cannot cancel whatever step + // is still in flight inside it. Invalidate the load before disposing so + // an abandoned `loadModel` that resolves afterward is a no-op (same + // staleness check that already guards overlapping loads on one + // instance) instead of calling setupModel() on a disposed manager. + holder.current?.getLoaderManager().invalidate() + holder.current?.remove() } } diff --git a/src/extensions/core/load3d/LoaderManager.ts b/src/extensions/core/load3d/LoaderManager.ts index a160297ae68..c6d715f33d4 100644 --- a/src/extensions/core/load3d/LoaderManager.ts +++ b/src/extensions/core/load3d/LoaderManager.ts @@ -81,6 +81,18 @@ export class LoaderManager implements LoaderManagerInterface { dispose(): void {} + /** + * Mark any in-flight `loadModel()` call as stale without waiting for it. + * Bumps `currentLoadId` so the abandoned load's own staleness check (the + * same one that already protects against overlapping `loadModel` calls) + * makes it a no-op on completion — it will neither publish its adapter nor + * call `modelManager.setupModel()`. Callers that give up on a load (e.g. a + * timeout) but keep this manager instance alive must call this first. + */ + invalidate(): void { + this.currentLoadId++ + } + async loadModel( url: string, originalFileName?: string,