bug(hosting): A host destroyed while its scene loads unconfigures its replacement's canvas

Offen
#14 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
50/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
typescript

Rechercherichtung

Start by reading the canvas surface release path and the web.host creation and destroy lifecycle, then reproduce the reported sequence with a pending createScene and two hosts sharing one canvas. The issue proposes guarding unconfigure() so an old surface cannot affect its replacement, and optionally aborting scene creation on destroy; done means the replacement can draw after the old load resolves, with any cancellation behavior covered.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Problem

destroy() is meant to be called from import.meta.hot.dispose or an effect cleanup, and "releases everything in order, even while the scene is starting". When destroy arrives while createScene is still pending, it is queued until creation resolves. createScene is never told, so the old scene keeps loading on its own. When it finally resolves, release destroys its canvas surface, which calls context.unconfigure().

A canvas has only one GPUCanvasContext. If a replacement host configured that canvas in the meantime, the replacement's next getCurrentTexture() throws and its scene stops. Any scene that awaits assets or compiles can hit this when it is re-hosted on the same canvas while it loads: Vite HMR, React StrictMode's mount, unmount and remount, or a route change and back.

const first = web.host(async () => {
  await slowLoad(); // e.g. models, pipelines.compile
  return { frame() {} };
}, { canvas });
first.destroy(); // HMR dispose / effect cleanup, while slowLoad is pending

const second = web.host((root, surface) => ({
  frame(frame) { surface.view(frame); },
}), { canvas });
// slowLoad resolves -> first's release unconfigures the shared context
// -> second's first frame: getCurrentTexture() on an unconfigured context, scene stops

Reproduced on 0.1.0 with web.host on recording roots and a single-context canvas stand-in:

configure                 // first host
first.destroy()           // while createScene is pending
configure                 // second host, same canvas
first scene created       // the destroyed load still completes
unconfigure               // first's release
second: getCurrentTexture on an unconfigured context (0 frames drawn)
Proposal
  1. Fix, no API change. A canvas surface unconfigures its context only if it is still the last kit surface configured on that canvas, e.g. a module-level WeakMap<HTMLCanvasElement, Surface>.
  2. Opt-in cancellation. createScene gets a fourth argument whose signal aborts when the host or stage is destroyed, or the watched device fails, before creation resolves. The destroyed load can then stop downloading and compiling instead of running to completion on its own:
export type CreateScene<Options, S extends Scene> = (
  root: TgpuRoot,
  surface: Surface,
  options: Options,
  context: { readonly signal: AbortSignal },
) => S | Promise<S>;

export async function createScene(root, surface, options, { signal }) {
  const bytes = await fetch(url, { signal });
  // ...
}

On a React Native worklet runtime without AbortController, the context could offer a cancelled getter instead.

Alternatives
  • User land. Keep an AbortController beside the host, abort it before destroy, and pass it in through the options. That covers part 2 but not part 1, since release runs inside web.host.
  • Wait for the old release before hosting again. That needs a promise from destroy, and HMR disposes and React cleanups are synchronous.
Cost when not used

Part 1 adds one WeakMap lookup per surface destroy. Part 2 adds one AbortController per host; existing createScene functions ignore the extra argument.

Vorherrschende Sprache
TypeScript
Sterne
69
Forks
7
Ø Merge
1 Std. 48 Min.
Gemergte PRs (30 T.)
15

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus software-mansion/typegpu-kit

Alle Issues in software-mansion/typegpu-kit

Ähnliche Issues

Weitere Issues zu TypeScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.