fix: isolate logger response body buffers#13450
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a bug where multiple logger plugins (e.g., a global http-logger and a route-level file-logger) collecting the response body would share ctx._resp_body_bytes and ctx._body_buffer[ctx._plugin_name], allowing one logger's accumulation/truncation state to break another logger's collection. The fix isolates the body buffer per caller by accepting an optional body_buffer_key (the logger's conf table) and moves byte counting onto the buffer itself, while also introducing a done flag so truncated buffers stop accumulating without dropping subsequent loggers' data.
Changes:
core.response.hold_body_chunknow accepts abody_buffer_keyand stores per-keybytes/doneinstead of a request-widectx._resp_body_bytes.log-util.collect_bodypasses the pluginconfas the buffer key and removes the now-redundant pre-call byte check.- New
t/plugin/http-logger.ttests 28–29 cover globalhttp-logger+ globalfile-logger+ route-levelfile-loggerall collecting bodies simultaneously.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| apisix/core/response.lua | Adds body_buffer_key param, per-buffer byte/done tracking. |
| apisix/utils/log-util.lua | Uses plugin conf as the buffer key and drops the shared-counter guard. |
| t/plugin/http-logger.t | Adds regression test for global+route loggers collecting response body. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This fixes response body collection when loggers are configured in multiple scopes for the same request. Each logger configuration now keeps an isolated body buffer, so a route-level logger cannot overwrite the response body collected for a global logger.
The change keeps existing plugin configuration unchanged and preserves the current response body size limit behavior.
Tests: