Skip to content

Instantly share code, notes, and snippets.

@rdlowrey
Last active December 20, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdlowrey/6076532 to your computer and use it in GitHub Desktop.
Save rdlowrey/6076532 to your computer and use it in GitHub Desktop.
Handling fatal errors inside threads via register_shutdown_function
<?php
class Worker extends \Worker {
function run() {
// &$this ref required to avoid segfault
register_shutdown_function([&$this, 'onShutdown']);
}
private function onShutdown() {
if (!$err = error_get_last()) {
return;
}
$fatals = [
E_ERROR,
E_PARSE,
E_CORE_ERROR,
E_CORE_WARNING,
E_COMPILE_ERROR,
E_COMPILE_WARNING
];
if (in_array($err['type'], $fatals)) {
$this->handleFatalError();
}
}
private function handleFatalError() {
die('Worker thread died :(');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment