Files
openbot/scripts/linux-gpu-fallback-electron.ts
T
4607005848 Use the GPU on Linux until it fails on this computer (#2308)
* Use the GPU on Linux until it fails on this computer

#1046 turned off hardware acceleration for all Linux computers (#992), so
surfaces with a blurred backdrop render on the CPU also where the GPU works.

Hardware acceleration now stays on. A GPU process crash (gpu-crash.json) or
a start that stops before it is confirmed (gpu-start.json, for the SIGTRAP
case where main stops first) turns on software rendering at later starts
with the same Electron version. OPENBOT_DISABLE_GPU=1 or 0 overrides it.

scripts/linux-gpu-fallback-e2e.ts runs the real Electron and crashes the
real GPU process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep software rendering on Linux servers, and key GPU failures on the Chromium major version

- A server (`OPENBOT_SERVER=1` or a hosted server) runs under Xvfb with no GPU. It keeps software
  rendering, as before #2308, and writes no start file.
- A failure record keeps the Chromium major version, not the Electron version. An Electron patch
  release keeps Chromium's major version, so it does not make a failing computer stop again.
- Only `configureLinuxGpu` and its option and result types are exported (knip).
- The E2E test has a server step. docs/TROUBLESHOOTING.md tells how to try the GPU again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Confirm the GPU start when a quit starts, not after the teardown

`will-quit` fires only after the teardown. The teardown can take longer than a stop allows: the
shutdown deadline ends with `app.exit`, and `dev:stop` sends SIGKILL after a few seconds. Then a
normal quit in the first 30 s left `gpu-start.json`, and later starts used software rendering.
`before-quit` fires when the quit starts.

A second instance can read the start file of a first instance that is still new. It no longer logs
that the GPU failed before.

The E2E test has a step that kills the app during a teardown that does not end. With `will-quit`,
this step fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Keep the GPU on Linux after a GPU context loss

When the GPU context is lost, for example after suspend and resume, Chromium stops the GPU process on
purpose with exit code 34 (`viz::ExitCode::RESULT_CODE_GPU_EXIT_ON_CONTEXT_LOST`) and starts it
again. Electron reports this exit as `abnormal-exit`, so one context loss wrote `gpu-crash.json`, and
all later starts used software rendering until a new Chromium major version. This exit no longer
counts as a GPU failure.

The E2E test sends this exit to the handler and checks that no crash file is written. Without the
fix, this step fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Natan Pimentel <natanpimentel@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: NB😈 <norbertbodziony@gmail.com>
2026-10-16 19:25:59 +02:00

57 lines
2.5 KiB
TypeScript

// Electron side of `scripts/linux-gpu-fallback-e2e.ts`. It starts the GPU mode as `src/main/index.ts`
// does and opens a window with a blurred backdrop.
// It writes the state to `--ready-file` when the page has loaded. A "quit" line or the end of stdin
// quits the app, so the app also quits when the test stops.
import { writeFileSync } from "node:fs";
import { app, BrowserWindow } from "electron";
import { configureLinuxGpu } from "../src/main/linux-gpu-mode";
function argument(name: string): string {
const value = process.argv.find((item) => item.startsWith(`--${name}=`));
if (!value) throw new Error(`Missing --${name}=`);
return value.slice(name.length + 3);
}
// A closed output must not stop the test app with an error dialog.
process.stdout.on("error", () => undefined);
process.stderr.on("error", () => undefined);
const userDataPath = argument("user-data");
app.setPath("userData", userDataPath);
const mode = configureLinuxGpu({
userDataPath,
ownsInstance: true,
server: process.argv.includes("--server"),
startConfirmMs: Number(argument("confirm-ms")),
});
// `--hang-on-quit`: a teardown that does not end, as when `dev:stop` or the shutdown deadline stops it.
if (process.argv.includes("--hang-on-quit")) app.on("before-quit", (event) => event.preventDefault());
// No top-level await: Electron does not get ready until an ESM main module has run to its end.
void app.whenReady().then(async () => {
// `--context-lost`: the exit of a GPU process that Chromium restarts after a GPU context loss.
// A test cannot make the driver lose the context, so it sends the event that Electron sends.
if (process.argv.includes("--context-lost")) {
app.emit("child-process-gone", {}, { type: "GPU", reason: "abnormal-exit", exitCode: 34 });
}
const window = new BrowserWindow({ width: 480, height: 320 });
await window.loadURL(
`data:text/html,${encodeURIComponent(
"<body style='margin:0;background:linear-gradient(45deg,#f60,#06f)'>" +
"<div style='margin:40px;height:200px;backdrop-filter:blur(24px)'>GPU fallback</div></body>",
)}`,
);
writeFileSync(
argument("ready-file"),
JSON.stringify({
pid: process.pid,
mode,
hardwareAccelerationEnabled: app.isHardwareAccelerationEnabled(),
gpuCompositing: app.getGPUFeatureStatus().gpu_compositing,
}),
);
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk: string) => {
if (chunk.includes("quit")) app.quit();
});
process.stdin.on("end", () => app.quit());
});