You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "caogw279 (via GitHub)" <gi...@apache.org> on 2024/05/11 06:15:25 UTC

[I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

caogw279 opened a new issue, #11244:
URL: https://github.com/apache/apisix/issues/11244

   ### Current Behavior
   
   When configured kafka-logger  in global rules with include_resp_body set to true and upstream server response 128K body, the memory will increase during the test and never get down,  if the test continue, the memory usage will continue increase until the OOM happen.
   
   ### Expected Behavior
   
   The memory usage will keep in a range during the test and will return to normal after the stress stop.
   
   ### Error Logs
   
   No error logs, please have look at output of "free" and the RES of very worker process
   <img width="674" alt="apisix_memory_leak" src="https://github.com/apache/apisix/assets/27066781/33642b0c-049b-42f0-bc7d-65dbbed4afcb">
   
   
   ### Steps to Reproduce
   
   1. Run apisix in CentOS 
   2. APISIX  Config route with Admin API
   3. APISIX  Config kafka-logger to globale_rules with  include_resp_body set to true
   4. Upstream server config, make the server return a text/html which  128K or more.
   5. Stress the APISIX
   6. Monitor the system memory usage (such as via order free -h) 
   7. Check the memory usage of every worker process (such as via top)
   
   ### Environment
   
   - APISIX version (run `apisix version`): apisix 3.8.0
   - Operating system (run `uname -a`): Linux localhost.localdomain 3.10.0-1160.105.1.el7.x86_64 #1 SMP Thu Dec 7 15:39:45 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
   - OpenResty / Nginx version (run `openresty -V` or `nginx -V`): openresty/1.21.4.2
   - etcd version, if relevant (run `curl http://127.0.0.1:9090/v1/server_info`): 3.5.0
   - APISIX Dashboard version, if relevant: No APISIX Dashboard used.
   - Plugin runner version, for issues related to plugin runners: no external plugin is used
   - LuaRocks version, for installation issues (run `luarocks --version`): No luarocks is installed.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "caogw279 (via GitHub)" <gi...@apache.org>.
caogw279 commented on issue #11244:
URL: https://github.com/apache/apisix/issues/11244#issuecomment-2109329956

   From the document [,](https://apisix.apache.org/docs/apisix/control-api/) I found a method which will Triggers a full garbage collection in the HTTP subsystem, it's works,  the memory not leak ? just hold by GC ? but why doesn't GC collection work by itself, why the memory is exhausted and GC still not work  ?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "shreemaan-abhishek (via GitHub)" <gi...@apache.org>.
shreemaan-abhishek commented on issue #11244:
URL: https://github.com/apache/apisix/issues/11244#issuecomment-2926582763

   @Baoyuantop, can you try load testing with this patch: https://github.com/apache/apisix/pull/12035/files
   
   last time I can confirm I was able to fix the memory leak bug by this patch. But could not document and prove it as I did not have proper wrk tool setup in my development machine.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on issue #11244:
URL: https://github.com/apache/apisix/issues/11244#issuecomment-4964436448

   Closing this out with the conclusion from the analysis above.
   
   Summary: this is not a leak in `hold_body_chunk` — it is an unbounded batch-processor backlog of large response bodies, made visible by LuaJIT's non-moving GC and the `lj_alloc` allocator (which only returns blocks >= 128 KB to the OS and trims the arena at >= 2 MB), so RSS stays at the fragmented high-water mark after the load stops.
   
   Mitigations, both available today, no code change needed:
   
   - `max_pending_entries` (plugin metadata, e.g. `/apisix/admin/plugin_metadata/kafka-logger`) — caps the backlog and drops the excess. Added in #12338, so it is available since **3.13.0**; it did not exist in 3.8.0, which is why there was no way out at the time this was reported.
   - `max_resp_body_bytes` (plugin attribute, default 512 KB) — caps the body kept per entry.
   
   Trade-off, stated plainly: bounded memory + full bodies + a sink that can't keep up ⇒ excess must be dropped. No buffer setting is both bounded and lossless under sustained overload.
   
   #13581 adds an optional byte-precise `max_buffer_bytes` variant; it is a convenience, not a prerequisite. Please reopen (or comment there) if you still see growth with `max_pending_entries` set on a recent version.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan closed issue #11244: bug: Memory leak when logger log the traffic with large (about 128K) response body
URL: https://github.com/apache/apisix/issues/11244


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #11244:
URL: https://github.com/apache/apisix/issues/11244#issuecomment-2915491667

   After local testing with
   ```
   wrk -t10 -c100 -d300s http://127.0.0.1:9080/
   ```
   
   The memory grows by about 2G and doesn't drop at the end of the test, it doesn't drop when `/v1/gc` is called, and it recovers after restarting the container.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on issue #11244:
URL: https://github.com/apache/apisix/issues/11244#issuecomment-4784923234

   ### Root cause
   
   The growth comes from how `core.response.hold_body_chunk` accumulates the response body when a logger has `include_resp_body = true`.
   
   For every `body_filter` chunk it appends the chunk to a Lua table and, once `max_resp_body_bytes` is reached (or on eof), concatenates the whole table with `table.concat`:
   
   ```lua
   body_buffer = { chunk, n = 1, bytes = #chunk }   -- first chunk
   ...
   body_buffer[n] = chunk                            -- subsequent chunks
   ...
   local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
   body_data = str_sub(body_data, 1, max_resp_body_bytes)
   ```
   
   Two things make this expensive for large bodies (the ~128K in this report, and worse under concurrency):
   
   1. At the moment of `concat`, both the **table of all chunks** and the **full concatenated string** are alive at the same time — roughly 2x the body size per in-flight request.
   2. `concat` + `str_sub` each allocate a fresh string, so the truncation path allocates the whole concatenated body and then a second truncated copy.
   
   Under load (e.g. `wrk -c100`), many requests hold these buffers simultaneously, so the transient allocations pile up and the LuaJIT/GC arena does not return the memory to the OS — which matches the "grows and never drops" behavior reported here. It is allocation/retention pressure rather than a true unbounded leak.
   
   ### Suggested fix
   
   Accumulate into a LuaJIT `string.buffer` instead of a Lua table:
   
   ```lua
   local str_buffer = require("string.buffer")
   ...
   body_buffer = { buf = str_buffer.new(), bytes = 0 }
   ...
   body_buffer.buf:put(chunk)
   body_buffer.bytes = body_buffer.bytes + #chunk
   ...
   -- truncation: get() returns exactly the first N bytes, buffer is dropped after
   local body_data = body_buffer.buf:get(max_resp_body_bytes)
   ```
   
   `buffer:put` appends without keeping every chunk reference, and `buffer:get(n)` produces the final (optionally truncated) string in one step, so the chunk table + intermediate concatenated copy are no longer held at once. All existing semantics (`hold_the_copy`, `max_resp_body_bytes` truncation, the `done` flag, the single-chunk eof path) are preserved.
   
   Note: #12035 touched the same function but was closed without landing. I can put up a PR with the `string.buffer` approach above plus tests covering multi-chunk accumulation, byte truncation across chunks, the `done` flag and small-body passthrough.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "caogw279 (via GitHub)" <gi...@apache.org>.
caogw279 commented on issue #11244:
URL: https://github.com/apache/apisix/issues/11244#issuecomment-2106825577

   Addition:  I test it with wrk as client and take the nginx as the upstream server, and change it's index.html with 512k bytes characters,  can't reproduce the issue, resident memory of every openresty process will keep at 1 - 1.5 G  no matter how long wrk stress it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: Memory leak when logger log the traffic with large (about 128K) response body [apisix]

Posted by "shreemaan-abhishek (via GitHub)" <gi...@apache.org>.
shreemaan-abhishek commented on issue #11244:
URL: https://github.com/apache/apisix/issues/11244#issuecomment-2728208809

   @caogw279, please share the details on how you stress test APISIX.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org