PuppeteerでNo usable sandbox! Update your kernelが出た時の対処法
Puppeteer を実行していて、いつの日か以下の警告が出ていることに気付く。
以前はこんなことがあった。

今回は以下のようなエラーが出ていてコケていた。
node_modules/@puppeteer/browsers/lib/cjs/launch.js:262
reject(new Error([
^Error: Failed to launch the browser process!
[1850:1850:0328/061014.661986:FATAL:zygote_host_impl_linux.cc(127)] No usable sandbox! Update your kernel or see https://chromium.googlesource.com/chromium/src/+/main/docs/linux/suid_sandbox_development.md for more information on developing with the SUID sandbox. If you want to live dangerously and need an immediate workaround, you can try using –no-sandbox.
調査
エラーから、ブラウザが起動できなかったということはわかる。
原因となりそうなところの筆頭は「Update your kernel」でしょうか。
Github Action で実行しているものだったので、ubunts のディストリビューションが変わったタイミングくらいからコケていた?
あとヒントとなりそうなのは「No usable sandbox」でしょうか。
原因と対策
エラーの内容に近いものが Issue にあったので参考に。
結果的に、Puppeteer に以下のトラブルシューティングが紹介されていました。
信頼できない Web コンテンツからホスト環境を保護するために、Chrome は複数のサンドボックス層を使用します。これを適切に機能させるには、まずホストを構成する必要があります。Chrome が使用できる適切なサンドボックスがない場合、エラー No usable sandbox! でクラッシュします。
Chrome で開くコンテンツを完全に信頼する場合は、–no-sandbox 引数を使用して Chrome を起動できます。
In order to protect the host environment from untrusted web content, Chrome uses multiple layers of sandboxing. For this to work properly, the host should be configured first. If there’s no good sandbox for Chrome to use, it will crash with the error No usable sandbox!.
If you absolutely trust the content you open in Chrome, you can launch Chrome with the –no-sandbox argument:
以前は以下のように browser を生成していましたが、
const browser = await puppeteer.launch({
headless: 'new',
});
サンドボックスモードを利用しないことを明示的に指定します。
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
これで無事に解決しました。