fix: prevent apisix_llm_active_connections gauge leak when plugin exits early via ngx.exit()#13139
Open
shreemaan-abhishek wants to merge 2 commits intoapache:masterfrom
Open
Conversation
…ts early via ngx.exit() Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
…-connections-gauge-leak
nic-6443
approved these changes
Apr 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
apisix_llm_active_connectionsis a Prometheus Gauge that tracks in-flight LLM requests. The counter leaks (never decrements) whenever a plugin callsngx.exit()during request processing — not only in SSE streaming, but also in non-streaming responses.Root cause: When
ai-aliyun-content-moderation(or any other plugin) callsngx.exit()inside a phase handler (e.g.body_filter,header_filter), OpenResty terminates the current coroutine immediately. This exit is not caught by thepcallwrapping the upstream request inai-proxy/base.lua. As a result:exporter.inc_llm_active_connections(ctx)is called beforepcall(do_request)✓ngx.exit()— either mid-stream (SSE) or after receiving a complete non-streaming responseexporter.dec_llm_active_connections(ctx)placed afterpcallis never reached ✗This affects both
ai-proxyandai-proxy-multiin all request types: non-streaming chat, SSE streaming, and any other path where a downstream plugin exits early.Fix
Remove the
deccall from afterpcallinai-proxy/base.luaand instead rely solely on the log phase, which always runs even afterngx.exit(). Introduce actx.llm_active_connections_trackedflag to prevent double-decrement:ai-proxy/base.lua— increment and set flag, nodecafterpcall:ai-proxy.luaandai-proxy-multi.lualog phase:The log phase runs unconditionally regardless of how the request ended (normal completion, upstream error, or
ngx.exit()from any plugin), so the gauge is always correctly decremented.Tests
Added a regression test in
t/plugin/ai-aliyun-content-moderation.t:prometheus+ai-proxy+ai-aliyun-content-moderation(check_response=true)ngx.exit(400)apisix_llm_active_connections{...} 0in Prometheus metrics after the log phase completesAll existing tests in
t/plugin/prometheus-ai-proxy.t(40 tests) continue to pass.Checklist