如何强制 GPU 进程崩溃并恢复后继续测试?
解读
在国内一线互联网公司的面试现场,这道题常被用来区分“只会写用例”与“真正理解浏览器运行时”的候选人。
面试官真正想听的是:
- 你能否主动触发 GPU 进程崩溃(而非被动等待);
- 崩溃发生后,能否让 Cypress 用例自动续跑,并断言恢复后的页面状态;
- 整个过程要可重复、可落地到 CI,不能只是手动点两下。
知识点
- Chromium 启动参数:
--disable-gpu-sandbox、--enable-logging、--v=1等,用于把 GPU 进程暴露给外部信号。 - GPU 进程 PID 获取:通过 Chrome DevTools Protocol(CDP)
Target.getTargets+ProcessInfo.getProcessInfo拿到 GPU 进程 pid。 - 强制崩溃手段:
- Linux:向 GPU 进程发送
SIGKILL; - Windows:调用
taskkill /F /PID; - 容器内:直接
kill -9即可。
- Linux:向 GPU 进程发送
- Cypress 崩溃恢复钩子:
Cypress.on('window:confirmed', …)屏蔽“喔唷,崩溃啦”提示条;cy.window({ timeout: 0 }).should('have.property', 'clientReady', true)自定义恢复标记,确保页面已重建完成。
- CDP 重连:Cypress 12+ 内置
cy.CDP('Target.attachToTarget', …),崩溃后原 target 失效,需重新attach并更新Cypress.automation的 sessionId。 - CI 稳定性:
- 用非 headless 模式(GPU 进程存在);
- 在 GitHub Action / 阿里云 Flow 里加
--no-sandbox与sysctl -w kernel.core_pattern避免 coredump 卡死。
答案
下面给出一段可直接搬进面试白板、且能在 GitLab CI 跑通的示范代码,关键步骤已加粗。
// cypress/support/gpu-crash.js
const { exec } = require('child_process');
function crashGpuProcess() {
return cy.task('getGpuPid').then(pid => {
if (!pid) throw new Error('GPU 进程未找到');
return cy.task('killProcess', pid, { timeout: 5000 });
});
}
Cypress.Commands.add('crashGpuAndContinue', () => {
cy.log('**步骤1:触发 GPU 崩溃**');
crashGpuProcess();
cy.log('**步骤2:等待浏览器自动重启 GPU 进程**');
cy.window({ timeout: 30000 }).should(win => {
// 业务方在 window 上注入一个恢复标记
expect(win.clientReady).to.equal(true);
});
cy.log('**步骤3:重新附加 CDP 会话**');
cy.CDP('Target.getTargets').its('targetInfos').should('have.length.gt', 0)
.then(list => list[0].targetId)
.then(targetId => cy.CDP('Target.attachToTarget', { targetId, flatten: true }))
.then(({ sessionId }) => {
// 把新的 sessionId 写回 Cypress 内部,后续网络拦截继续生效
Cypress.automation('remote:debugger:protocol', { sessionId });
});
cy.log('**步骤4:断言页面功能正常**');
cy.get('#render-canvas').should('be.visible');
});
// plugins/index.js
module.exports = (on, config) => {
on('before:browser:launch', (browser, launchOptions) => {
if (browser.family === 'chromium') {
launchOptions.args.push('--disable-gpu-sandbox');
launchOptions.args.push('--enable-logging');
launchOptions.args.push('--v=1');
}
return launchOptions;
});
on('task', {
async getGpuPid() {
const { execSync } = require('child_process');
// 通过 chrome 日志抓取 GPU 进程 pid
const log = execSync('cat /tmp/chrome_debug.log | grep "GPU process" | tail -1').toString();
const m = log.match(/pid=(\d+)/);
return m ? parseInt(m[1], 10) : null;
},
killProcess(pid) {
return new Promise((resolve, reject) => {
exec(`kill -9 ${pid}`, err => err ? reject(err) : resolve(true));
});
}
});
};
用例层只需一行即可:
it('GPU 崩溃恢复后,WebGL 场景自动重绘', () => {
cy.visit('/3d-scene');
cy.crashGpuAndContinue();
cy.get('canvas').toMatchImageSnapshot(); // 视觉回归,确保渲染无黑屏
});
落地要点:
- 本地调试时打开
cypress open --browser chrome; - CI 里用
cypress run --browser chrome --config video=true保留录像,方便复盘; - 若跑在容器,需加
--ipc=host共享内存,否则 GPU 进程起不来。
拓展思考
- 如果页面使用了 OffscreenCanvas + WebWorker,GPU 崩溃后 Worker 线程也会连带被杀,恢复策略需要把 Worker 也重新实例化,并在主线程同步状态。
- 对于 Electron 应用,GPU 进程与主进程耦合更紧,可借助
app.getGPUInfo('complete')轮训,发现空值即触发重启,再用cy.wrap(require('electron').remote.app.relaunch())让 Cypress 重新 attach。 - 在政企内网场景,客户机可能安装国产双显卡切换驱动,崩溃后系统会自动回退到核显,此时需断言
navigator.gpu.wgpu.adapter.limits.maxTextureSize的数值变化,确保业务降级逻辑生效。