heroku / heroku-buildpack-chrome-for-testing

Heroku Buildpack that installs Chrome for Testing
19 stars 17 forks source link

Python pyppeteer no GOOGLE_CHROME_SHIM variable. #23

Open Kwieeciol opened 4 months ago

Kwieeciol commented 4 months ago

Good morning, as I upgraded my stack on heroku I've noticed a message telling me to use this buildpack rather than the older buildpacks I've used. Logically I proceeded to install this buildpack per the instructions. selenium.webdriver works fine, but pyppeteer doesn't work at all, this is the error I've encountered:

2024-07-24T19:38:13.917255+00:00 app[worker.1]: [INFO] Starting Chromium download.
2024-07-24T19:38:17.444891+00:00 app[worker.1]:
2024-07-24T19:38:17.446137+00:00 app[worker.1]: [INFO] Beginning extraction
2024-07-24T19:38:20.286229+00:00 app[worker.1]: [INFO] Chromium extracted to: /app/.local/share/pyppeteer/local-chromium/588429        
2024-07-24T19:38:50.317178+00:00 app[worker.1]: [2024-07-24 19:38:50] [ERROR   ] discord.ext.tasks: Unhandled exception in internal background task '_amex_experiences'.
2024-07-24T19:38:50.317187+00:00 app[worker.1]: Traceback (most recent call last):
2024-07-24T19:38:50.317188+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/ext/tasks/__init__.py", 
line 239, in _loop
2024-07-24T19:38:50.317188+00:00 app[worker.1]: await self.coro(*args, **kwargs)
2024-07-24T19:38:50.317189+00:00 app[worker.1]: File "/app/cogs/tasks.py", line 205, in _amex_experiences
2024-07-24T19:38:50.317189+00:00 app[worker.1]: await amex_experiences.run_experiences(self.bot.pool, self.bot)
2024-07-24T19:38:50.317189+00:00 app[worker.1]: File "/app/amex_experiences.py", line 82, in run_experiences
2024-07-24T19:38:50.317190+00:00 app[worker.1]: browser = await launch({
2024-07-24T19:38:50.317190+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/pyppeteer/launcher.py", line 307, in launch
2024-07-24T19:38:50.317190+00:00 app[worker.1]: return await Launcher(options, **kwargs).launch()
2024-07-24T19:38:50.317190+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/pyppeteer/launcher.py", line 168, in launch
2024-07-24T19:38:50.317191+00:00 app[worker.1]: self.browserWSEndpoint = get_ws_endpoint(self.url)
2024-07-24T19:38:50.317191+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/pyppeteer/launcher.py", line 227, in get_ws_endpoint
2024-07-24T19:38:50.317191+00:00 app[worker.1]: raise BrowserError('Browser closed unexpectedly:\n')
2024-07-24T19:38:50.317192+00:00 app[worker.1]: pyppeteer.errors.BrowserError: Browser closed unexpectedly:
2024-07-24T19:38:50.317192+00:00 app[worker.1]:
2024-07-24T19:38:50.317201+00:00 app[worker.1]: [2024-07-24 19:38:50] [ERROR   ] discord.ext.tasks: Unhandled exception in internal background task '_amex_experiences'.
2024-07-24T19:38:50.317201+00:00 app[worker.1]: Traceback (most recent call last):
2024-07-24T19:38:50.317201+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/discord/ext/tasks/__init__.py", line 239, in _loop
2024-07-24T19:38:50.317201+00:00 app[worker.1]: await self.coro(*args, **kwargs)
2024-07-24T19:38:50.317202+00:00 app[worker.1]: File "/app/cogs/tasks.py", line 205, in _amex_experiences
2024-07-24T19:38:50.317202+00:00 app[worker.1]: await amex_experiences.run_experiences(self.bot.pool, self.bot)
2024-07-24T19:38:50.317202+00:00 app[worker.1]: File "/app/amex_experiences.py", line 82, in run_experiences
2024-07-24T19:38:50.317202+00:00 app[worker.1]: browser = await launch({
2024-07-24T19:38:50.317202+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/pyppeteer/launcher.py", line 307, in launch
2024-07-24T19:38:50.317203+00:00 app[worker.1]: return await Launcher(options, **kwargs).launch()
2024-07-24T19:38:50.317203+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/pyppeteer/launcher.py", line 168, in launch
2024-07-24T19:38:50.317203+00:00 app[worker.1]: self.browserWSEndpoint = get_ws_endpoint(self.url)
2024-07-24T19:38:50.317203+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.10/site-packages/pyppeteer/launcher.py", line 227, in get_ws_endpoint
2024-07-24T19:38:50.317203+00:00 app[worker.1]: raise BrowserError('Browser closed unexpectedly:\n')
2024-07-24T19:38:50.317203+00:00 app[worker.1]: pyppeteer.errors.BrowserError: Browser closed unexpectedly:

I've tried all solutions available on StackOverflow, like installing the puppeteer buildpack, that doesn't work though, I've added the --disable-setuid-sandbox argument to pyppeteer, doesn't work either.

MRE:

from pyppeteer import launch
import asyncio
import os

exec_path = os.environ.get("GOOGLE_CHROME_SHIM", None)

async def main():
    await launch({
        "headless": True,
        "args": {
            "--no-sandbox", "--disable-setuid-sandbox"
        },
        "executablePath": exec_path
    })

asyncio.run(main())
lucafrollo commented 4 months ago

Are you using JMalloc?

When I upgraded from heroku-buildpack-google-chrome to this buildpack, puppeteer stopped working no matter which shims I used. I copied the four flags used by the previous buildpack --headless --no-sandbox --disable-gpu --remote-debugging-port=9222 but I still got errors where Chrome was quitting on launch.

The only thing that fixed was to disable JMalloc for node, by setting this env variable for Node as described here (we use Grover)

I'm not sure why the other Chrome buildpack does not have this issue.

Kwieeciol commented 4 months ago

I'm not using JMalloc