Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(sdk): scene load-failure recovery + cleanup-throw safety (audit A18)

esengine9b64b05371d3
diff --git a/sdk/src/sceneManager.ts b/sdk/src/sceneManager.ts
index 1927008d5..b45799ecb 100644
--- a/sdk/src/sceneManager.ts
+++ b/sdk/src/sceneManager.ts
@@ -308,6 +308,9 @@ export class SceneManagerState {
this.loadPromises_.set(name, loadPromise);
try {
return await loadPromise;
+ } catch (err) {
+ this.rollbackFailedLoad_(name, instance);
+ throw err;
} finally {
this.loadPromises_.delete(name);
}
@@ -360,11 +363,31 @@ export class SceneManagerState {
this.loadPromises_.set(name, loadPromise);
try {
return await loadPromise;
+ } catch (err) {
+ this.rollbackFailedLoad_(name, instance);
+ throw err;
} finally {
this.loadPromises_.delete(name);
}
}
+ /**
+ * Roll back a load that threw partway. Without this, the half-registered
+ * instance stays in `scenes_` stuck at `status==='loading'` while its
+ * `loadPromise` is deleted in `finally` — so a retry hits the
+ * `status==='loading'` branch and returns the now-undefined loadPromise,
+ * wedging the scene so it can NEVER be loaded again. We also despawn any
+ * entities `setup`/`loadSceneData_` spawned before the throw, to avoid a leak.
+ */
+ private rollbackFailedLoad_(name: string, instance: SceneInstance): void {
+ for (const entity of instance.entities) {
+ if (this.app_.world.valid(entity)) this.app_.world.despawn(entity);
+ }
+ instance.entities.clear();
+ this.scenes_.delete(name);
+ this.contexts_.delete(name);
+ }
+
async unload(name: string, options?: TransitionOptions): Promise<void> {
const instance = this.scenes_.get(name);
if (!instance) return;
@@ -373,7 +396,13 @@ export class SceneManagerState {
instance.status = 'unloading';
if (instance.config.cleanup) {
- instance.config.cleanup(ctx);
+ try {
+ instance.config.cleanup(ctx);
+ } catch (err) {
+ // A throwing user cleanup must NOT abort the teardown below —
+ // that would leak every entity/system/texture the scene owns.
+ log.error('scene', `Scene "${name}" cleanup callback threw; continuing teardown`, err);
+ }
}
const keepPersistent = options?.keepPersistent ?? true;
diff --git a/sdk/tests/scene-load-failure.test.ts b/sdk/tests/scene-load-failure.test.ts
new file mode 100644
index 000000000..6b9350384
--- /dev/null
+++ b/sdk/tests/scene-load-failure.test.ts
@@ -0,0 +1,167 @@
+/**
+ * @file scene-load-failure.test.ts
+ * @brief Regression net for the audit's A18 (scene lifecycle robustness):
+ * A18a — a load that throws partway must NOT wedge the scene (a retry
+ * has to start fresh, not resolve to undefined forever), and any
+ * entities spawned before the throw must be cleaned up.
+ * A18b — a user `cleanup` callback that throws must NOT abort unload's
+ * entity teardown (which would leak the scene's entities).
+ */
+import { describe, expect, it, beforeEach, vi } from 'vitest';
+
+vi.mock('../src/scene', () => ({
+ loadSceneWithAssets: vi.fn().mockResolvedValue(new Map()),
+}));
+vi.mock('../src/customDraw', () => ({
+ registerDrawCallback: vi.fn(),
+ unregisterDrawCallback: vi.fn(),
+}));
+vi.mock('../src/postprocess', () => ({
+ PostProcess: { bind: vi.fn(), unbind: vi.fn() },
+ PostProcessStack: vi.fn(),
+}));
+vi.mock('../src/material', () => ({
+ Material: { release: vi.fn(), createShader: vi.fn() },
+ defineResource: vi.fn(),
+}));
+vi.mock('../src/asset', () => ({
+ Assets: {
+ releaseTexture: vi.fn(),
+ releaseFont: vi.fn(),
+ releaseMaterial: vi.fn(),
+ releaseAll: vi.fn(),
+ },
+ discoverSceneAssets: vi.fn().mockReturnValue({ textures: [], fonts: [], spines: [], audios: [], unresolved: [] }),
+ preloadSceneAssets: vi.fn().mockResolvedValue({ loadedTextures: new Set(), loadedFonts: new Set(), loadedMaterials: new Set(), missing: [] }),
+}));
+
+import { SceneManagerState } from '../src/sceneManager';
+
+const SCENE_DATA = { version: '1.0', name: 'Test', entities: [] };
+
+function createMockApp() {
+ const entities = new Map<number, Map<symbol, any>>();
+ let nextEntity = 1;
+ const resources = new Map<any, any>();
+
+ const world = {
+ spawn: vi.fn(() => {
+ const e = nextEntity++;
+ entities.set(e, new Map());
+ return e;
+ }),
+ despawn: vi.fn((e: number) => entities.delete(e)),
+ valid: vi.fn((e: number) => entities.has(e)),
+ has: vi.fn((e: number, comp: symbol) => entities.get(e)?.has(comp) ?? false),
+ get: vi.fn((e: number, comp: symbol) => entities.get(e)?.get(comp)),
+ insert: vi.fn((e: number, comp: symbol, data: any) => {
+ if (!entities.has(e)) entities.set(e, new Map());
+ entities.get(e)!.set(comp, data);
+ }),
+ set: vi.fn((e: number, comp: symbol, data: any) => {
+ if (!entities.has(e)) entities.set(e, new Map());
+ entities.get(e)!.set(comp, data);
+ }),
+ remove: vi.fn((e: number, comp: symbol) => {
+ entities.get(e)?.delete(comp);
+ }),
+ };
+
+ return {
+ world,
+ liveEntityCount: () => entities.size,
+ hasResource: vi.fn((key: any) => resources.has(key)),
+ getResource: vi.fn((key: any) => resources.get(key)),
+ addSystemToSchedule: vi.fn(),
+ };
+}
+
+describe('SceneManager load-failure recovery (A18a)', () => {
+ let app: ReturnType<typeof createMockApp>;
+ let manager: SceneManagerState;
+
+ beforeEach(() => {
+ app = createMockApp();
+ manager = new SceneManagerState(app as any);
+ });
+
+ it('a scene that fails to load can be retried — it does not wedge forever', async () => {
+ let attempts = 0;
+ manager.register({
+ name: 'flaky',
+ data: SCENE_DATA,
+ setup: () => { attempts++; if (attempts === 1) throw new Error('transient load failure'); },
+ });
+
+ // First attempt fails...
+ await expect(manager.load('flaky')).rejects.toThrow(/transient load failure/);
+
+ // ...and a retry must start FRESH and succeed. Before the fix the stuck
+ // status==='loading' instance (whose loadPromise was deleted) made every
+ // retry resolve to `undefined`, so the scene could never be loaded again.
+ const ctx = await manager.load('flaky');
+ expect(ctx).toBeDefined();
+ expect(ctx.name).toBe('flaky');
+ expect(attempts).toBe(2);
+ });
+
+ it('loadAdditive has the same retry recovery', async () => {
+ let attempts = 0;
+ manager.register({
+ name: 'flaky-add',
+ data: SCENE_DATA,
+ setup: () => { attempts++; if (attempts === 1) throw new Error('transient'); },
+ });
+
+ await expect(manager.loadAdditive('flaky-add')).rejects.toThrow(/transient/);
+ const ctx = await manager.loadAdditive('flaky-add');
+ expect(ctx).toBeDefined();
+ expect(attempts).toBe(2);
+ });
+
+ it('despawns entities spawned before the throw (no orphan leak)', async () => {
+ const spawned: number[] = [];
+ manager.register({
+ name: 'partial',
+ data: SCENE_DATA,
+ setup: (ctx) => {
+ spawned.push(ctx.spawn(), ctx.spawn());
+ throw new Error('failed after spawning');
+ },
+ });
+
+ await expect(manager.load('partial')).rejects.toThrow(/failed after spawning/);
+
+ expect(spawned).toHaveLength(2);
+ for (const e of spawned) expect(app.world.despawn).toHaveBeenCalledWith(e);
+ expect(app.liveEntityCount()).toBe(0); // nothing left orphaned
+ });
+});
+
+describe('SceneManager unload teardown robustness (A18b)', () => {
+ let app: ReturnType<typeof createMockApp>;
+ let manager: SceneManagerState;
+
+ beforeEach(() => {
+ app = createMockApp();
+ manager = new SceneManagerState(app as any);
+ });
+
+ it('still despawns the scene entities when a cleanup callback throws', async () => {
+ const spawned: number[] = [];
+ manager.register({
+ name: 'leaky',
+ data: SCENE_DATA,
+ setup: (ctx) => { spawned.push(ctx.spawn(), ctx.spawn()); },
+ cleanup: () => { throw new Error('cleanup blew up'); },
+ });
+ await manager.load('leaky');
+ expect(app.liveEntityCount()).toBe(2);
+
+ // A throwing cleanup must not abort teardown (which would leak entities).
+ await expect(manager.unload('leaky')).resolves.toBeUndefined();
+
+ for (const e of spawned) expect(app.world.despawn).toHaveBeenCalledWith(e);
+ expect(app.liveEntityCount()).toBe(0);
+ });
+});