Stories: a teardown that times out cannot undo the restored view

directorSettled now tells a timeout from settlement, and on a timeout
revokes the scene's layer cleanup still in flight before Close puts the
view back.
This commit is contained in:
Sameh Khamis committed 2026-11-16 13:31:51 -08:00
1 parent 0a39b05cdb
commit 4fa80fb18c
5 files changed
+105 -8

No files matched your search

+3 -1
View File
@@ -756,7 +756,9 @@ globe, including when it opens from a story link without a hash.
Close dismisses the Stories screens and keeps the keyboard and microphone
guards until the previous view is back. The director is waited
on until idle (`SceneDirector.whenIdle`: no run and no pending LOAD, seek or
interaction; at most 15 s). The camera and world clock are put back (a clock
interaction; at most 15 s). When the 15 s run out, the scene's layer cleanup
still under way is revoked (`SceneDirector.revokeSceneRelease`) before
anything is put back, so it cannot turn off a layer Close restores. The camera and world clock are put back (a clock
nothing moved while Stories was open, with no jump and the same mode and
rate, is left alone), unless a
camera owner took over during the wait (a drag on the globe, a layer's
+20 -3
View File
@@ -2683,13 +2683,19 @@ export class SceneDirector {
const layerIds = Array.isArray(scene?.releaseLayerIds)
? scene.releaseLayerIds
: [];
// revokeSceneRelease() ends this release's authority over layers.
this._releaseAbort ??= new AbortController();
const revoked = this._releaseAbort.signal;
const signal = token?.signal
? AbortSignal.any([token.signal, revoked])
: revoked;
let released = true;
for (const layerId of layerIds) {
if (token?.cancelled) return false;
if (token?.cancelled || revoked.aborted) return false;
try {
const settled = await this.dataManager.setEnabled(layerId, false, {
origin: 'scene',
signal: token?.signal,
signal,
});
if (settled === false) {
released = false;
@@ -2714,7 +2720,18 @@ export class SceneDirector {
});
}
}
return released && !token?.cancelled;
return released && !token?.cancelled && !revoked.aborted;
}
/**
* Stop every scene layer release still in flight from turning any further
* layer off: the one under way is aborted and the rest are skipped. For a
* caller that stopped waiting on a teardown (whenIdle) and is about to put
* layers back itself; a release started afterwards is unaffected.
*/
revokeSceneRelease() {
this._releaseAbort?.abort();
this._releaseAbort = null;
}
/**
+36
View File
@@ -2031,6 +2031,42 @@ test('startScene resolves its result, resumes after a shot without a scene id, a
} finally { await director.destroy(); restore(); }
});
test('a revoked scene release turns no further layer off', async () => {
const project = structuredClone(PROJECT_FIXTURE);
project.scenes[0].releaseLayerIds = ['flights', 'traffic'];
const { director, dataManager, restore } = makeDirector({ project });
try {
const calls = [];
let finishFirst;
dataManager.setEnabled = (id, enabled, { signal }) => {
calls.push({ id, enabled, signal });
// The first layer's disable hangs, as a stuck teardown does.
return new Promise((resolve) => {
finishFirst = resolve;
});
};
const releasing = director._releaseSceneLayers(director._project.scenes[0]);
await settle();
assert.deepEqual(calls.map(({ id }) => id), ['flights']);
director.revokeSceneRelease();
assert.equal(calls[0].signal.aborted, true, 'the disable under way is aborted');
finishFirst(true);
assert.equal(await releasing, false);
assert.deepEqual(
calls.map(({ id }) => id),
['flights'],
'the next layer is never turned off',
);
// A release started afterwards has its say again.
dataManager.setEnabled = async (id, enabled, { signal }) => {
calls.push({ id, enabled, signal });
return true;
};
assert.equal(await director._releaseSceneLayers(director._project.scenes[0]), true);
assert.deepEqual(calls.slice(1).map(({ id }) => id), ['flights', 'traffic']);
} finally { await director.destroy(); restore(); }
});
/** Drive frame-by-frame camera motion headlessly for one test. */
function installAnimationFrames() {
const original = globalThis.requestAnimationFrame;
+10 -3
View File
@@ -7,21 +7,28 @@ export const SETTLE_LIMIT_MS = 15000;
* Resolves once the director is idle: no run, and every LOAD, seek and
* interaction settled (SceneDirector.whenIdle). A teardown that never
* finishes is given SETTLE_LIMIT_MS, so a stuck scene cannot leave Stories
* unable to close; that case is logged.
* unable to close; that case is logged, and the scene's layer cleanup still
* in flight loses its say over layers (SceneDirector.revokeSceneRelease), so
* it cannot turn off a layer the caller puts back next.
* @param {import('../scenes/director.js').SceneDirector} director
* @returns {Promise<boolean>} True once idle, false when the limit ran out.
*/
export function directorSettled(director) {
let timer = 0;
const limit = new Promise((resolve) => {
timer = setTimeout(() => {
console.warn('Stories: the scene did not finish tearing down; going on');
resolve();
director.revokeSceneRelease?.();
resolve(false);
}, SETTLE_LIMIT_MS);
});
// A failure to wait counts as settled: the view still comes back.
const idle = Promise.resolve()
.then(() => director.whenIdle())
.catch(() => {});
.then(
() => true,
() => true,
);
return Promise.race([idle, limit]).finally(() => clearTimeout(timer));
}
+36 -1
View File
@@ -2,7 +2,11 @@
// to finish; a Pause, Seek or Destroy meanwhile must win.
import test from 'node:test';
import assert from 'node:assert/strict';
import { createGevPlayer } from './gevPlayer.js';
import {
createGevPlayer,
directorSettled,
SETTLE_LIMIT_MS,
} from './gevPlayer.js';
const story = {
sceneId: 'scene-1',
@@ -281,3 +285,34 @@ test('Play from a paused link goes on from the link’s time', async () => {
director.finishRun();
await playing;
});
test('a teardown that outlasts the limit is told apart, and loses its say over layers', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const warn = console.warn;
console.warn = () => {};
let revoked = 0;
const director = {
whenIdle: () => new Promise(() => {}),
revokeSceneRelease: () => revoked++,
};
try {
const settled = directorSettled(director);
await turns();
assert.equal(revoked, 0, 'nothing is revoked while waiting');
t.mock.timers.tick(SETTLE_LIMIT_MS);
assert.equal(await settled, false, 'a timeout is not settlement');
assert.equal(revoked, 1, 'the stale cleanup is revoked before going on');
} finally {
console.warn = warn;
}
});
test('a teardown that finishes settles without revoking anything', async () => {
let revoked = 0;
const director = {
whenIdle: async () => {},
revokeSceneRelease: () => revoked++,
};
assert.equal(await directorSettled(director), true);
assert.equal(revoked, 0);
});