From aae8b9b28c6510663cb63bf534269e51dcfd5d48 Mon Sep 17 00:00:00 2001 From: Lucas Holt Date: Sun, 29 Nov 2026 16:59:06 -0500 Subject: [PATCH] prowld: stop handling requests while a client's replies back up prowld kept reading and answering a client's requests even while earlier replies were still unsent. A client that sent many requests without reading the replies made PID 1 queue up to IPC_OUT_MAX (8 MiB) for it, after which further replies were dropped and the client was left waiting for them. jemalloc keeps that memory: in a test guest, 200 pipelined "list" requests left PID 1 at 45 MB instead of 2.7 MB. Once more than IPC_OUT_HIWAT (1 MiB) is queued for a client, stop dispatching its requests until the queue drains below that again. The requests are still read into the receive buffer, so a client that is blocked writing a batch of requests can get to its replies; reading stops only when the receive buffer is full (IPC_MSG_MAX). Resuming after the queue drains shares ipc_resume_client() with the end of a service action. Tested in a bhyve guest with this prowld as PID 1, also with malloc.conf junk:true,abort:true: 200 pipelined "list" requests left unread for 8 seconds all got their replies, with PID 1 at about 4.8 MB. Half-closed and slow-reading clients, actions pipelined with "list", and the existing IPC load tests pass. AI-Assisted-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Lucas Holt --- sbin/prowld/ipc.c | 72 +++++++++++++++++++++++++++++++++++--------- sbin/prowld/prowld.h | 1 + 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/sbin/prowld/ipc.c b/sbin/prowld/ipc.c index 3b79909b14..7fa00cf01e 100644 --- a/sbin/prowld/ipc.c +++ b/sbin/prowld/ipc.c @@ -90,6 +90,7 @@ static pid_t sysrc_pid; static ipc_client_t *ipc_get_client(int fd); static void ipc_send_error(int fd, const char *id, const char *message); static void ipc_dispatch_buffered(ipc_client_t *); +static void ipc_resume_client(ipc_client_t *); static uintptr_t ipc_event_tag(uintptr_t type) @@ -177,6 +178,17 @@ json_escape_str(const char *src, char *dst, size_t dstsz) /* Upper bound on queued output per client; slower readers lose messages. */ #define IPC_OUT_MAX (IPC_REPLY_MAX * 2) +/* + * Once this much output is queued for a client, its further requests are + * not handled until the queue drains below it again. They are still read + * into the receive buffer, so a client blocked writing a batch of requests + * can get to its replies; only a full receive buffer stops reading. A + * client that keeps sending requests without reading the replies is slowed + * down instead of making prowld queue replies up to IPC_OUT_MAX and then + * drop them. + */ +#define IPC_OUT_HIWAT (1024 * 1024) + /* * Drop everything queued for a client and stop watching for writability. */ @@ -295,6 +307,9 @@ ipc_send(int fd, const char *json, size_t len) /* If EVFILT_WRITE is armed, it will drain the queue in order. */ if (!c->want_write) ipc_flush_client(c); + + if (c->want_write && c->out_len - c->out_off > IPC_OUT_HIWAT) + c->out_held = true; } /* @@ -309,10 +324,37 @@ ipc_write_ready(int fd, uintptr_t tag) if (c == NULL || c->event_tag != tag) return; ipc_flush_client(c); - if (c->read_eof && !c->want_write) + if (c->out_held && c->out_len - c->out_off <= IPC_OUT_HIWAT) { + c->out_held = false; + ipc_resume_client(c); + if (!c->active) + return; + } + if (c->read_eof && !c->want_write && !c->out_held && + c->action_tag == 0) ipc_close_client(fd); } +/* + * Read and dispatch the client's requests again once it has neither a + * service action running nor too much output waiting to be sent. + */ +static void +ipc_resume_client(ipc_client_t *c) +{ + struct timespec now; + + if (!c->active || c->action_tag != 0 || c->out_held) + return; + if (ipc_watch_read(c, EV_ADD | EV_ENABLE, false) == -1 || + clock_gettime(CLOCK_MONOTONIC, &now) == -1) { + ipc_close_client(c->fd); + return; + } + c->frame_started = now.tv_sec; + ipc_dispatch_buffered(c); +} + static void ipc_send_ok(int fd, const char *id, const char *result_json) { @@ -790,7 +832,6 @@ void ipc_action_complete(uintptr_t tag, int code, const char *out) { ipc_client_t *client = NULL; - struct timespec now; for (size_t i = 0; i < nitems(g_clients); i++) { if (g_clients[i].active && g_clients[i].action_tag == tag) { @@ -808,13 +849,7 @@ ipc_action_complete(uintptr_t tag, int code, const char *out) else ipc_send_error(client->fd, client->action_id, "could not persist enable setting with sysrc"); - if (ipc_watch_read(client, EV_ADD | EV_ENABLE, false) == -1 || - clock_gettime(CLOCK_MONOTONIC, &now) == -1) { - ipc_close_client(client->fd); - return; - } - client->frame_started = now.tv_sec; - ipc_dispatch_buffered(client); + ipc_resume_client(client); } static void @@ -1512,6 +1547,7 @@ ipc_expire_clients(void) return; for (i = 0; i < IPC_MAX_CLIENTS; i++) { if (g_clients[i].active && g_clients[i].action_tag == 0 && + !g_clients[i].out_held && (!g_clients[i].completed_request || g_clients[i].buf_used != 0) && now.tv_sec - g_clients[i].frame_started >= IPC_FRAME_TIMEOUT) ipc_close_client(g_clients[i].fd); @@ -1536,6 +1572,7 @@ ipc_close_client(int fd) g_clients[i].out_len = 0; g_clients[i].out_off = 0; g_clients[i].want_write = false; + g_clients[i].out_held = false; g_clients[i].is_subscriber = false; g_clients[i].active = false; g_clients[i].fd = -1; @@ -1556,11 +1593,17 @@ ipc_read_client(int fd, uintptr_t tag, bool eof) if (client == NULL || client->event_tag != tag) return; - if (client->action_tag != 0) { + /* Requests are held, and no more fit in the receive buffer. */ + if (client->action_tag != 0 || + (client->out_held && client->buf_used == IPC_MSG_MAX)) { struct pollfd pfd = { .fd = fd }; - if (!eof) + if (!eof) { + if (client->action_tag == 0 && + ipc_watch_read(client, EV_ADD, true) == -1) + ipc_close_client(fd); return; + } /* A peer that only closed its write side can still read the reply. */ if (poll(&pfd, 1, 0) == -1 && errno == EINTR) return; @@ -1571,8 +1614,9 @@ ipc_read_client(int fd, uintptr_t tag, bool eof) } /* - * A partial frame is always shorter than IPC_MSG_MAX, so a full - * buffer can grow; complete frames were dispatched already. + * Unless requests are held, a partial frame is always shorter than + * IPC_MSG_MAX, so a full buffer can grow; complete frames were + * dispatched already. */ if (client->buf_used == client->buf_cap) { size_t ncap = client->buf_cap * 2; @@ -1628,7 +1672,7 @@ ipc_dispatch_buffered(ipc_client_t *client) { int fd = client->fd; - while (client->action_tag == 0) { + while (client->action_tag == 0 && !client->out_held) { uint32_t msglen; size_t total; diff --git a/sbin/prowld/prowld.h b/sbin/prowld/prowld.h index c53331e205..8fa87b926d 100644 --- a/sbin/prowld/prowld.h +++ b/sbin/prowld/prowld.h @@ -466,6 +466,7 @@ typedef struct ipc_client { size_t out_len; /* bytes queued in out */ size_t out_off; /* bytes of out already sent */ bool want_write; /* EVFILT_WRITE registered */ + bool out_held; /* reads held: output over HIWAT */ bool read_eof; /* drain queued output before closing */ } ipc_client_t;