I think I found a fix for the memory leak issue on gRPC module

Sangmin Lee leesm815 at gmail.com
Fri Apr 5 02:28:31 UTC 2024


I am sending this mail again because I did a mistake while I was writing a
mail. I didn't know, in gmail, "Ctrl - Enter" would send a mail immediately
even without a title!
I am sorry for that, so I am sending again.

Hello,

I think I found the main cause of the memory leak issue when using gRPC
stream so I made a patch for it.
Please find the test scenario and details here -- This is what I wrote.:
https://trac.nginx.org/nginx/ticket/2614

After I changed the memory pool totally on nginx and tested our workload --
long-lived gRPC streams with many connections -- using Valgrind and massif,
I was able to find what brought up the memory leak issue.
like the picture below.

[image: Screenshot from 2024-04-02 11-49-29.png]
( I am not sure whether this picture will be sent properly )

After I patched one part, it seems okay now I have tested it for 1 week
with out workload.

[image: Screenshot from 2024-04-04 15-47-14.png]
( I am not sure whether this picture will be sent properly )

But because I am not familiar with Mercurial, I couldn't find a way to
create PR like on github. I guess this mailing list is for this patch.
>From my point of view, it is more like a workaround and I think the way of
using ngx_chain_add_copy() or itself needs to be changed because it
allocates a ngx_chain_t structure using ngx_alloc_chain_link() but inside
of that, it just copies pointer, like cl->buf = in->buf;
so this ngx_chain_t instance should be dealt with differently unlike other
ngx_chain_t instances.
But I am quite new to nginx codes so my view might be wrong.
Anyhow, please go over this patch and I would like to further talk here.

--------------------------------------------------------------------------------------------------------------------------------------------

diff --git a/src/http/modules/ngx_http_grpc_module.c
b/src/http/modules/ngx_http_grpc_module.c
index dfe49c586..1db67bd0a 100644
--- a/src/http/modules/ngx_http_grpc_module.c
+++ b/src/http/modules/ngx_http_grpc_module.c
@@ -1462,6 +1462,12 @@ ngx_http_grpc_body_output_filter(void *data,
ngx_chain_t *in)
         in = in->next;
     }

+       ngx_chain_t *nl;
+       for (ngx_chain_t *dl = ctx->in; dl != in; dl = nl ) {
+               nl = dl->next;
+               ngx_free_chain(r->pool, dl);
+       }
+
     ctx->in = in;

     if (last) {

--------------------------------------------------------------------------------------------------------------------------------------------

Best regards,
Sangmin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20240405/91054afb/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot from 2024-04-02 11-49-29.png
Type: image/png
Size: 371470 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20240405/91054afb/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot from 2024-04-04 15-47-14.png
Type: image/png
Size: 65011 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20240405/91054afb/attachment-0003.png>


More information about the nginx-devel mailing list