chrome-php / chrome

Instrument headless chrome/chromium instances from PHP
MIT License
2.27k stars 279 forks source link

Timeout #644

Open jazz265 opened 3 months ago

jazz265 commented 3 months ago
require_once __DIR__ . '/vendor/autoload.php';
use HeadlessChromium\BrowserFactory;

$browserFactory = new BrowserFactory();

// starts headless Chrome
$browser = $browserFactory->createBrowser([
    'headless' => false, 
]);

try {
    // creates a new page and navigate to an URL
    $page = $browser->createPage();
    $page->navigate('http://example.com')->waitForNavigation();

    // get page title
    $pageTitle = $page->evaluate('document.title')->getReturnValue();

    // screenshot - Say "Cheese"! ??
    $page->screenshot()->saveToFile('/foo/bar.png');

    // pdf
    $page->pdf(['printBackground' => false])->saveToFile('/foo/bar.pdf');
} finally {
    // bye
    $browser->close();
}

PHP Fatal error: Uncaught HeadlessChromium\Exception\OperationTimedOut: Operation timed out (5sec) in C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\Utils.php:71 Stack trace:

0 C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\Communication\ResponseReader.php(115): HeadlessChromium\Utils::tryWithTimeout(5000000, Object(Generator))

1 C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\Communication\Connection.php(247): HeadlessChromium\Communication\ResponseReader->waitForResponse(5000)

2 C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\Browser.php(77): HeadlessChromium\Communication\Connection->sendMessageSync(Object(HeadlessChromium\Communication\Message))

3 C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\Browser\ProcessAwareBrowser.php(26): HeadlessChromium\Browser->__construct(Object(HeadlessChromium\Communication\Connection))

4 C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\Browser\BrowserProcess.php(157): HeadlessChromium\Browser\ProcessAwareBrowser->__construct(Object(HeadlessChromium\Communication\Connection), Object(HeadlessChromium\Browser\BrowserProcess))

5 C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\BrowserFactory.php(71): HeadlessChromium\Browser\BrowserProcess->start('C:\Program File...', Array)

6 C:\xampp\htdocs\x\sc\test2.php(16): HeadlessChromium\BrowserFactory->createBrowser(Array)

7 {main}

thrown in C:\xampp\htdocs\x\sc\vendor\chrome-php\chrome\src\Utils.php on line 71

The browser starts up, but it doesn't work and times out, closes, and gives the above error. Please let me know how to solve this.

cliffordvickrey commented 3 months ago

Getting the same issue with Chrome >= 128 when A) running PHP for Windows in B) SAPI mode (e.g. Apache). In the logs, everything looks good (in my use case, I successfully invoke Page.printToPDF and see that DevTools logs "firstMeaningfulPaintCandidate"), except Chrome never writes a response to the web socket.

A workaround for me was disabling sandbox mode, e.g.:

$browserFactory->createBrowser(['headless' => true, 'noSandbox' => true]);

This may have security implications, depending on what you're using Chrome for; these can be mitigated by running Chrome inside a container.

jazz265 commented 2 months ago

Thank you for answering! I tried it, but it didn't work. Is there any other way?

Issei0804-ie commented 2 months ago

In my environment, that error occurred when CPU resources were insufficient. I think too long time to process

jimfm29 commented 2 months ago

Chrome was updated on our production server yesterday eve to 128.0.6613.85. Now, chrome-php times out. Running on Windows IIS with php 8.2.20; tested with php 8.3.10 with same results.

HeadlessChromium\Exception\OperationTimedOut: Operation timed out after 240s. in \chrome-php\chrome\src\Exception\OperationTimedOut.php:18 Stack trace:

0 \chrome-php\chrome\src\Utils.php(67): HeadlessChromium\Exception\OperationTimedOut::createFromTimeout(240000000)

1 \chrome-php\chrome\src\Communication\ResponseReader.php(114): HeadlessChromium\Utils::tryWithTimeout(240000000, Object(Generator))

2 \chrome-php\chrome\src\PageUtils\AbstractBinaryInput.php(67): HeadlessChromium\Communication\ResponseReader->waitForResponse(240000)

3 \our_project_library\Export.php(249): HeadlessChromium\PageUtils\AbstractBinaryInput->saveToFile('C:\Windows\Temp...', 240000)

andrewsauder commented 2 months ago

Also experiencing this issue with Windows PHP 8.1 nts x64 and PHP 8.2 nts x64. Issue occurs using php-cgi.exe with IIS and php.exe from command line. Disabling sandbox mode has resolved the issue for now. $browserFactory->createBrowser(['headless' => true, 'noSandbox' => true]);