miyakogi / pyppeteer

Headless chrome/chromium automation library (unofficial port of puppeteer)
Other
3.57k stars 371 forks source link

Possible race condition while launching chrome? #117

Closed devxpy closed 6 years ago

devxpy commented 6 years ago
launch_kwargs = dict(
        args=[
            "--use-fake-ui-for-media-stream",
            "--allow-insecure-localhost",
            "--no-sandbox",
            "--disable-setuid-sandbox",
        ],
        dumpio=True,
        executablePath=subprocess.check_output(
            ["which", "chromium-browser"], encoding="utf-8"
        ).strip()
)

browser = await pyppeteer.launch(**launch_kwargs)
page = await browser.newPage()
await page.goto(f"http://localhost:{SERVER_PORT}/peerjs.html")
...
27.07.18 06:36:12 (+0530) [0727/010612.963133:ERROR:browser_main_loop.cc(670)] Failed to put Xlib into threaded mode.
27.07.18 06:36:13 (+0530) [0727/010613.047240:WARNING:discardable_shared_memory_manager.cc(186)] Less than 64MB of free space in temporary directory for shared memory files: 63
27.07.18 06:36:13 (+0530) Process Process-3:
27.07.18 06:36:13 (+0530) Traceback (most recent call last):
27.07.18 06:36:13 (+0530)   File "/.pyenv/versions/3.6.6/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
27.07.18 06:36:13 (+0530)     self.run()
27.07.18 06:36:13 (+0530) [0727/010613.381678:ERROR:gpu_process_transport_factory.cc(1029)] Lost UI shared context.
27.07.18 06:36:13 (+0530)   File "/.pyenv/versions/3.6.6/lib/python3.6/multiprocessing/process.py", line 93, in run
27.07.18 06:36:13 (+0530)     self._target(*self._args, **self._kwargs)
27.07.18 06:36:13 (+0530)   File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/zproc/zproc.py", line 536, in _child_process
27.07.18 06:36:13 (+0530)     process.target(state, *target_args, **target_kwargs)
27.07.18 06:36:13 (+0530)   File "/usr/src/app/iwakie/services/start_call.py", line 6, in mainloop
27.07.18 06:36:13 (+0530)     web_rtc_call = WebRtcCall()
27.07.18 06:36:13 (+0530)   File "/usr/src/app/iwakie/core/peerjs.py", line 73, in __init__
27.07.18 06:36:13 (+0530)     self.connect()
27.07.18 06:36:13 (+0530)   File "/usr/src/app/iwakie/core/peerjs.py", line 78, in connect
27.07.18 06:36:13 (+0530)     self.page = loop.run_until_complete(init_chrome_and_peer())
27.07.18 06:36:13 (+0530)   File "/.pyenv/versions/3.6.6/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
27.07.18 06:36:13 (+0530)     return future.result()
27.07.18 06:36:13 (+0530)   File "/usr/src/app/iwakie/core/peerjs.py", line 34, in init_chrome_and_peer
27.07.18 06:36:13 (+0530)     browser = await pyppeteer.launch(**launch_kwargs)
27.07.18 06:36:13 (+0530)   File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pyppeteer/launcher.py", line 251, in launch
27.07.18 06:36:13 (+0530)     return await Launcher(options, **kwargs).launch()
27.07.18 06:36:13 (+0530)   File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pyppeteer/launcher.py", line 166, in launch
27.07.18 06:36:13 (+0530)     self.browserWSEndpoint = self._get_ws_endpoint()
27.07.18 06:36:13 (+0530)   File "/.pyenv/versions/3.6.6/lib/python3.6/site-packages/pyppeteer/launcher.py", line 184, in _get_ws_endpoint
27.07.18 06:36:13 (+0530)     raise BrowserError(f'Failed to connect to browser port: {url}')
27.07.18 06:36:13 (+0530) pyppeteer.errors.BrowserError: Failed to connect to browser port: http://127.0.0.1:32947/json/version
27.07.18 06:36:13 (+0530) 
27.07.18 06:36:13 (+0530) DevTools listening on ws://127.0.0.1:32947/devtools/browser/be78382f-ccfc-466e-8100-bf11777b2ca6

From above logs it looks like pyppeteer attempts to connect to browser before DevTools has started. Does pyppeteer wait for the devtools to load, before it attempts to connect to the browser?

miyakogi commented 6 years ago

Pyppeteer waits for about 10 seconds to start the browser. It is currently hard coded. On your machine, does it take more than 10 seconds to start browser?

devxpy commented 6 years ago

It's a raspberry pi so, unfortunately, yes.

I do admit that it is a rare occurrence, but the race is still there...

miyakogi commented 6 years ago

I have removed timeout of browser startup in dev branch. Please try it:

pip install -U git+git://github.com/miyakogi/pyppeteer@dev
miyakogi commented 6 years ago

Released.