Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Renderer: dispose on a lost context no longer throws (closes #541)

Nguyen Xuan An772f0f7e9b3a
diff --git a/changes/541.md b/changes/541.md
new file mode 100644
index 00000000..e6a3c56c
--- /dev/null
+++ b/changes/541.md
@@ -0,0 +1 @@
+- Fixed: disposing a renderer right after its WebGL context was lost (for example stopping a docs live example before its shaders finish) no longer throws `Shader compilation failed: null`. `finishProgram` deletes the program and returns quietly when the context is lost (#541).
diff --git a/changes/541.size.md b/changes/541.size.md
new file mode 100644
index 00000000..1e2f8dc7
--- /dev/null
+++ b/changes/541.size.md
@@ -0,0 +1,5 @@
+- entry:core: +17 bytes gzip (80937 to 80954) for the lost-context check in `finishProgram`, which stops `dispose` from throwing `Shader compilation failed: null` and skipping the rest of its cleanup (#541). Accepted by the lead under the trade-off rule (a fixed glitch is worth a few bytes); the first try in `renderer.js` cost 51 bytes.
+- scene:lit-box: +20 bytes gzip, the same check.
+- goal:lit-box: +20 bytes gzip over a goal it already misses, the same check; accepted by the lead on 2026-11-07 as a bug fix.
+- goal:gltf-box: +14 bytes gzip, the same check; accepted by the lead on 2026-11-07 as a bug fix.
+- goal:agent-scene: +14 bytes gzip, the same check; accepted by the lead on 2026-11-07 as a bug fix.
diff --git a/src/gl-program.js b/src/gl-program.js
index e935f4ad..1b0904a4 100644
--- a/src/gl-program.js
+++ b/src/gl-program.js
@@ -34,6 +34,8 @@ export function finishProgram(context, { program, shaders }) {
shaders.forEach((shader) => context.deleteShader(shader));
if (!linked) {
context.deleteProgram(program);
+ // On a lost context every link reports false with a null log; the context is gone, so there is nothing to report (#541).
+ if (context.isContextLost()) return program;
throw new Error(failedShader ? `Shader compilation failed: ${message}` : `Shader linking failed: ${message}`);
}
return program;
diff --git a/tests/gl-program-lost-context.test.js b/tests/gl-program-lost-context.test.js
new file mode 100644
index 00000000..a1e0b17f
--- /dev/null
+++ b/tests/gl-program-lost-context.test.js
@@ -0,0 +1,32 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { finishProgram } from '../src/gl-program.js';
+
+function fakeContext({ lost }) {
+ const deleted = [];
+ return {
+ deleted,
+ LINK_STATUS: 0x8b82,
+ COMPILE_STATUS: 0x8b81,
+ getProgramParameter: () => false,
+ getShaderParameter: () => false,
+ getShaderInfoLog: () => null,
+ getProgramInfoLog: () => null,
+ deleteShader: (shader) => deleted.push(shader),
+ deleteProgram: (program) => deleted.push(program),
+ isContextLost: () => lost,
+ };
+}
+
+test('finishProgram on a lost context deletes the program and throws nothing (#541)', () => {
+ const context = fakeContext({ lost: true });
+ const pending = { program: 'program', shaders: ['vertex', 'fragment'] };
+ assert.doesNotThrow(() => finishProgram(context, pending));
+ assert.deepEqual(context.deleted.sort(), ['fragment', 'program', 'vertex']);
+});
+
+test('finishProgram on a live context still reports a failed compile', () => {
+ const context = fakeContext({ lost: false });
+ context.getShaderInfoLog = () => 'ERROR: 0:1: syntax error';
+ assert.throws(() => finishProgram(context, { program: 'program', shaders: ['vertex'] }), /Shader compilation failed: ERROR/);
+});