private function handleChildErrors(&$socket): Closure
    {
        // This storage is freed on error (case of allowed memory exhausted).
        $reserved = str_repeat('*', 32 * 1024);

        return function () use (&$reserved, &$socket): void {
            unset($reserved);

            $error = error_get_last();

            if ($error === null) {
                unset($reserved);
                return;
            }

            $handler = new ErrorFormatter();
            if ($handler->constant($error['type']) === null) {
                $this->logger->warning(
                    '{type}: Unable to recognize error type. Skip sending error to master: {message}',
                    $this->logContext + ['message' => $error['message']]
                );
                return;
            }

            if (is_resource($socket) == false) {
                $this->logger->warning(
                    '{type}: supplied resource is not a valid socket resource. Skip sending error to master: {message}',
                    $this->logContext + ['message' => $error['message']]
                );
                return;
            }

            $this->logger->debug('{type}: sending error to master', $this->logContext);
            $data = serialize($error);

            do {
                $len = socket_write($socket, $data);
                if ($len === false || $len === 0) {
                    break;
                }

                $data = substr($data, $len);
            } while (is_numeric($len) && $len > 0 && is_resource($socket));
        };
    }