[PATCH] gRPC: reinitialization of in, out, and busy chains

Maxim Dounin mdounin at mdounin.ru
Thu Feb 26 01:13:36 UTC 2026


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1772068294 -10800
#      Thu Feb 26 04:11:34 2026 +0300
# Node ID 125ca4a7d885b9c30f8745dccef669f29d91a68a
# Parent  90f2bd990f0cd4419fdff3ac12f64ebcd80ad9e9
gRPC: reinitialization of in, out, and busy chains.

If an error happens with data buffered in ctx->in (or control frames
queued in ctx->out), keeping these while switching to the next upstream
server will result in incorrect data sent to the new connection, likely
causing a failure.

Similarly, if there are buffers in ctx->busy, switching to the next
upstream server will reinitialize ngx_chain_writer() context, u->writer,
and these buffers will be forgotten.  Still, since they are in ctx->busy
and not marked as fully sent, ngx_chain_update_chains() won't look any
further, thus breaking buffers reuse and causing excessive memory usage
on long-running requests.

The fix is to clear ctx->in, ctx->out, and ctx->busy chains on request
reinitialization.

Prodded by David Carlier,
https://freenginx.org/pipermail/nginx-devel/2026-February/000934.html

diff --git a/src/http/modules/ngx_http_grpc_module.c b/src/http/modules/ngx_http_grpc_module.c
--- a/src/http/modules/ngx_http_grpc_module.c
+++ b/src/http/modules/ngx_http_grpc_module.c
@@ -1217,6 +1217,9 @@ ngx_http_grpc_reinit_request(ngx_http_re
     ctx->rst = 0;
     ctx->goaway = 0;
     ctx->connection = NULL;
+    ctx->in = NULL;
+    ctx->out = NULL;
+    ctx->busy = NULL;
     ctx->pings = 0;
     ctx->settings = 0;
     ctx->headers = 0;



More information about the nginx-devel mailing list