|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +use t::APISIX 'no_plan'; |
| 19 | + |
| 20 | +log_level("info"); |
| 21 | +repeat_each(1); |
| 22 | +no_long_string(); |
| 23 | +no_root_location(); |
| 24 | + |
| 25 | + |
| 26 | +add_block_preprocessor(sub { |
| 27 | + my ($block) = @_; |
| 28 | + |
| 29 | + if (!defined $block->request) { |
| 30 | + $block->set_value("request", "GET /t"); |
| 31 | + } |
| 32 | + |
| 33 | + # Mock upstream: slow SSE server that streams chunks until the connection |
| 34 | + # is closed, tracking the final chunk count in the "test" shared dict. |
| 35 | + my $http_config = $block->http_config // <<_EOC_; |
| 36 | + server { |
| 37 | + server_name slow_openai_sse; |
| 38 | + listen 7750; |
| 39 | +
|
| 40 | + default_type 'text/event-stream'; |
| 41 | +
|
| 42 | + location /v1/chat/completions { |
| 43 | + content_by_lua_block { |
| 44 | + ngx.header["Content-Type"] = "text/event-stream" |
| 45 | + local dict = ngx.shared["test"] |
| 46 | + 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. |
| 50 | + for i = 1, 2000 do |
| 51 | + local ok, err = ngx.print( |
| 52 | + 'data: {"id":"chatcmpl-1","object":' |
| 53 | + .. '"chat.completion.chunk","choices":[{"delta":' |
| 54 | + .. '{"content":"tok"},"index":0,' |
| 55 | + .. '"finish_reason":null}],"usage":null}\\n\\n') |
| 56 | + if not ok then |
| 57 | + return |
| 58 | + end |
| 59 | + local flush_ok = ngx.flush(true) |
| 60 | + if not flush_ok then |
| 61 | + return |
| 62 | + end |
| 63 | + dict:set("upstream_chunks", i) |
| 64 | + ngx.sleep(0.03) |
| 65 | + end |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + # Probe endpoint to read the current chunk count. |
| 70 | + location /chunks { |
| 71 | + content_by_lua_block { |
| 72 | + local dict = ngx.shared["test"] |
| 73 | + ngx.say(dict:get("upstream_chunks") or 0) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +_EOC_ |
| 78 | + |
| 79 | + $block->set_value("http_config", $http_config); |
| 80 | +}); |
| 81 | + |
| 82 | + |
| 83 | +run_tests(); |
| 84 | + |
| 85 | +__DATA__ |
| 86 | +
|
| 87 | +=== TEST 1: set route for client disconnect test |
| 88 | +--- config |
| 89 | + location /t { |
| 90 | + content_by_lua_block { |
| 91 | + local t = require("lib.test_admin").test |
| 92 | + local code, body = t('/apisix/admin/routes/1', |
| 93 | + ngx.HTTP_PUT, |
| 94 | + [[{ |
| 95 | + "uri": "/anything", |
| 96 | + "plugins": { |
| 97 | + "ai-proxy": { |
| 98 | + "provider": "openai", |
| 99 | + "auth": { |
| 100 | + "header": { |
| 101 | + "Authorization": "Bearer token" |
| 102 | + } |
| 103 | + }, |
| 104 | + "options": { |
| 105 | + "model": "gpt-4", |
| 106 | + "stream": true |
| 107 | + }, |
| 108 | + "override": { |
| 109 | + "endpoint": "http://localhost:7750" |
| 110 | + }, |
| 111 | + "ssl_verify": false |
| 112 | + } |
| 113 | + } |
| 114 | + }]] |
| 115 | + ) |
| 116 | +
|
| 117 | + if code >= 300 then |
| 118 | + ngx.status = code |
| 119 | + end |
| 120 | + ngx.say(body) |
| 121 | + } |
| 122 | + } |
| 123 | +--- response_body |
| 124 | +passed |
| 125 | +
|
| 126 | +
|
| 127 | +
|
| 128 | +=== TEST 2: client disconnect aborts upstream read early |
| 129 | +--- config |
| 130 | + location /t { |
| 131 | + content_by_lua_block { |
| 132 | + local http = require("resty.http") |
| 133 | + local httpc = http.new() |
| 134 | +
|
| 135 | + local ok, err = httpc:connect({ |
| 136 | + scheme = "http", |
| 137 | + host = "localhost", |
| 138 | + port = ngx.var.server_port, |
| 139 | + }) |
| 140 | + if not ok then |
| 141 | + ngx.status = 500 |
| 142 | + ngx.say("connect failed: ", err) |
| 143 | + return |
| 144 | + end |
| 145 | +
|
| 146 | + local res, err = httpc:request({ |
| 147 | + method = "POST", |
| 148 | + headers = { ["Content-Type"] = "application/json" }, |
| 149 | + path = "/anything", |
| 150 | + body = [[{"messages": [{"role": "user", "content": "hi"}]}]], |
| 151 | + }) |
| 152 | + if not res then |
| 153 | + ngx.status = 500 |
| 154 | + ngx.say("request failed: ", err) |
| 155 | + return |
| 156 | + end |
| 157 | +
|
| 158 | + -- Read exactly 3 chunks then close the connection abruptly. |
| 159 | + for i = 1, 3 do |
| 160 | + local chunk, rerr = res.body_reader() |
| 161 | + if rerr or not chunk then |
| 162 | + ngx.status = 500 |
| 163 | + ngx.say("unexpected end of stream at chunk ", i, ": ", rerr) |
| 164 | + return |
| 165 | + end |
| 166 | + end |
| 167 | + httpc:close() |
| 168 | +
|
| 169 | + -- Allow time for the proxy to detect the disconnect and stop |
| 170 | + -- feeding the upstream connection, then capture the chunk count. |
| 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) |
| 174 | +
|
| 175 | + -- Read chunk count from the mock upstream's probe endpoint. |
| 176 | + local probe = http.new() |
| 177 | + ok, err = probe:connect({ scheme = "http", host = "localhost", port = 7750 }) |
| 178 | + if not ok then |
| 179 | + ngx.status = 500 |
| 180 | + ngx.say("probe connect failed: ", err) |
| 181 | + return |
| 182 | + end |
| 183 | + local probe_res, probe_err = probe:request({ |
| 184 | + method = "GET", |
| 185 | + path = "/chunks", |
| 186 | + headers = { Host = "localhost" }, |
| 187 | + }) |
| 188 | + if not probe_res then |
| 189 | + ngx.status = 500 |
| 190 | + ngx.say("probe request failed: ", probe_err) |
| 191 | + return |
| 192 | + end |
| 193 | + local count_str = probe_res:read_body() |
| 194 | + probe:close() |
| 195 | +
|
| 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 |
| 214 | + ngx.status = 500 |
| 215 | + ngx.say("upstream was not aborted promptly, chunks: ", count) |
| 216 | + return |
| 217 | + end |
| 218 | + ngx.say("ok, upstream aborted after ~", count, " chunks") |
| 219 | + } |
| 220 | + } |
| 221 | +--- response_body_like |
| 222 | +^ok, upstream aborted after ~\d+ chunks$ |
| 223 | +--- error_log |
| 224 | +client disconnected during AI streaming |
0 commit comments