This is an automated email from the ASF dual-hosted git repository.
nic-6443 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 3072a5e5c fix(ai-proxy): yield to scheduler in streaming SSE loop to
avoid worker CPU starvation (#13255)
3072a5e5c is described below
commit 3072a5e5cf9d6f66e600393375fdd2248d8f9acd
Author: Nic <[email protected]>
AuthorDate: Mon Apr 20 15:04:28 2026 +0800
fix(ai-proxy): yield to scheduler in streaming SSE loop to avoid worker CPU
starvation (#13255)
---
apisix/plugins/ai-providers/base.lua | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/apisix/plugins/ai-providers/base.lua
b/apisix/plugins/ai-providers/base.lua
index 83ac9447b..dc2d67f67 100644
--- a/apisix/plugins/ai-providers/base.lua
+++ b/apisix/plugins/ai-providers/base.lua
@@ -492,6 +492,17 @@ function _M.parse_streaming_response(self, ctx, res,
target_proto, converter, co
local status = limit_hit == "max_stream_duration_ms" and 504 or 502
return status, limit_hit .. " exceeded"
end
+
+ -- WORKAROUND, not a real fix: yield to the nginx scheduler so other
+ -- coroutines on this worker (health checks, concurrent requests) can
+ -- run. body_reader() and ngx.flush() do not yield when the upstream
+ -- socket already has data buffered or the downstream client drains
+ -- immediately, so under bursty SSE upstreams this loop can monopolize
+ -- the worker CPU. ngx.sleep(0) only prevents a single request from
+ -- monopolizing the worker; it does not bound per-stream CPU time, add
+ -- backpressure, or time out stalled streams. See #13256 for a proper
+ -- solution.
+ ngx.sleep(0)
end
end