Swoole\Server->on('WorkerError', fn)

Declaration

<?php
Swoole\Server->on('WorkerError', Callable $callback)

Parameters

event

The event callback name.

callback

Callable event function.

Return

success

If success, it returns true, otherwise it returns false.

Description

Execute the callback function when a Worker Process is stopped caused by a fatal Error or Exception.

The callback function is executed in the Manager Process thread. This function is mainly used for alarm and monitoring purposes. Once it is found that the Worker process exits abnormally, it is very likely that it has encountered a fatal error or a process core dump due to a segmentation fault etc. It is best to use this event for recording logs or sending alarm signals to alert that a fatal error has taken place.

Example

<?php
$server->on('WorkerError', function(Swoole\Server $server, int $workerId, int $workerPid, int $exitCode, int $signal)
{
  // ...
});
  • $server: The Swoole server object
  • $workerId The ID number of worker process that had a fatal error which failed abnormally
  • $workerPid The pid of worker process that failed abnormally
  • $exitCode The exit code of worker process that failed abnormally (0-255)
  • $signal The exit signal of worker process that failed abnormally

Notes

  • When the signal is 11, the worker process experienced a segmentation fault, this means the error has been caused by either an uncaught exception due to user code where Swoole does not expect certain situations and has failed to catch an error gracefully or a bug has been discovered within Swoole which requires a core dump to be taken in order to find out what caused the error

  • When the exit code is 255 it means there was a traditional PHP fatal error and you will need to check your logs to find out what went wrong

  • When the signal is 9 it means the worker was forced to be killed, this could be because some process executed a kill -9 command or that the worker process was killed by some other means, the worker process could be out of memory etc.

  • If worker processes are running out of memory, consider upgrading system memory and checking your server configuration to check the socket_buffer_size and if you are using any large Swoole Table instances