winzik/docker/ext/cryo/cryo_fork.c
2026-11-14 17:21:23 +01:00

1684 lines
48 KiB
C

/*
* cryo_fork.c — fork mode of Cryo.
*
* cryo_fork_pool() master + worker processes (jobs pushed by the master,
* or pulled by the job itself), recycled on max_jobs /
* max_memory, respawned after abnormal deaths.
* cryo_fork_send/recv() message channel between a worker and its master.
* cryo_fork() fork checkpoint (variants child, parent, template);
* the C entry point cryo_fork_checkpoint() is what
* cryo_freeze(..., ['mode' => 'fork']) calls.
* cryo_thaw_fork() resume from a suspended checkpoint (variant parent).
* cryo_fork_job_done() end of one job in a template child (limits).
* cryo_fork_info() role of this process, suspended checkpoints.
*
* Channel: one AF_UNIX socket pair per child. Frame = u32 payload length,
* u8 type, PHP-serialized payload (empty for STOP and RECYCLE).
*
* WebAssembly (CRYO_BACKEND_WASM): no processes. The pool runs the jobs one
* after the other in this instance (messages go through in-memory queues);
* checkpoints call cryo_host_clone(mode): the host returns 0 in the clone
* (CRYO_THAWED there) and the clone id in the calling instance (CRYO_FROZEN);
* how a variant is carried out (waiting, re-cloning, number of workers,
* resuming the clone) is up to the JS host. Outside a pool, the channel is
* cryo_host_send()/cryo_host_recv() with peer id 0 = the parent instance;
* cryo_host_recv() keeps a message larger than the buffer and returns its
* size, the call is then repeated with a big enough buffer.
*
* NTS only: the state lives in statics.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_cryo.h"
#include "cryo_host.h"
#include "zend_exceptions.h"
#include "zend_smart_str.h"
#include "ext/standard/php_var.h"
#include "main/php_output.h"
#ifndef CRYO_BACKEND_WASM
# include <unistd.h>
# include <errno.h>
# include <fcntl.h>
# include <poll.h>
# include <signal.h>
# include <time.h>
# include <sys/socket.h>
# include <sys/wait.h>
# ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
# endif
#endif
/* Provided by cryo_freeze.c (runs the cryo_on_thaw hooks); this weak empty
* version only keeps the extension linkable until then. */
__attribute__((weak)) void cryo_hooks_run_thaw(void)
{
}
/* frame types */
enum { CF_JOB = 1, CF_STOP, CF_RESULT, CF_MSG, CF_RECYCLE };
/* checkpoint variants */
enum { V_CHILD = 0, V_PARENT = 1, V_TEMPLATE = 2 };
/* slot states */
enum { SLOT_EMPTY = 0, SLOT_IDLE, SLOT_BUSY, SLOT_DONE, SLOT_DEAD };
/* master events */
enum { EV_NONE = 0, EV_FRAME, EV_EXIT };
#define CF_RESPAWN_LIMIT 16
typedef struct {
zend_long pid; /* process id (WASM: instance id) */
zend_long owner; /* process that kills it at its normal end */
zend_long parent; /* process that forked it (the one that can reap it) */
int wake_fd; /* write end of the wake-up pipe */
} cf_ckpt;
typedef struct {
zend_long pid; /* 0 = no process */
zend_long last_pid;
int fd;
int state;
int32_t job; /* job index, -1 = none */
uint32_t runs; /* processes forked for this slot */
bool recycle; /* RECYCLE received: replace after exit */
bool reported; /* an error was already reported for this process */
} cf_slot;
typedef struct cf_master {
cf_slot *slots;
zend_long n;
zval *on_message;
zval pending; /* events read ahead by cryo_fork_recv() */
int stopping; /* signal forwarded to the children */
struct cf_master *prev;
#ifndef CRYO_BACKEND_WASM
struct sigaction old_sig[3];
struct sigaction old_chld;
bool sig_installed;
#endif
} cf_master;
typedef struct {
int kind;
zend_long w;
uint8_t t;
zval v;
int status;
} cf_event;
static struct {
int parent_fd; /* channel to the master, -1 = none */
zend_long worker; /* slot id in the master, -1 = none */
const char *role;
zval inbox; /* child side: messages from the master */
zval mbox; /* master side: ['worker', 'pid', 'data'] */
cf_master *master; /* running pool / supervisor */
bool template_child;
zend_long max_jobs;
zend_long max_memory;
zend_long jobs_done;
bool in_job; /* WASM sequential pool: inside a job */
bool had_pool;
cf_ckpt *ckpts;
uint32_t nckpts;
uint32_t ckpt_cap;
} cf;
/* ------------------------------------------------------------------------ */
/* Helpers */
/* ------------------------------------------------------------------------ */
static zend_long cf_getpid(void)
{
#ifdef CRYO_BACKEND_WASM
return 1;
#else
return (zend_long) getpid();
#endif
}
#ifndef CRYO_BACKEND_WASM
static double cf_now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double) ts.tv_sec * 1000.0 + (double) ts.tv_nsec / 1e6;
}
#endif
/* Nothing buffered may be duplicated by fork(). */
static void cf_flush(void)
{
php_output_flush_all();
fflush(stdout);
fflush(stderr);
}
static zval *cf_opt(HashTable *o, const char *k)
{
zval *v = o ? zend_hash_str_find_deref(o, k, strlen(k)) : NULL;
return v && Z_TYPE_P(v) != IS_NULL ? v : NULL;
}
static zend_long cf_opt_long(HashTable *o, const char *k, zend_long def)
{
zval *v = cf_opt(o, k);
return v ? zval_get_long(v) : def;
}
static bool cf_opt_bool(HashTable *o, const char *k, bool def)
{
zval *v = cf_opt(o, k);
return v ? zend_is_true(v) : def;
}
static zend_long cf_cpus(void)
{
zend_long n = 0;
#if defined(_SC_NPROCESSORS_ONLN) && !defined(CRYO_BACKEND_WASM)
n = (zend_long) sysconf(_SC_NPROCESSORS_ONLN);
#endif
return n > 0 ? n : 4;
}
/* queues: packed arrays, ownership of v moves in / out */
static void q_push(zval *q, zval *v)
{
if (Z_ISUNDEF_P(q)) array_init(q);
add_next_index_zval(q, v);
}
static bool q_shift(zval *q, zval *out)
{
if (Z_ISUNDEF_P(q) || zend_hash_num_elements(Z_ARRVAL_P(q)) == 0) return false;
zend_ulong idx;
zval *v;
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(q), idx, v) {
ZVAL_COPY(out, v);
zend_hash_index_del(Z_ARRVAL_P(q), idx);
break;
} ZEND_HASH_FOREACH_END();
return true;
}
static void q_clear(zval *q)
{
if (!Z_ISUNDEF_P(q)) {
zval_ptr_dtor(q);
ZVAL_UNDEF(q);
}
}
static zend_string *cf_serialize(zval *v)
{
smart_str buf = {0};
php_serialize_data_t sd;
PHP_VAR_SERIALIZE_INIT(sd);
php_var_serialize(&buf, v, &sd);
PHP_VAR_SERIALIZE_DESTROY(sd);
if (EG(exception)) {
smart_str_free(&buf);
return NULL;
}
return smart_str_extract(&buf);
}
static bool cf_unserialize(const char *p, size_t len, zval *out)
{
const unsigned char *s = (const unsigned char *) p;
php_unserialize_data_t ud;
ZVAL_UNDEF(out);
PHP_VAR_UNSERIALIZE_INIT(ud);
bool ok = php_var_unserialize(out, &s, s + len, &ud);
PHP_VAR_UNSERIALIZE_DESTROY(ud);
if (!ok) {
zval_ptr_dtor(out);
ZVAL_UNDEF(out);
}
return ok;
}
static zend_string *cf_exception_message(void)
{
zend_object *ex = EG(exception);
zval rv, *msg = zend_read_property_ex(ex->ce, ex, ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv);
return zend_strpprintf(0, "%s: %s", ZSTR_VAL(ex->ce->name),
msg && Z_TYPE_P(msg) == IS_STRING ? Z_STRVAL_P(msg) : "");
}
#ifndef CRYO_BACKEND_WASM
/* An exception thrown by a hook or a callback where nobody can catch it. */
static void cf_report_exception(const char *where)
{
if (!EG(exception) || zend_is_unwind_exit(EG(exception)) || zend_is_graceful_exit(EG(exception))) return;
zend_string *m = cf_exception_message();
zend_clear_exception();
php_error_docref(NULL, E_WARNING, "%s: uncaught %s", where, ZSTR_VAL(m));
zend_string_release(m);
}
#endif
static void cf_ckpt_push(zend_long pid, int wake_fd)
{
if (cf.nckpts == cf.ckpt_cap) {
cf.ckpt_cap = cf.ckpt_cap ? cf.ckpt_cap * 2 : 4;
cf.ckpts = perealloc(cf.ckpts, cf.ckpt_cap * sizeof(cf_ckpt), 1);
}
cf_ckpt *c = &cf.ckpts[cf.nckpts++];
c->pid = pid;
c->owner = cf_getpid();
c->parent = cf_getpid();
c->wake_fd = wake_fd;
}
/* ------------------------------------------------------------------------ */
/* POSIX: frames, child setup, signals */
/* ------------------------------------------------------------------------ */
#ifndef CRYO_BACKEND_WASM
#define CF_HDR 5
static bool fd_write_all(int fd, const char *p, size_t n)
{
while (n) {
ssize_t w = send(fd, p, n, MSG_NOSIGNAL);
if (w < 0 && errno == EINTR) continue;
if (w <= 0) return false;
p += w;
n -= (size_t) w;
}
return true;
}
static bool fd_read_all(int fd, char *p, size_t n)
{
while (n) {
ssize_t r = read(fd, p, n);
if (r < 0 && errno == EINTR) continue;
if (r <= 0) return false;
p += r;
n -= (size_t) r;
}
return true;
}
static bool frame_send_str(int fd, uint8_t type, zend_string *payload)
{
char hdr[CF_HDR];
uint32_t len = payload ? (uint32_t) ZSTR_LEN(payload) : 0;
memcpy(hdr, &len, 4);
hdr[4] = (char) type;
return fd_write_all(fd, hdr, CF_HDR) && (len == 0 || fd_write_all(fd, ZSTR_VAL(payload), len));
}
/* false with EG(exception) set = the value cannot be serialized */
static bool frame_send(int fd, uint8_t type, zval *value)
{
zend_string *s = NULL;
if (value) {
s = cf_serialize(value);
if (!s) return false;
}
bool ok = frame_send_str(fd, type, s);
if (s) zend_string_release(s);
return ok;
}
/* 1 = frame read (*out = payload, UNDEF if none, null if undecodable),
* -1 = closed or broken */
static int frame_recv(int fd, uint8_t *type, zval *out)
{
char hdr[CF_HDR];
uint32_t len;
ZVAL_UNDEF(out);
if (!fd_read_all(fd, hdr, CF_HDR)) return -1;
memcpy(&len, hdr, 4);
*type = (uint8_t) hdr[4];
if (len == 0) return 1;
char *buf = emalloc(len);
if (!fd_read_all(fd, buf, len)) {
efree(buf);
return -1;
}
if (!cf_unserialize(buf, len, out)) {
if (EG(exception)) zend_clear_exception();
ZVAL_NULL(out);
}
efree(buf);
return 1;
}
/* 1 = readable, 0 = timeout (ms < 0: forever) */
static int cf_readable(int fd, int ms)
{
struct pollfd p = { fd, POLLIN, 0 };
for (;;) {
int r = poll(&p, 1, ms);
if (r < 0 && errno == EINTR) continue;
return r > 0 ? 1 : 0;
}
}
static int cf_status_code(int status)
{
if (WIFEXITED(status)) return WEXITSTATUS(status);
if (WIFSIGNALED(status)) return 128 + WTERMSIG(status);
return 1;
}
static bool cf_pid_stopped(zend_long pid)
{
#ifdef __linux__
char path[64], buf[512];
snprintf(path, sizeof(path), "/proc/" ZEND_LONG_FMT "/stat", pid);
int fd = open(path, O_RDONLY);
if (fd < 0) return false;
ssize_t n = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (n <= 0) return false;
buf[n] = '\0';
char *p = strrchr(buf, ')');
if (!p) return false;
p++;
while (*p == ' ') p++;
return *p == 'T' || *p == 't';
#else
(void) pid;
return true;
#endif
}
static void cf_waitpid(zend_long pid, int *status)
{
*status = 0;
while (waitpid((pid_t) pid, status, 0) < 0) {
if (errno != EINTR) {
*status = 0;
return;
}
}
}
/* signals forwarded by a master to its children */
static volatile sig_atomic_t cf_sig = 0;
static const int cf_fwd[3] = { SIGTERM, SIGINT, SIGHUP };
static void cf_sig_handler(int signo)
{
cf_sig = signo;
}
static void cf_signals_install(cf_master *m)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_handler = cf_sig_handler; /* no SA_RESTART: poll() returns EINTR */
for (int i = 0; i < 3; i++) sigaction(cf_fwd[i], &sa, &m->old_sig[i]);
sa.sa_handler = SIG_DFL; /* waitpid() needs SIGCHLD not ignored */
sigaction(SIGCHLD, &sa, &m->old_chld);
m->sig_installed = true;
}
static void cf_signals_restore(cf_master *m)
{
if (!m->sig_installed) return;
for (int i = 0; i < 3; i++) sigaction(cf_fwd[i], &m->old_sig[i], NULL);
sigaction(SIGCHLD, &m->old_chld, NULL);
m->sig_installed = false;
}
/* In a new child: forget everything that belongs to the master side. */
static void cf_child_setup(int fd, zend_long worker, const char *role)
{
for (cf_master *m = cf.master; m; m = m->prev) {
cf_signals_restore(m);
for (zend_long i = 0; i < m->n; i++) {
if (m->slots[i].fd >= 0) close(m->slots[i].fd);
m->slots[i].fd = -1;
m->slots[i].pid = 0;
}
q_clear(&m->pending);
}
cf_sig = 0;
cf.master = NULL;
if (cf.parent_fd >= 0 && cf.parent_fd != fd) close(cf.parent_fd);
cf.parent_fd = fd;
cf.worker = worker;
cf.role = role;
q_clear(&cf.inbox);
q_clear(&cf.mbox);
cf.template_child = false;
cf.jobs_done = 0;
cf.had_pool = false;
cryo_process_atfork_child();
}
static ZEND_NORETURN void cf_worker_exit(int code)
{
cf_flush();
_exit(code);
}
/* ---- master side: events ------------------------------------------------ */
static void pending_push(cf_master *m, int kind, zend_long w, uint8_t t, zval *v, int status)
{
zval a;
array_init(&a);
add_next_index_long(&a, kind);
add_next_index_long(&a, w);
add_next_index_long(&a, t);
if (v && !Z_ISUNDEF_P(v)) add_next_index_zval(&a, v); else add_next_index_null(&a);
add_next_index_long(&a, status);
q_push(&m->pending, &a);
}
static bool pending_take(cf_master *m, bool msg_only, cf_event *e)
{
if (Z_ISUNDEF(m->pending)) return false;
zend_ulong idx;
zval *a;
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL(m->pending), idx, a) {
HashTable *h = Z_ARRVAL_P(a);
int kind = (int) Z_LVAL_P(zend_hash_index_find(h, 0));
uint8_t t = (uint8_t) Z_LVAL_P(zend_hash_index_find(h, 2));
if (msg_only && (kind != EV_FRAME || t != CF_MSG)) continue;
e->kind = kind;
e->w = Z_LVAL_P(zend_hash_index_find(h, 1));
e->t = t;
ZVAL_COPY(&e->v, zend_hash_index_find(h, 3));
e->status = (int) Z_LVAL_P(zend_hash_index_find(h, 4));
zend_hash_index_del(Z_ARRVAL(m->pending), idx);
return true;
} ZEND_HASH_FOREACH_END();
return false;
}
static void cf_forward_signal(cf_master *m)
{
if (!cf_sig) return;
int s = cf_sig;
cf_sig = 0;
m->stopping = s;
for (zend_long w = 0; w < m->n; w++) {
if (m->slots[w].pid > 0) kill((pid_t) m->slots[w].pid, s);
}
}
/* The child of slot w has exited: read what it left, then the exit event. */
static void slot_reaped(cf_master *m, zend_long w, int status)
{
cf_slot *s = &m->slots[w];
while (s->fd >= 0 && cf_readable(s->fd, 0)) {
uint8_t t;
zval v;
if (frame_recv(s->fd, &t, &v) != 1) break;
pending_push(m, EV_FRAME, w, t, &v, 0);
}
if (s->fd >= 0) close(s->fd);
s->fd = -1;
s->pid = 0;
pending_push(m, EV_EXIT, w, 0, NULL, status);
}
/* Next event of the master: a frame from a child, or a child exit.
* timeout_ms < 0 = wait as long as a child is alive. msg_only: only messages
* are returned, other events stay pending. EV_NONE = timeout or no child. */
static int cf_master_wait(cf_master *m, int timeout_ms, bool msg_only, cf_event *e)
{
ZVAL_UNDEF(&e->v);
e->kind = EV_NONE;
if (pending_take(m, msg_only, e)) return e->kind;
double deadline = cf_now_ms() + (timeout_ms > 0 ? timeout_ms : 0);
struct pollfd *pfd = safe_emalloc((size_t) m->n, sizeof(struct pollfd), 0);
zend_long *map = safe_emalloc((size_t) m->n, sizeof(zend_long), 0);
for (;;) {
cf_forward_signal(m);
nfds_t np = 0;
zend_long alive = 0;
for (zend_long w = 0; w < m->n; w++) {
cf_slot *s = &m->slots[w];
if (s->pid > 0) alive++;
if (s->fd >= 0) {
pfd[np].fd = s->fd;
pfd[np].events = POLLIN;
pfd[np].revents = 0;
map[np++] = w;
}
}
if (alive == 0) break;
int wait = 200;
if (timeout_ms >= 0) {
double left = deadline - cf_now_ms();
if (left <= 0) break;
if (left < wait) wait = (int) left + 1;
}
int r = poll(np ? pfd : NULL, np, wait);
if (r < 0) {
if (errno == EINTR) continue;
break;
}
if (r > 0) {
for (nfds_t q = 0; q < np; q++) {
if (!pfd[q].revents) continue;
zend_long w = map[q];
cf_slot *s = &m->slots[w];
uint8_t t;
zval v;
if (frame_recv(s->fd, &t, &v) == 1) {
pending_push(m, EV_FRAME, w, t, &v, 0);
continue;
}
/* EOF: the child is exiting */
close(s->fd);
s->fd = -1;
int status;
cf_waitpid(s->pid, &status);
slot_reaped(m, w, status);
}
} else {
/* timeout: children that exited while another process (one of
* their own children) still holds their socket */
for (zend_long w = 0; w < m->n; w++) {
cf_slot *s = &m->slots[w];
if (s->pid <= 0) continue;
int status = 0;
pid_t p = waitpid((pid_t) s->pid, &status, WNOHANG);
if (p == (pid_t) s->pid || (p < 0 && errno == ECHILD)) slot_reaped(m, w, status);
}
}
if (pending_take(m, msg_only, e)) break;
}
efree(pfd);
efree(map);
return e->kind;
}
/* Kills and reaps every child of the master (exception in a callback). */
static void cf_master_abort(cf_master *m)
{
for (zend_long w = 0; w < m->n; w++) {
cf_slot *s = &m->slots[w];
if (s->pid > 0) {
int status;
kill((pid_t) s->pid, SIGKILL);
cf_waitpid(s->pid, &status);
s->pid = 0;
}
if (s->fd >= 0) close(s->fd);
s->fd = -1;
}
}
/* Kills (if still suspended) and reaps the checkpoints this process owns.
* Returns the exit code of a checkpoint that was resumed meanwhile (by a
* resumed checkpoint) and has been waited for, or -1. */
static int cf_ckpts_cleanup(uint32_t from)
{
int code = -1;
zend_long me = cf_getpid();
for (uint32_t i = from; i < cf.nckpts; i++) {
cf_ckpt *c = &cf.ckpts[i];
if (c->owner == me || c->parent == me) {
bool stopped = cf_pid_stopped(c->pid);
if (stopped) kill((pid_t) c->pid, SIGKILL);
if (c->parent == me) {
int status;
cf_waitpid(c->pid, &status);
/* SIGKILL = a checkpoint killed while suspended (here or by the
* resumed process at its end); anything else = it was resumed */
if (!stopped && !(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)) code = cf_status_code(status);
}
}
if (c->wake_fd >= 0) close(c->wake_fd);
c->wake_fd = -1;
}
if (from < cf.nckpts) cf.nckpts = from;
return code;
}
#endif /* !CRYO_BACKEND_WASM */
/* ------------------------------------------------------------------------ */
/* Messages to the master */
/* ------------------------------------------------------------------------ */
static void cf_dispatch(cf_master *m, zend_long w, zval *data)
{
zend_long pid = (w >= 0 && w < m->n) ? m->slots[w].last_pid : 0;
if (m->on_message) {
zval params[3], ret;
ZVAL_COPY(&params[0], data);
ZVAL_LONG(&params[1], w);
ZVAL_LONG(&params[2], pid);
ZVAL_UNDEF(&ret);
call_user_function(NULL, NULL, m->on_message, &ret, 3, params);
zval_ptr_dtor(&ret);
zval_ptr_dtor(&params[0]);
return;
}
zval a;
array_init(&a);
add_assoc_long(&a, "worker", w);
add_assoc_long(&a, "pid", pid);
Z_TRY_ADDREF_P(data);
add_assoc_zval(&a, "data", data);
q_push(&cf.mbox, &a);
}
/* ------------------------------------------------------------------------ */
/* cryo_fork_pool() */
/* ------------------------------------------------------------------------ */
typedef struct {
zend_fcall_info *fci;
zend_fcall_info_cache *fcc;
bool has_jobs;
zend_long max_jobs;
zend_long max_memory;
zval *on_result;
zval *on_error;
} cf_pool;
/* Calls job(...args) (args NULL: job(worker)) and fills result with
* ['ok' => value] or ['error' => message]. Returns true when the job called
* exit() (EG(exception) is then the unwind exit, result is not set). */
static bool pool_call(cf_pool *pc, zval *args, zend_long worker, zval *result)
{
zend_fcall_info *fci = pc->fci;
zval retval, wid;
ZVAL_UNDEF(&retval);
fci->retval = &retval;
fci->named_params = NULL;
if (!args) {
ZVAL_LONG(&wid, worker);
fci->params = &wid;
fci->param_count = 1;
} else if (Z_TYPE_P(args) == IS_ARRAY) {
fci->params = NULL;
fci->param_count = 0;
fci->named_params = Z_ARRVAL_P(args); /* list => positional, string keys => named */
} else {
fci->params = args;
fci->param_count = 1;
}
zend_call_function(fci, pc->fcc);
fci->named_params = NULL;
fci->params = NULL;
fci->param_count = 0;
if (EG(exception)) {
if (zend_is_unwind_exit(EG(exception)) || zend_is_graceful_exit(EG(exception))) {
zval_ptr_dtor(&retval);
return true;
}
array_init(result);
add_assoc_str(result, "error", cf_exception_message());
zend_clear_exception();
} else {
array_init(result);
add_assoc_zval(result, "ok", &retval);
ZVAL_UNDEF(&retval);
}
zval_ptr_dtor(&retval);
return false;
}
static void pool_callback(zval *cb, zval *a, zval *b, zend_long w)
{
if (!cb) return;
zval params[3], ret;
ZVAL_COPY(&params[0], a);
ZVAL_COPY(&params[1], b);
ZVAL_LONG(&params[2], w);
ZVAL_UNDEF(&ret);
call_user_function(NULL, NULL, cb, &ret, 3, params);
zval_ptr_dtor(&ret);
zval_ptr_dtor(&params[0]);
zval_ptr_dtor(&params[1]);
}
static void pool_error(cf_pool *pc, zend_string *msg, zval *key, zend_long w)
{
if (pc->on_error) {
zval m;
ZVAL_STR_COPY(&m, msg);
pool_callback(pc->on_error, &m, key, w);
zval_ptr_dtor(&m);
} else {
php_error_docref(NULL, E_WARNING, "Job failed: %s", ZSTR_VAL(msg));
}
}
/* Result of one job (or final value of a worker): true when ok. */
static bool pool_deliver(cf_pool *pc, zval *res, zval *key, zend_long w, zval *rv)
{
zval *ok = res && Z_TYPE_P(res) == IS_ARRAY ? zend_hash_str_find(Z_ARRVAL_P(res), "ok", 2) : NULL;
zval *err = res && Z_TYPE_P(res) == IS_ARRAY ? zend_hash_str_find(Z_ARRVAL_P(res), "error", 5) : NULL;
if (ok) {
Z_TRY_ADDREF_P(ok);
if (Z_TYPE_P(key) == IS_STRING) zend_hash_update(Z_ARRVAL_P(rv), Z_STR_P(key), ok);
else zend_hash_index_update(Z_ARRVAL_P(rv), Z_LVAL_P(key), ok);
pool_callback(pc->on_result, ok, key, w);
return true;
}
zend_string *m = err && Z_TYPE_P(err) == IS_STRING
? zend_string_copy(Z_STR_P(err)) : zend_string_init("the result cannot be decoded", 28, 0);
pool_error(pc, m, key, w);
zend_string_release(m);
return false;
}
static bool cf_limits_reached(zend_long jobs, zend_long max_jobs, zend_long max_memory)
{
if (max_jobs > 0 && jobs >= max_jobs) return true;
if (max_memory > 0 && (zend_long) zend_memory_usage(0) > max_memory) return true;
return false;
}
#ifndef CRYO_BACKEND_WASM
static void pool_send_result(int fd, zval *res)
{
if (!frame_send(fd, CF_RESULT, res) && EG(exception)) {
zend_clear_exception();
zval err;
array_init(&err);
add_assoc_string(&err, "error", "the result cannot be serialized");
frame_send(fd, CF_RESULT, &err);
zval_ptr_dtor(&err);
}
}
/* Body of a worker process. */
static ZEND_NORETURN void pool_worker(cf_pool *pc, zend_long w)
{
int fd = cf.parent_fd;
zend_long n = 0;
cryo_hooks_run_thaw();
cf_report_exception("cryo_on_thaw hook in a worker");
if (pc->has_jobs) {
for (;;) {
uint8_t t;
zval msg;
if (frame_recv(fd, &t, &msg) != 1) break;
if (t == CF_MSG) {
q_push(&cf.inbox, &msg);
continue;
}
if (t != CF_JOB) {
zval_ptr_dtor(&msg);
if (t == CF_STOP) break;
continue;
}
zval res;
bool exited = pool_call(pc, &msg, w, &res);
zval_ptr_dtor(&msg);
if (exited) cf_worker_exit((int) EG(exit_status));
cf_flush();
/* RECYCLE before the result: the master gives it no new job */
bool recycle = cf_limits_reached(++n, pc->max_jobs, pc->max_memory);
if (recycle) frame_send(fd, CF_RECYCLE, NULL);
pool_send_result(fd, &res);
zval_ptr_dtor(&res);
if (recycle) break;
}
cf_worker_exit(0);
}
/* no jobs: job(worker) again as long as it returns true */
for (;;) {
zval res;
if (pool_call(pc, NULL, w, &res)) cf_worker_exit((int) EG(exit_status));
zval *ok = zend_hash_str_find(Z_ARRVAL(res), "ok", 2);
if (ok && Z_TYPE_P(ok) == IS_TRUE) {
zval_ptr_dtor(&res);
if (cf_limits_reached(++n, pc->max_jobs, pc->max_memory)) {
cf_flush();
frame_send(fd, CF_RECYCLE, NULL);
cf_worker_exit(0);
}
continue;
}
bool failed = ok == NULL;
cf_flush();
pool_send_result(fd, &res);
zval_ptr_dtor(&res);
cf_worker_exit(failed ? 1 : 0);
}
}
static bool pool_spawn(cf_master *m, zend_long w, cf_pool *pc)
{
cf_slot *s = &m->slots[w];
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) {
php_error_docref(NULL, E_WARNING, "Cannot create the worker channel: %s", strerror(errno));
s->state = SLOT_DEAD;
return false;
}
cf_flush();
pid_t pid = fork();
if (pid == 0) {
close(sv[0]);
cf_child_setup(sv[1], w, "worker");
pool_worker(pc, w);
}
close(sv[1]);
if (pid < 0) {
php_error_docref(NULL, E_WARNING, "Cannot fork a worker: %s", strerror(errno));
close(sv[0]);
s->state = SLOT_DEAD;
return false;
}
s->pid = s->last_pid = pid;
s->fd = sv[0];
s->state = pc->has_jobs ? SLOT_IDLE : SLOT_BUSY;
s->job = -1;
s->recycle = s->reported = false;
s->runs++;
return true;
}
#endif /* !CRYO_BACKEND_WASM */
/* cryo_fork_pool(callable $job, array $options = []): array — see cryo.stub.php */
PHP_FUNCTION(cryo_fork_pool)
{
zend_fcall_info fci;
zend_fcall_info_cache fcc;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_FUNC(fci, fcc)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(opts)
ZEND_PARSE_PARAMETERS_END();
cf_pool pc;
memset(&pc, 0, sizeof(pc));
pc.fci = &fci;
pc.fcc = &fcc;
zval *zjobs = cf_opt(opts, "jobs");
pc.has_jobs = zjobs != NULL;
if (zjobs && Z_TYPE_P(zjobs) != IS_ARRAY) {
zend_argument_value_error(2, "option \"jobs\" must be an array");
RETURN_THROWS();
}
pc.max_jobs = cf_opt_long(opts, "max_jobs", 0);
pc.max_memory = cf_opt_long(opts, "max_memory", 0);
pc.on_result = cf_opt(opts, "on_result");
pc.on_error = cf_opt(opts, "on_error");
zval *on_message = cf_opt(opts, "on_message");
if (on_message && !zend_is_callable(on_message, 0, NULL)) {
zend_argument_value_error(2, "option \"on_message\" must be a valid callback");
RETURN_THROWS();
}
bool respawn = cf_opt_bool(opts, "respawn", true);
zend_long respawn_limit = cf_opt_long(opts, "respawn_limit", CF_RESPAWN_LIMIT);
zend_long workers = cf_opt_long(opts, "workers", 0);
if (workers <= 0) workers = cf_cpus();
uint32_t njobs = pc.has_jobs ? zend_hash_num_elements(Z_ARRVAL_P(zjobs)) : 0;
if (pc.has_jobs && (zend_long) njobs < workers) workers = njobs;
array_init(return_value);
cf.had_pool = true;
if (pc.has_jobs && njobs == 0) return;
zval jobs;
ZVAL_UNDEF(&jobs);
zval *keys = NULL, *jargs = NULL;
if (pc.has_jobs) {
ZVAL_COPY(&jobs, zjobs);
keys = safe_emalloc(njobs, sizeof(zval), 0);
jargs = safe_emalloc(njobs, sizeof(zval), 0);
uint32_t i = 0;
zend_string *k;
zend_ulong h;
zval *v;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(jobs), h, k, v) {
if (k) ZVAL_STR_COPY(&keys[i], k); else ZVAL_LONG(&keys[i], h);
ZVAL_COPY_VALUE(&jargs[i], v);
i++;
} ZEND_HASH_FOREACH_END();
}
cf_master m;
memset(&m, 0, sizeof(m));
m.n = workers;
m.slots = ecalloc((size_t) workers, sizeof(cf_slot));
for (zend_long w = 0; w < workers; w++) {
m.slots[w].fd = -1;
m.slots[w].job = -1;
}
m.on_message = on_message;
ZVAL_UNDEF(&m.pending);
m.prev = cf.master;
cf.master = &m;
#ifdef CRYO_BACKEND_WASM
/* no processes: one worker (0) runs everything here, in order */
(void) respawn;
(void) respawn_limit;
if (pc.has_jobs) {
for (uint32_t i = 0; i < njobs && !EG(exception); i++) {
zval res;
cf.in_job = true;
bool exited = pool_call(&pc, &jargs[i], 0, &res);
cf.in_job = false;
if (exited) break;
pool_deliver(&pc, &res, &keys[i], 0, return_value);
zval_ptr_dtor(&res);
}
} else {
while (!EG(exception)) {
zval res, key;
cf.in_job = true;
bool exited = pool_call(&pc, NULL, 0, &res);
cf.in_job = false;
if (exited) break;
zval *ok = zend_hash_str_find(Z_ARRVAL(res), "ok", 2);
if (ok && Z_TYPE_P(ok) == IS_TRUE) {
zval_ptr_dtor(&res);
continue;
}
ZVAL_LONG(&key, 0);
pool_deliver(&pc, &res, &key, 0, return_value);
zval_ptr_dtor(&res);
break;
}
}
q_clear(&cf.inbox);
#else
cf_signals_install(&m);
uint32_t next = 0, done = 0, nreq = 0;
uint32_t *requeue = pc.has_jobs ? safe_emalloc(njobs, sizeof(uint32_t), 0) : NULL;
zend_long fails = 0;
bool warned = false;
for (zend_long w = 0; w < workers; w++) pool_spawn(&m, w, &pc);
for (;;) {
if (EG(exception)) {
cf_master_abort(&m);
break;
}
if (pc.has_jobs) {
for (zend_long w = 0; w < workers && !m.stopping; w++) {
cf_slot *s = &m.slots[w];
if (s->state != SLOT_IDLE || s->pid <= 0 || s->fd < 0) continue;
if (nreq == 0 && next >= njobs) break;
uint32_t j = nreq ? requeue[--nreq] : next++;
if (frame_send(s->fd, CF_JOB, &jargs[j])) {
s->state = SLOT_BUSY;
s->job = (int32_t) j;
} else if (EG(exception)) {
zend_clear_exception();
zend_string *msg = zend_string_init("the job arguments cannot be sent", 32, 0);
pool_error(&pc, msg, &keys[j], w);
zend_string_release(msg);
done++;
} else {
requeue[nreq++] = j; /* broken channel: its exit event follows */
}
}
if (done >= njobs) break;
}
bool alive = false;
for (zend_long w = 0; w < workers; w++) if (m.slots[w].pid > 0) alive = true;
if (!alive) break;
cf_event e;
int k = cf_master_wait(&m, -1, false, &e);
if (k == EV_NONE) continue;
cf_slot *s = &m.slots[e.w];
if (k == EV_FRAME) {
if (e.t == CF_MSG) {
cf_dispatch(&m, e.w, &e.v);
} else if (e.t == CF_RECYCLE) {
s->recycle = true;
} else if (e.t == CF_RESULT) {
if (pc.has_jobs && s->state == SLOT_BUSY && s->job >= 0) {
uint32_t j = (uint32_t) s->job;
s->job = -1;
s->state = s->recycle ? SLOT_DONE : SLOT_IDLE;
done++;
if (pool_deliver(&pc, &e.v, &keys[j], e.w, return_value)) fails = 0;
} else if (!pc.has_jobs) {
zval key;
ZVAL_LONG(&key, e.w);
s->reported = true;
if (pool_deliver(&pc, &e.v, &key, e.w, return_value)) fails = 0;
}
}
zval_ptr_dtor(&e.v);
continue;
}
/* EV_EXIT */
bool normal = WIFEXITED(e.status) && WEXITSTATUS(e.status) == 0;
bool abnormal = false, replace = false;
if (pc.has_jobs && s->state == SLOT_BUSY && s->job >= 0) {
zend_string *msg = WIFSIGNALED(e.status)
? zend_strpprintf(0, "worker crashed (signal %d)", WTERMSIG(e.status))
: zend_strpprintf(0, "worker exited (status %d)", cf_status_code(e.status));
pool_error(&pc, msg, &keys[s->job], e.w);
zend_string_release(msg);
s->job = -1;
done++;
abnormal = true;
} else if (s->recycle) {
replace = true;
} else if (!pc.has_jobs && normal) {
fails = 0;
} else {
if (!pc.has_jobs && !s->reported) {
zval key;
ZVAL_LONG(&key, e.w);
zend_string *msg = WIFSIGNALED(e.status)
? zend_strpprintf(0, "worker crashed (signal %d)", WTERMSIG(e.status))
: zend_strpprintf(0, "worker exited (status %d)", cf_status_code(e.status));
pool_error(&pc, msg, &key, e.w);
zend_string_release(msg);
}
abnormal = true;
}
if (abnormal) {
if (respawn && ++fails <= respawn_limit) {
replace = true;
} else if (respawn && !warned) {
warned = true;
php_error_docref(NULL, E_WARNING, "Workers keep dying: not respawned after "
ZEND_LONG_FMT " deaths in a row", fails - 1);
}
}
bool work_left = !pc.has_jobs || nreq > 0 || next < njobs;
if (replace && work_left && !m.stopping && !EG(exception)) {
pool_spawn(&m, e.w, &pc);
} else {
s->state = abnormal && !replace ? SLOT_DEAD : SLOT_DONE;
}
}
/* stop the idle workers */
for (zend_long w = 0; w < workers; w++) {
if (m.slots[w].pid > 0 && m.slots[w].fd >= 0) frame_send(m.slots[w].fd, CF_STOP, NULL);
}
for (;;) {
cf_event e;
bool alive = false;
for (zend_long w = 0; w < workers; w++) if (m.slots[w].pid > 0) alive = true;
if (!alive) break;
int k = cf_master_wait(&m, -1, false, &e);
if (k == EV_FRAME && e.t == CF_MSG && !EG(exception)) cf_dispatch(&m, e.w, &e.v);
if (k == EV_FRAME) zval_ptr_dtor(&e.v);
}
/* messages that arrived after the last result */
cf_event e;
while (pending_take(&m, true, &e)) {
if (!EG(exception)) cf_dispatch(&m, e.w, &e.v);
zval_ptr_dtor(&e.v);
}
if (pc.has_jobs && done < njobs && !EG(exception)) {
php_error_docref(NULL, E_WARNING, "%u job(s) not run: every worker died", njobs - done);
}
for (zend_long w = 0; w < workers; w++) if (m.slots[w].fd >= 0) close(m.slots[w].fd);
if (requeue) efree(requeue);
cf_signals_restore(&m);
if (m.stopping) raise(m.stopping);
#endif
cf.master = m.prev;
q_clear(&m.pending);
efree(m.slots);
if (pc.has_jobs) {
for (uint32_t i = 0; i < njobs; i++) zval_ptr_dtor(&keys[i]);
efree(keys);
efree(jargs);
zval_ptr_dtor(&jobs);
}
}
/* ------------------------------------------------------------------------ */
/* Channel */
/* ------------------------------------------------------------------------ */
/* cryo_fork_send(mixed $data, ?int $worker = null): void */
PHP_FUNCTION(cryo_fork_send)
{
zval *data;
zend_long worker = 0;
bool worker_null = true;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(data)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(worker, worker_null)
ZEND_PARSE_PARAMETERS_END();
#ifdef CRYO_BACKEND_WASM
if (cf.master && cf.in_job && worker_null) {
cf_dispatch(cf.master, 0, data);
return;
}
if (cf.master && !cf.in_job) {
zval c;
ZVAL_COPY(&c, data);
q_push(&cf.inbox, &c);
return;
}
zend_string *s = cf_serialize(data);
if (!s) RETURN_THROWS();
int32_t r = cryo_host_send(worker_null ? 0 : (int32_t) worker, (const uint8_t *) ZSTR_VAL(s), (uint32_t) ZSTR_LEN(s));
zend_string_release(s);
if (r < 0) {
zend_throw_exception(cryo_ce_exception, "cryo_fork_send(): the host refused the message", 0);
RETURN_THROWS();
}
#else
if (!worker_null || (cf.parent_fd < 0 && cf.master)) {
cf_master *m = cf.master;
if (!m) {
zend_throw_exception(cryo_ce_exception, "cryo_fork_send(): no running pool to send to", 0);
RETURN_THROWS();
}
if (!worker_null && (worker < 0 || worker >= m->n || m->slots[worker].fd < 0)) {
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_fork_send(): worker " ZEND_LONG_FMT " is not running", worker);
RETURN_THROWS();
}
zend_string *s = cf_serialize(data);
if (!s) RETURN_THROWS();
for (zend_long w = 0; w < m->n; w++) {
if ((worker_null || w == worker) && m->slots[w].fd >= 0) frame_send_str(m->slots[w].fd, CF_MSG, s);
}
zend_string_release(s);
return;
}
if (cf.parent_fd < 0) {
zend_throw_exception(cryo_ce_exception, "cryo_fork_send(): this process has no fork channel", 0);
RETURN_THROWS();
}
if (!frame_send(cf.parent_fd, CF_MSG, data)) {
if (!EG(exception)) zend_throw_exception(cryo_ce_exception, "cryo_fork_send(): the channel to the master is closed", 0);
RETURN_THROWS();
}
#endif
}
/* cryo_fork_recv(?float $timeout = null): mixed */
PHP_FUNCTION(cryo_fork_recv)
{
double timeout = 0;
bool tnull = true;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE_OR_NULL(timeout, tnull)
ZEND_PARSE_PARAMETERS_END();
int ms = -1;
if (!tnull) ms = timeout <= 0 ? 0 : (timeout * 1000.0 >= (double) INT_MAX ? INT_MAX : (int) (timeout * 1000.0));
#ifdef CRYO_BACKEND_WASM
if (cf.master) {
if (cf.in_job ? q_shift(&cf.inbox, return_value) : q_shift(&cf.mbox, return_value)) return;
RETURN_NULL();
}
if (q_shift(&cf.mbox, return_value)) return;
uint32_t cap = 65536;
for (;;) {
uint8_t *buf = emalloc(cap);
int32_t r = cryo_host_recv(0, buf, cap, ms);
if (r < 0) {
efree(buf);
RETURN_NULL();
}
if ((uint32_t) r > cap) {
efree(buf);
cap = (uint32_t) r;
continue;
}
zval v;
bool ok = cf_unserialize((const char *) buf, (size_t) r, &v);
efree(buf);
if (!ok) {
if (!EG(exception)) zend_throw_exception(cryo_ce_exception, "cryo_fork_recv(): undecodable message", 0);
RETURN_THROWS();
}
RETURN_COPY_VALUE(&v);
}
#else
if (cf.master) {
if (q_shift(&cf.mbox, return_value)) return;
cf_event e;
if (cf_master_wait(cf.master, ms, true, &e) != EV_FRAME) RETURN_NULL();
array_init(return_value);
add_assoc_long(return_value, "worker", e.w);
add_assoc_long(return_value, "pid", cf.master->slots[e.w].last_pid);
add_assoc_zval(return_value, "data", &e.v);
return;
}
if (q_shift(&cf.mbox, return_value)) return;
if (cf.parent_fd >= 0) {
if (q_shift(&cf.inbox, return_value)) return;
double deadline = cf_now_ms() + (ms > 0 ? ms : 0);
for (;;) {
int left = ms;
if (ms >= 0) {
double l = deadline - cf_now_ms();
left = l <= 0 ? 0 : (int) l + 1;
}
if (!cf_readable(cf.parent_fd, left)) RETURN_NULL();
uint8_t t;
zval v;
if (frame_recv(cf.parent_fd, &t, &v) != 1) {
zend_throw_exception(cryo_ce_exception, "cryo_fork_recv(): the channel to the master is closed", 0);
RETURN_THROWS();
}
if (t == CF_MSG) {
if (Z_ISUNDEF(v)) RETURN_NULL();
RETURN_COPY_VALUE(&v);
}
zval_ptr_dtor(&v);
}
}
if (cf.had_pool) RETURN_NULL();
zend_throw_exception(cryo_ce_exception, "cryo_fork_recv(): this process has no fork channel", 0);
RETURN_THROWS();
#endif
}
/* ------------------------------------------------------------------------ */
/* Checkpoints */
/* ------------------------------------------------------------------------ */
#ifndef CRYO_BACKEND_WASM
static ZEND_NORETURN void cf_supervisor_exit(cf_master *m, int code)
{
cf_ckpts_cleanup(0);
cf_flush();
cf_signals_restore(m);
if (m->stopping) raise(m->stopping);
_exit(code);
}
/* 0 in the master, CRYO_FROZEN / CRYO_THAWED in the new child */
static zend_long sup_spawn(cf_master *m, zend_long w, int variant, zend_long value, HashTable *opts)
{
cf_slot *s = &m->slots[w];
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) {
php_error_docref(NULL, E_WARNING, "Cannot create the child channel: %s", strerror(errno));
s->state = SLOT_DEAD;
return 0;
}
cf_flush();
pid_t pid = fork();
if (pid == 0) {
close(sv[0]);
cf_child_setup(sv[1], w, variant == V_TEMPLATE ? "template" : "child");
if (variant == V_TEMPLATE) {
cf.template_child = true;
cf.max_jobs = cf_opt_long(opts, "max_jobs", 0);
cf.max_memory = cf_opt_long(opts, "max_memory", 0);
}
return value;
}
close(sv[1]);
if (pid < 0) {
php_error_docref(NULL, E_WARNING, "Cannot fork: %s", strerror(errno));
close(sv[0]);
s->state = SLOT_DEAD;
return 0;
}
s->pid = s->last_pid = pid;
s->fd = sv[0];
s->state = SLOT_BUSY;
s->recycle = false;
s->runs++;
return 0;
}
/* variants child and template: this process becomes a supervisor that never
* returns; the call returns in each forked child */
static zend_long cf_supervise(int variant, HashTable *opts)
{
zend_long workers = variant == V_CHILD ? 1 : cf_opt_long(opts, "workers", 0);
if (workers <= 0) workers = cf_cpus();
zend_long max_runs = cf_opt_long(opts, "max_runs", 0);
bool respawn = cf_opt_bool(opts, "respawn", true);
zend_long respawn_limit = cf_opt_long(opts, "respawn_limit", CF_RESPAWN_LIMIT);
zval *on_message = cf_opt(opts, "on_message");
if (on_message && !zend_is_callable(on_message, 0, NULL)) {
zend_value_error("option \"on_message\" must be a valid callback");
return 0;
}
cf_master m;
memset(&m, 0, sizeof(m));
m.n = workers;
m.slots = ecalloc((size_t) workers, sizeof(cf_slot));
for (zend_long w = 0; w < workers; w++) m.slots[w].fd = -1;
m.on_message = on_message;
ZVAL_UNDEF(&m.pending);
m.prev = cf.master;
cf.master = &m;
cf_signals_install(&m);
zend_long result = 0, total = 0, fails = 0;
int last_bad = 0;
zend_long first = variant == V_TEMPLATE ? CRYO_THAWED : CRYO_FROZEN;
for (zend_long w = 0; w < workers; w++) {
result = sup_spawn(&m, w, variant, first, opts);
if (result) goto child;
if (m.slots[w].pid > 0) total++;
}
if (total == 0) {
/* nothing forked: no supervisor */
cf.master = m.prev;
cf_signals_restore(&m);
efree(m.slots);
zend_throw_exception(cryo_ce_exception, "cryo_fork(): cannot fork", 0);
return 0;
}
for (;;) {
bool alive = false;
for (zend_long w = 0; w < workers; w++) if (m.slots[w].pid > 0) alive = true;
if (!alive) break;
cf_event e;
int k = cf_master_wait(&m, -1, false, &e);
if (k == EV_NONE) continue;
cf_slot *s = &m.slots[e.w];
if (k == EV_FRAME) {
if (e.t == CF_MSG) {
cf_dispatch(&m, e.w, &e.v);
cf_report_exception("cryo_fork() on_message");
} else if (e.t == CF_RECYCLE) {
s->recycle = true;
}
zval_ptr_dtor(&e.v);
continue;
}
int code = cf_status_code(e.status);
bool again = false;
if (variant == V_CHILD) {
if (m.stopping) cf_supervisor_exit(&m, code);
if (max_runs > 0) {
if (total >= max_runs) cf_supervisor_exit(&m, code);
again = true;
} else if (code == 0) {
cf_supervisor_exit(&m, 0);
} else if (respawn && ++fails <= respawn_limit) {
again = true;
} else {
if (respawn) php_error_docref(NULL, E_WARNING, "The child keeps dying: not forked again");
cf_supervisor_exit(&m, code);
}
} else {
bool room = max_runs <= 0 || total < max_runs;
if (m.stopping) {
if (code) last_bad = code;
} else if (s->recycle) {
again = room;
fails = 0;
} else if (code == 0) {
fails = 0;
} else {
if (respawn && ++fails <= respawn_limit) {
again = room;
} else if (respawn && fails == respawn_limit + 1) {
php_error_docref(NULL, E_WARNING, "Children keep dying: not forked again");
}
if (!again) last_bad = code; /* the slot ends on a failure */
}
}
if (again) {
result = sup_spawn(&m, e.w, variant, CRYO_THAWED, opts);
if (result) goto child;
if (s->pid > 0) total++;
else if (variant == V_CHILD) cf_supervisor_exit(&m, code ? code : 1);
} else {
s->state = code == 0 ? SLOT_DONE : SLOT_DEAD;
}
}
cf_supervisor_exit(&m, last_bad);
child:
/* cf_child_setup() closed the master's sockets and restored the signals */
efree(m.slots);
return result;
}
/* variant parent: a forked copy stops at once and waits to be resumed by
* cryo_thaw_fork() */
static zend_long cf_checkpoint_parent(void)
{
int p[2];
if (pipe(p) != 0) {
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_fork(): %s", strerror(errno));
return 0;
}
fcntl(p[0], F_SETFD, FD_CLOEXEC);
fcntl(p[1], F_SETFD, FD_CLOEXEC);
pid_t pgid = getpgrp();
cf_flush();
pid_t pid = fork();
if (pid < 0) {
int err = errno;
close(p[0]);
close(p[1]);
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_fork(): cannot fork: %s", strerror(err));
return 0;
}
if (pid == 0) {
close(p[1]);
fcntl(p[0], F_SETFL, O_NONBLOCK);
/* own process group: terminal signals do not reach the checkpoint;
* SIGHUP (orphaned group) is ignored while suspended */
setpgid(0, 0);
struct sigaction ign, old_hup;
memset(&ign, 0, sizeof(ign));
sigemptyset(&ign.sa_mask);
ign.sa_handler = SIG_IGN;
sigaction(SIGHUP, &ign, &old_hup);
for (;;) {
raise(SIGSTOP);
char b;
ssize_t r = read(p[0], &b, 1);
if (r == 1) break;
if (r == 0) _exit(0); /* nobody can resume it any more */
/* woken without the token (kill -CONT, orphaned group): stop again */
}
close(p[0]);
sigaction(SIGHUP, &old_hup, NULL);
setpgid(0, pgid); /* back in the original job when it still exists */
zend_long me = cf_getpid();
for (uint32_t i = 0; i < cf.nckpts; i++) cf.ckpts[i].owner = me; /* adopt the older ones */
cf.role = "thawed";
cryo_process_atfork_child();
return CRYO_THAWED;
}
close(p[0]);
int st = 0;
while (waitpid(pid, &st, WUNTRACED) < 0 && errno == EINTR);
if (!WIFSTOPPED(st)) {
close(p[1]);
zend_throw_exception(cryo_ce_exception, "cryo_fork(): the checkpoint process died", 0);
return 0;
}
cf_ckpt_push(pid, p[1]);
return CRYO_FROZEN;
}
#endif /* !CRYO_BACKEND_WASM */
zend_long cryo_fork_checkpoint(zend_string *variant, HashTable *opts)
{
int v;
if (!variant || ZSTR_LEN(variant) == 0 || zend_string_equals_literal(variant, "child")) v = V_CHILD;
else if (zend_string_equals_literal(variant, "parent")) v = V_PARENT;
else if (zend_string_equals_literal(variant, "template")) v = V_TEMPLATE;
else {
zend_value_error("Fork variant must be \"child\", \"parent\" or \"template\", \"%s\" given", ZSTR_VAL(variant));
return 0;
}
if (cf.master) {
zend_throw_exception(cryo_ce_exception, "cryo_fork(): not available inside a running pool callback", 0);
return 0;
}
#ifdef CRYO_BACKEND_WASM
(void) opts;
cf_flush();
int32_t id = cryo_host_clone((uint32_t) v);
if (id < 0) {
zend_throw_exception(cryo_ce_exception, "cryo_fork(): the host cannot clone this instance", 0);
return 0;
}
if (id == 0) {
cf.role = v == V_TEMPLATE ? "template" : (v == V_PARENT ? "thawed" : "child");
cryo_hooks_run_thaw();
return CRYO_THAWED;
}
if (v == V_PARENT) cf_ckpt_push(id, -1);
return CRYO_FROZEN;
#else
zend_long r = v == V_PARENT ? cf_checkpoint_parent() : cf_supervise(v, opts);
if (r == CRYO_THAWED || (r == CRYO_FROZEN && v == V_CHILD)) {
/* a new process: reopen what cannot be shared */
cryo_hooks_run_thaw();
}
return r;
#endif
}
/* cryo_fork(string $variant = 'child', array $options = []): int */
PHP_FUNCTION(cryo_fork)
{
zend_string *variant = NULL;
HashTable *opts = NULL;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_STR(variant)
Z_PARAM_ARRAY_HT(opts)
ZEND_PARSE_PARAMETERS_END();
zend_long r = cryo_fork_checkpoint(variant, opts);
if (EG(exception)) RETURN_THROWS();
RETURN_LONG(r);
}
/* cryo_thaw_fork(int $pid): never */
PHP_FUNCTION(cryo_thaw_fork)
{
zend_long pid;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(pid)
ZEND_PARSE_PARAMETERS_END();
uint32_t idx = 0;
while (idx < cf.nckpts && cf.ckpts[idx].pid != pid) idx++;
if (idx == cf.nckpts) {
zend_throw_exception_ex(cryo_ce_exception, 0, "cryo_thaw_fork(): " ZEND_LONG_FMT " is not a suspended checkpoint of this process", pid);
RETURN_THROWS();
}
cf_ckpt c = cf.ckpts[idx];
cf_flush();
#ifdef CRYO_BACKEND_WASM
/* later checkpoints are unknown to the resumed instance */
for (uint32_t i = idx + 1; i < cf.nckpts; i++) cryo_host_kill((int32_t) cf.ckpts[i].pid);
cf.nckpts = idx;
cryo_host_send((int32_t) c.pid, (const uint8_t *) "", 0); /* the host resumes the clone */
php_output_end_all();
EG(exit_status) = 0;
zend_throw_unwind_exit();
RETURN_THROWS();
#else
/* checkpoints taken after it are unknown to the resumed process: killed */
cf_ckpts_cleanup(idx + 1);
cf.nckpts = idx; /* the older ones stay: the resumed process owns them now */
char b = 'T';
ssize_t wr;
do { wr = write(c.wake_fd, &b, 1); } while (wr < 0 && errno == EINTR);
close(c.wake_fd);
kill((pid_t) c.pid, SIGCONT);
if (c.parent != cf_getpid()) _exit(0);
/* our own child: this process runs no more PHP code, it only waits for
* the resumed one to pass on its exit status (no orphan, shells see the
* real result) */
int status;
cf_waitpid(c.pid, &status);
int code = cf_status_code(status);
for (uint32_t i = 0; i < cf.nckpts; i++) cf.ckpts[i].owner = cf_getpid();
int later = cf_ckpts_cleanup(0);
_exit(later >= 0 ? later : code);
#endif
}
/* cryo_fork_job_done(): bool — end of one job in a template child */
PHP_FUNCTION(cryo_fork_job_done)
{
ZEND_PARSE_PARAMETERS_NONE();
if (!cf.template_child) RETURN_FALSE;
if (cf_limits_reached(++cf.jobs_done, cf.max_jobs, cf.max_memory)) {
#ifndef CRYO_BACKEND_WASM
if (cf.parent_fd >= 0) frame_send(cf.parent_fd, CF_RECYCLE, NULL);
#endif
EG(exit_status) = 0;
zend_throw_unwind_exit();
RETURN_THROWS();
}
RETURN_TRUE;
}
/* cryo_fork_info(): array */
PHP_FUNCTION(cryo_fork_info)
{
ZEND_PARSE_PARAMETERS_NONE();
array_init(return_value);
add_assoc_string(return_value, "role", cf.master ? "master" : cf.role);
if (cf.worker >= 0) add_assoc_long(return_value, "worker", cf.worker);
else add_assoc_null(return_value, "worker");
add_assoc_long(return_value, "pid", cf_getpid());
add_assoc_bool(return_value, "channel", cf.parent_fd >= 0);
add_assoc_long(return_value, "jobs", cf.jobs_done);
zval list;
array_init(&list);
for (uint32_t i = 0; i < cf.nckpts; i++) add_next_index_long(&list, cf.ckpts[i].pid);
add_assoc_zval(return_value, "suspended", &list);
}
/* ------------------------------------------------------------------------ */
/* Life cycle (called from cryo_process.c) */
/* ------------------------------------------------------------------------ */
void cryo_fork_minit(void)
{
memset(&cf, 0, sizeof(cf));
cf.parent_fd = -1;
cf.worker = -1;
cf.role = "main";
ZVAL_UNDEF(&cf.inbox);
ZVAL_UNDEF(&cf.mbox);
}
void cryo_fork_rshutdown(void)
{
/* suspended checkpoints die with the process that owns them */
#ifdef CRYO_BACKEND_WASM
for (uint32_t i = 0; i < cf.nckpts; i++) cryo_host_kill((int32_t) cf.ckpts[i].pid);
cf.nckpts = 0;
#else
cf_ckpts_cleanup(0);
#endif
if (cf.ckpts) pefree(cf.ckpts, 1);
cf.ckpts = NULL;
cf.nckpts = cf.ckpt_cap = 0;
q_clear(&cf.inbox);
q_clear(&cf.mbox);
cf.had_pool = false;
}