几天后,我无法再启动 Puppeteer,直到我重新启动服务器

Posted

技术标签:

【中文标题】几天后,我无法再启动 Puppeteer,直到我重新启动服务器【英文标题】:After a few days, I can no longer start Puppeteer until I restart the server 【发布时间】:2021-12-21 19:18:27 【问题描述】: Puppeteer 版本:10.2.0 平台/操作系统版本:Ubuntu 20.04.3 LTS aarch64 Node.js 版本:v.14.17.6 发生在任何 URL 上。

这是我的测试脚本:

bug.hr/homepage.js

const puppeteer = require('puppeteer-extra');

(async () => 
    const eventPage = `https://www.bug.hr/`;
    const browser = await puppeteer.launch(
        headless: true,
        dumpio: true
    );
    const page = await browser.newPage();
    await page.goto(eventPage, 
        waitUntil: 'domcontentloaded'
    );
    console.log("TITLE: ", await page.evaluate(() => document.title));
    await browser.close();
)();

这应该启动 Puppeteer(在我重新启动服务器后它会启动),相反,我得到了这个:

ubuntu@ip-123-45-67-89:/var/www/tests$ env DEBUG="puppeteer:*" node bug.hr/homepage.js 
  puppeteer:launcher Calling /usr/bin/chromium-browser --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=Translate --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --enable-blink-features=IdleDetection --headless --hide-scrollbars --mute-audio about:blank --remote-debugging-port=0 --user-data-dir=/tmp/puppeteer_dev_chrome_profile-fvYjAc +0ms

