Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Chrome process stopped before startup completed. #610

Open
DanielSpravtsev opened this issue Feb 29, 2024 · 10 comments
Open

[BUG] Chrome process stopped before startup completed. #610

DanielSpravtsev opened this issue Feb 29, 2024 · 10 comments

Comments

@DanielSpravtsev
Copy link

Using option userDataDir causes RuntimeException Chrome process stopped before startup completed.

'userDataDir' => 'storage/test'

Sometimes can be tricked with 'connectionDelay' => 100,

As I understood, issue caused because script trying to execute next command before browser fully booted.
So can we somehow handle browser boot finished event to prevent executing scripts before boot is done?

@enricodias
Copy link
Member

Is this boot finished a real event? I couldn't find it in the docs.

I also never managed to reproduce the process stopped before startup error so I wouldn't be able to try any solution for it. But if you managed to, feel free to open a PR for that.

@DanielSpravtsev
Copy link
Author

Is this boot finished a real event? I couldn't find it in the docs.

No, it's just in theory. Maybe there is a way to ask chrome through websocket when it's ready to work (booted)

I also never managed to reproduce the process stopped before startup error so I wouldn't be able to try any solution for it. But if you managed to, feel free to open a PR for that.

I immediately faced it when added 'userDataDir' => 'storage/test' with keepAlive => true & headless => false for working with cookie logged profiles

I'll try to investigate more time into this issue as soon as I'll closely work with it

@darkvovich
Copy link

I have the same problem on Cent OS, fixed it by REinstalling Chrome via this instruction https://linuxize.com/post/how-to-install-google-chrome-web-browser-on-centos-7/

@divinity76
Copy link
Contributor

Can you guys make a full reproducible example? I am unable to reproduce it on Windows 10 + PHP 8.3.4 + chrome-php/chrome v1.10.0 + Google Chrome 122.0.6261.129 with the code

<?php

declare(strict_types=1);
require_once __DIR__ . "/vendor/autoload.php";
$browserFactory = new \HeadlessChromium\BrowserFactory(
    "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
);
$userDataDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR  . 'chrome_php_issue_610';
echo "User data dir: " . $userDataDir . PHP_EOL;
if (file_exists($userDataDir)) {
    echo "Removing user data dir" . PHP_EOL;
    shell_exec('rmdir /s /q ' . escapeshellarg($userDataDir));
}
mkdir($userDataDir);
$browser = $browserFactory->createBrowser([
    'headless' => !true,
    'userDataDir' => $userDataDir,
]);
$page = $browser->createPage();
$page->navigate('https://example.com/')->waitForNavigation();

outputs:

C:\Users\hans\test>php reproduce.php
User data dir: C:\Users\hans\AppData\Local\Temp\chrome_php_issue_610

C:\Users\hans\test>

and just as expected, chromium visits example.com without any problem 🤔

@manikandang1468
Copy link

RuntimeException Chrome process stopped before startup completed😒

@DanielSpravtsev
Copy link
Author

RuntimeException Chrome process stopped before startup completed😒

Can you provide your createBrowser params?

@divinity76
Copy link
Contributor

divinity76 commented Mar 22, 2024

can any of you guys write a reproducible example? I am also unable to reproduce this on Linux Ubuntu 24.04-beta PHP8.3.0 Chromium 122.0.6261.128 chrome-php/chrome v1.11.0 with the code:

<?php

declare(strict_types=1);
require_once __DIR__ . "/vendor/autoload.php";
$browserFactory = new \HeadlessChromium\BrowserFactory('chromium-browser');
$userDataDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR  . 'chrome_php_issue_610';
echo "User data dir: " . $userDataDir . PHP_EOL;
if (file_exists($userDataDir)) {
    echo "Removing user data dir" . PHP_EOL;
    shell_exec('rm -rfv ' . escapeshellarg($userDataDir));
}
mkdir($userDataDir);
$browser = $browserFactory->createBrowser([
    'headless' => !true,
    'userDataDir' => $userDataDir,
]);
$page = $browser->createPage();
$page->navigate('https://example.com/')->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE);
  • just as expected, it visits example.com without any problem:
