Skip to content

Commit

Permalink
fix rare crash in getHtml
Browse files Browse the repository at this point in the history
for a split second, documentElement might be missing, causing getHtml() to crash.
I had a program that was doing page stuff and calling getHtml() like every 10 milliseconds (100 times per second), and got an unexpected crash. Was able to create a small reproducible sample:
```php
<?php

declare(strict_types=1);
require_once('vendor/autoload.php');
$chromeBinary = "/snap/bin/chromium";
$browser_factory = new \HeadlessChromium\BrowserFactory($chromeBinary);
$browser_factory->setOptions([
    "headless" => true,
    "noSandbox" => true,
    'windowSize'   => [1000, 1000]
]);
$browser = $browser_factory->createBrowser();
$page = $browser->createPage();
for ($i = 0; $i < 100; ++$i) {
    $page->navigate("http://example.com");
    $html = $page->getHtml();
    $page->navigate("http://example.org");
    $html = $page->getHtml();
}
```
consistently crash with:
```
PHP Fatal error:  Uncaught HeadlessChromium\Exception\JavascriptException: Error during javascript evaluation: TypeError: Cannot read properties of null (reading 'outerHTML')
    at <anonymous>:1:26 in /home/hans/projects/ibkr/vendor/chrome-php/chrome/src/PageUtils/PageEvaluation.php:89
Stack trace:
#0 /home/hans/projects/ibkr/vendor/chrome-php/chrome/src/PageUtils/PageEvaluation.php(108): HeadlessChromium\PageUtils\PageEvaluation->waitForResponse()
chrome-php#1 /home/hans/projects/ibkr/vendor/chrome-php/chrome/src/Page.php(894): HeadlessChromium\PageUtils\PageEvaluation->getReturnValue()
chrome-php#2 /home/hans/projects/ibkr/test_crash.php(16): HeadlessChromium\Page->getHtml()
chrome-php#3 {main}
  thrown in /home/hans/projects/ibkr/vendor/chrome-php/chrome/src/PageUtils/PageEvaluation.php on line 89
```
  • Loading branch information
divinity76 committed May 3, 2023
1 parent c3a3652 commit c968993
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,19 @@ public function setHtml(string $html, int $timeout = 3000, string $eventName = s
*/
public function getHtml(?int $timeout = null): string
{
return $this->evaluate('document.documentElement.outerHTML')->getReturnValue($timeout);
try {
return $this->evaluate('document.documentElement.outerHTML')->getReturnValue($timeout);
} catch (Exception\JavascriptException $e) {
if (0 === strpos($e->getMessage(), 'Error during javascript evaluation: TypeError: Cannot read properties of null (reading \'outerHTML\')')) {
// sometimes after a page reload, for a split second,
// document.documentElement does not exist
// (not sure if its a chromium bug or intentional but either way)
usleep(1000); // 1ms seems to be more than enough, unable to reproduce.
return $this->evaluate('document.documentElement.outerHTML')->getReturnValue($timeout);
}
throw $e;
}
}

/**
* Read cookies for the current page.
*
Expand Down

0 comments on commit c968993

Please sign in to comment.