DevTools listening on ws://127.0.0.1:42943/devtools/browser/5a94e4ce-f053-4e83-afdb-f44731fc185e
[1109/015336.602361:ERROR:devtools_http_handler.cc(292)] Error writing DevTools active port to file
[1109/015336.608627:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
[1109/015336.612599:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015336.613361:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015336.613265:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
  puppeteer:protocol:SEND ► "method":"Target.setDiscoverTargets","params":"discover":true,"id":1 +0ms
  puppeteer:protocol:RECV ◀ "method":"Target.targetCreated","params":"targetInfo":"targetId":"17591FCF231B151133561BADC2D1A20C","type":"page","title":"","url":"about:blank","attached":false,"canAccessOpener":false,"browserContextId":"3F175AC4C3C999720E0FFEFA74971974" +0ms
  puppeteer:protocol:RECV ◀ "method":"Target.targetCreated","params":"targetInfo":"targetId":"3066064f-b08d-429f-af6c-99c828566f63","type":"browser","title":"","url":"","attached":false,"canAccessOpener":false +1ms
  puppeteer:protocol:RECV ◀ "method":"Target.targetCreated","params":"targetInfo":"targetId":"7b188175-60bc-4608-b6a2-a83ed34381b0","type":"browser","title":"","url":"","attached":true,"canAccessOpener":false +0ms
  puppeteer:protocol:RECV ◀ "id":1,"result": +0ms
  puppeteer:protocol:SEND ► "method":"Target.createTarget","params":"url":"about:blank","id":2 +4ms
  puppeteer:protocol:RECV ◀ "method":"Target.targetCreated","params":"targetInfo":"targetId":"BD017C91F9A3803C4D1204C1F648B61E","type":"page","title":"","url":"","attached":false,"canAccessOpener":false,"browserContextId":"3F175AC4C3C999720E0FFEFA74971974" +4ms
  puppeteer:protocol:RECV ◀ "id":2,"result":"targetId":"BD017C91F9A3803C4D1204C1F648B61E" +0ms
[1109/015336.626616:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
  puppeteer:protocol:RECV ◀ "method":"Target.targetCrashed","params":"targetId":"17591FCF231B151133561BADC2D1A20C","status":"crashed","errorCode":133 +187ms
[1109/015336.827392:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015336.827421:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 1 time(s)
[1109/015336.834997:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
[1109/015336.838813:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015336.839148:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
  puppeteer:protocol:RECV ◀ "method":"Target.targetCrashed","params":"targetId":"BD017C91F9A3803C4D1204C1F648B61E","status":"crashed","errorCode":133 +49ms
[1109/015337.039942:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015337.039973:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 2 time(s)
[1109/015337.047216:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
[1109/015337.051080:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.051332:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.248703:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015337.248732:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 3 time(s)
[1109/015337.255892:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
[1109/015337.259819:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.260091:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.454161:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015337.454191:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 4 time(s)
[1109/015337.461741:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
[1109/015337.465935:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.466181:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.666248:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015337.666276:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 5 time(s)
[1109/015337.673524:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is swiftshader, ANGLE is 
[1109/015337.677539:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.677807:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.880581:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015337.880610:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 6 time(s)
[1109/015337.882645:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is disabled, ANGLE is 
[1109/015337.884399:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015337.884679:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015338.076122:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015338.076151:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 7 time(s)
[1109/015338.078253:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is disabled, ANGLE is 
[1109/015338.079942:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015338.080236:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015338.268891:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015338.268920:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 8 time(s)
[1109/015338.271026:ERROR:gpu_init.cc(453)] Passthrough is not supported, GL is disabled, ANGLE is 
[1109/015338.272679:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015338.272975:FATAL:sandbox_bpf.cc(283)] Kernel refuses to turn on BPF filters
[1109/015338.461377:ERROR:gpu_process_host.cc(979)] GPU process exited unexpectedly: exit_code=133
[1109/015338.461406:WARNING:gpu_process_host.cc(1292)] The GPU process has crashed 9 time(s)
[1109/015338.461419:FATAL:gpu_data_manager_impl_private.cc(417)] GPU process isn't usable. Goodbye.

我不能随意复制它。在服务器上运行 Puppeteer 几天后,它不会再启动了。它发生了两次。重新启动服务器后,它又可以工作了。

我相信罪魁祸首是 Kernel refuses to turn on BPF filters,来自日志。

/usr/bin/chromium-browser --product-version 给出 95.0.4638.69

有什么想法吗?

谢谢

【问题讨论】:

我会尝试使用 args: ['--disable-gpu'] 和一些类似的标志来启动。 @pguardiario 我试过了,没有帮助 【参考方案1】:

尝试在await puppeteer.launch() 中添加args: ['--no-sandbox'] 标志,因为考虑到错误FATAL:sandbox_bpf.cc(283),这似乎是一个沙盒问题

【讨论】:

我不想禁用沙箱。一旦它再次停止工作,我将进行测试。 这也可能与 linux 缺少 chromium 依赖有关。您可以在尝试禁用沙箱之前尝试检查一下。 这确实解决了它 - 谢谢。糟透了,我失去了沙盒的安全性,我想弥补它的最佳方法是将每个 Puppeteer 实例包装在自己的 VM 中。 仍然想了解根本原因...... 听了太棒了,就像我说的,我相信它与 linux 上的 chromium 依赖关系有关,但我不确定它是否也可能是其他东西,具体取决于您的设置。【参考方案2】:

尝试将 --single-process--no-zygote 添加到 args 中,类似这样

const browser = await puppeteer.launch(
    headless: true,
    args: ['--single-process', '--no-zygote']
)

【讨论】:

您好,谢谢您的帮助。我也知道这个论点,这与在没有沙箱的情况下运行 Puppeteer 非常相似。换句话说,它确实牺牲了安全性。 codereview.chromium.org/2384163002 这会因--no-sandbox should be used together with --no--zygote pupp 异常而崩溃【参考方案3】:

可以通过将 --disable-seccomp-filter-sandbox 参数传递给 Chromium 来修复。

这对安全性不利,因为它会部分禁用沙箱 (https://chromium.googlesource.com/chromium/src/+/HEAD/docs/linux/sandboxing.md#the-sandbox-1),但它是目前唯一适合我的解决方案,并且比禁用整个沙箱或 zygote 更好。

【讨论】:

以上是关于几天后,我无法再启动 Puppeteer,直到我重新启动服务器的主要内容,如果未能解决你的问题,请参考以下文章

Spring security - 身份验证在几天后停止工作 - 需要重新启动

Web API 在使用几天后总是无法连接到 MYSQL 数据库

安装了virtualbox几天后启动安卓平板模拟器后蓝屏?

无法在 Ubuntu 16.04 AWS EC2 实例上使用 puppeteer 启动 chromium headless

使用几天后应用程序无法打开[重复]

我重装了系统再装SQL Server 2005遇到了下面的一系列问题