hans@DESKTOP-EE15SLU:~/test$ php -v
PHP 8.3.0-1ubuntu1 (cli) (built: Jan 19 2024 14:00:34) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.0-1ubuntu1, Copyright (c), by Zend Technologies
    with Xdebug v3.2.0, Copyright (c) 2002-2022, by Derick Rethans
hans@DESKTOP-EE15SLU:~/test$ chromium-browser --version
Chromium 122.0.6261.128 snap
hans@DESKTOP-EE15SLU:~/test$ php reproduce2.php 
User data dir: /tmp/chrome_php_issue_610
Removing user data dir

@DanielSpravtsev
Copy link
Author

DanielSpravtsev commented Mar 22, 2024

@divinity76

Sure. Here is my example


 $browserFactory = new BrowserFactory();
// starts headless Chrome
        $browser = $browserFactory->createBrowser([
            'windowSize'   => [1920, 1000],
            'headless' => false,
            'keepAlive' => true,
            'userDataDir' => storage_path('/test'), // I use it from Laravel, you need to change it to you path
            'enableImages' => true,
            'ignoreCertificateErrors' => true,
            'userAgent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
            'excludedSwitches' => ['--enable-automation', '--no-sandbox', '--disable-setuid-sandbox', '--hide-scrollbars'],
            'customFlags' => ['--disable-blink-features=AutomationControlled']
        ]);
        $browser->setPagePreScript("// Simulate navigator permissions;
const originalQuery = window.navigator.permissions.query;
window.navigator.permissions.query = (parameters) => (
    parameters.name === 'notifications' ?
        Promise.resolve({ state: Notification.permission }) :
        originalQuery(parameters)
);");
        $page = $browser->createPage();

That's all, after browser launch it is immediately throwing Chrome process stopped before startup completed no matter if there are any other commands after createPage method

And here is my debug log with 'debugLogger' => 'php://stdout' enabled

// Laravel Tinker started at 2024-03-22 13:19:17
[2024-03-22T13:19:18.152200+03:00] chrome.DEBUG: process: initializing [] []
[2024-03-22T13:19:18.152993+03:00] chrome.DEBUG: process: using directory: /Users/_username_/PhpstormProjects/_projectname_/storage/test [] []
[2024-03-22T13:19:18.154067+03:00] chrome.DEBUG: process: starting process: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' '--remote-debugging-port=0' '--remote-allow-origins=*' '--disable-background-networking' '--disable-background-timer-throttling' '--disable-client-side-phishing-detection' '--disable-hang-monitor' '--disable-popup-blocking' '--disable-prompt-on-repost' '--disable-sync' '--disable-translate' '--disable-features=ChromeWhatsNewUI' '--metrics-recording-only' '--no-first-run' '--safebrowsing-disable-auto-update' '--enable-automation' '--password-store=basic' '--use-mock-keychain' '--window-size=1920,1000' '--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36' '--ignore-certificate-errors' '--user-data-dir=/Users/_username_/PhpstormProjects/_projectname_/storage/test' [] []
[2024-03-22T13:19:18.155849+03:00] chrome.DEBUG: process: waiting for 30 seconds for startup [] []
[2024-03-22T13:19:18.292862+03:00] chrome.DEBUG: process: ✗ chrome process stopped [] []
 RuntimeException  Chrome process stopped before startup completed.

*** Execution finished ***

For clarification: Browser still opened and alive after exception is thrown. It's not crashed, freezed or something like that, it's totally fine 😄

One more thing: This exception throws only when keepAlive = true, maybe because php loses process when it tries to run next command $page = $browser->createPage();

Maybe some thoughts above will be interested for you @enricodias

@theoaksoft
Copy link

Is there any solution or workaround for this?

@DanielSpravtsev
Copy link
Author

Is there any solution or workaround for this?

As I mentioned earlier: do not use KeepAlive = true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants