@@ -44,6 +44,9 @@ add_block_preprocessor(sub {
4444 ngx.header["Content-Type"] = "text/event-stream"
4545 local dict = ngx.shared["test"]
4646 dict:set("upstream_chunks", 0)
47+ -- Stream up to 2000 chunks with 30ms sleep between each.
48+ -- The proxy should abort well before this completes when
49+ -- the client disconnects.
4750 for i = 1, 2000 do
4851 local ok, err = ngx.print(
4952 'data: {"id":"chatcmpl-1","object":'
@@ -63,6 +66,7 @@ add_block_preprocessor(sub {
6366 }
6467 }
6568
69+ # Probe endpoint to read the current chunk count.
6670 location /chunks {
6771 content_by_lua_block {
6872 local dict = ngx.shared["test"]
@@ -164,7 +168,9 @@ passed
164168
165169 -- Allow time for the proxy to detect the disconnect and stop
166170 -- feeding the upstream connection, then capture the chunk count.
167- ngx.sleep(0.3)
171+ -- 1s window: unfixed path produces ~33 chunks (1000ms / 30ms per
172+ -- chunk); fixed path stops within a few chunks of the disconnect.
173+ ngx.sleep(1.0)
168174
169175 -- Read chunk count from the mock upstream's probe endpoint.
170176 local probe = http.new()
@@ -187,11 +193,24 @@ passed
187193 local count_str = probe_res:read_body()
188194 probe:close()
189195
190- local count = tonumber(count_str) or 0
191- -- Without the fix, the upstream would have produced hundreds of
192- -- chunks by now. With the fix it stops shortly after disconnect.
193- -- We expect well under 50 chunks after only 0.3s budget.
194- if count > 50 then
196+ if probe_res.status ~= 200 then
197+ ngx.status = 500
198+ ngx.say("probe status unexpected: ", probe_res.status)
199+ return
200+ end
201+
202+ local count = tonumber(count_str)
203+ if not count then
204+ ngx.status = 500
205+ ngx.say("invalid probe response: ", count_str or "nil")
206+ return
207+ end
208+
209+ -- With the fix the upstream stops shortly after client disconnect
210+ -- (well under 15 chunks). Without the fix it reaches ~33 chunks in
211+ -- the 1s observation window, so this threshold reliably catches the
212+ -- regression while leaving ample headroom for timing variation.
213+ if count > 15 then
195214 ngx.status = 500
196215 ngx.say("upstream was not aborted promptly, chunks: ", count)
197216 return
0 commit comments