[WebGPU] A cached `ComputeEffect` ignores later callers' `onError` and keeps the first caller's callbacks forever

Open
#19,131 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start in Engines/WebGPU/Extensions/engine.computeShader.pure.js at the cache-hit branch of createComputeEffect (around lines 170-182), then read the onCompiled/onError hand-off in Compute/computeShader.pure.js (around lines 260-265). Run the repro with two ComputeShaders using the same invalid source. Done means the second shader's onError fires, and the cached effect no longer holds the first caller's callbacks.

Written by the indexing model from the issue text.

Description

Summary

WebGPUEngine.createComputeEffect returns an existing ComputeEffect when the key (source + defines) matches. On that cache hit it:

  • ignores the new caller's onError, and calls onCompiled only if the effect is already ready;
  • leaves the cached effect holding the first caller's onCompiled and onError.

So:

  • A second ComputeShader with the same source never hears about a compilation error. Its onError is silently dropped.
  • Whatever the first caller's callbacks capture stays reachable for the engine's lifetime. ComputeShader has no dispose(), and nothing ever removes entries from _compiledComputeEffects except releaseComputeEffects().

Where

  • Engines/WebGPU/Extensions/engine.computeShader.pure.js:170-182, in createComputeEffect:
    if (this._compiledComputeEffects[name]) {
        const compiledEffect = this._compiledComputeEffects[name];
        if (options.onCompiled && compiledEffect.isReady()) {
            options.onCompiled(compiledEffect);
        }
        return compiledEffect;      // options.onError is ignored; the first caller's callbacks stay on the effect
    }
    
  • Compute/computeShader.pure.js:260-265: ComputeShader passes onCompiled: this.onCompiled, onError: this.onError straight into createComputeEffect.

Repro (Playground, WebGPU: https://playground.babylonjs.com/?webgpu#GK5KU8#1)

const source = "@compute @workgroup_size(1) fn main() { let x : f32 = notDefinedAnywhere; }";   // invalid on purpose
const calls = [];
const make = (label) => {
  const cs = new BABYLON.ComputeShader(label, engine, { computeSource: source }, { bindingsMapping: {} });
  cs.onError = () => calls.push(label);
  return cs;
};
const first = make("first");  first.isReady();
// ...wait for the compile result...
const second = make("second"); second.isReady();
// expected: calls = ["first", "second"]
// actual:   calls = ["first"]; second._effect === first._effect, and that effect's onError === first.onError

Measured in my game

Our foliage culling used shader.onError = (effect, error) => { this.failure = error; }. Through the cached effect's onError closure, that kept the first stage's batch object, and with it that stage's whole Scene, alive for the session. Heap-snapshot retainer path: WebGPUEngine._compiledComputeEffects[...] → ComputeEffect.onError → closure context → ComputeFoliageBatch.scene → Scene.

Suggested fix

  • On a cache hit, attach the caller's callbacks for that caller only. For example, register them on onCompileObservable and onErrorObservable (call onError immediately if the effect already failed) instead of relying on the first caller's options.
  • Don't keep per-caller callbacks on the shared cached effect.
  • Optionally give ComputeShader a dispose() that removes its observers.

Workaround we use

Build onError handlers at module scope so the closure captures only a WeakRef to its owner. Read the compile error from the effect when needed rather than relying on a per-shader onError.

Related

No issue, PR or forum thread found. PR #18955 "Add on error on EffectWrapper" (merged 2026-09-30) is adjacent, adding onError for EffectWrapper rather than compute effects.

Dominant language
TypeScript
Stars
26.6k
Forks
3.8k
Avg merge
1d 9h
Merged PRs (30d)
112

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from BabylonJS/Babylon.js

All issues in BabylonJS/Babylon.js

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.