diff --git a/src/junction.c b/src/junction.c index b27d3dc..99d7021 100644 --- a/src/junction.c +++ b/src/junction.c @@ -2738,7 +2738,7 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, const char *out_body, size_t out_len, const char *as_model, const char *rq_model, const char *cache_state, double req_started, int head_sent, const char *vkey_name, j_buf *capture, - char *err, size_t errlen) + int capture_client_error, char *err, size_t errlen) { char host[J_NAME_MAX], port[16], prefix[J_URL_MAX]; int tls = 0; @@ -2971,15 +2971,22 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, free(body_alloc); return -1; } - /* A review call must not write an error into the client's open stream: it is an - * internal extra call, and the caller keeps the last good answer. */ + /* A review round must not write an error into the client's stream: it is an + * internal extra call, and the caller keeps the last good answer. The original + * call of a review rule is different: the client asked for this request, so its + * provider error must be delivered, not hidden behind no_endpoint. */ if (capture && (r.status < 200 || r.status >= 300)) { - log_upstream_error_body(&up, &r, ep->name); - snprintf(err, errlen, "upstream_status_%d", r.status); - resp_free(&r); - j_conn_close(&up); - free(body_alloc); - return -1; + if (!capture_client_error) { + log_upstream_error_body(&up, &r, ep->name); + snprintf(err, errlen, "upstream_status_%d", r.status); + resp_free(&r); + j_conn_close(&up); + free(body_alloc); + return -1; + } + STATE_LOCK(); + snprintf(ep->last_error, sizeof ep->last_error, "upstream_status_%d", r.status); + STATE_UNLOCK(); } /* Once the stream head is out the caller has a 200; anything that goes wrong from * here has to be an event, and failing over is no longer possible. */ @@ -3013,6 +3020,35 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, free(body_alloc); return -2; } + if (capture && capture_client_error && (r.status < 200 || r.status >= 300)) { + char *body = NULL; + size_t body_len = 0; + if (read_full_body(&up, &r.buf, r.body_off, r.chunked, r.content_length, + ep->idle_timeout_s, deadline, &body, &body_len, + err, errlen) == 0) { + int hrc = send_head(client, r.status, r.reason[0] ? r.reason : "Error", + &r.hdrs, (long)body_len, err, errlen); + if (hrc == 0 && body_len > 0 && + j_conn_write(client, body, body_len, err, errlen) < 0) + hrc = -1; + free(body); + if (hrc != 0) { + resp_free(&r); + j_conn_close(&up); + free(body_alloc); + return -2; + } + } else { + send_simple(client, r.status, r.reason[0] ? r.reason : "Error", + "{\"error\":\"upstream_error\"}"); + } + snprintf(err, errlen, "upstream_status_%d", r.status); + t_heartbeat = NULL; + resp_free(&r); + j_conn_close(&up); + free(body_alloc); + return -2; + } char cc[J_HDR_VALUE]; int resp_no_store = !j_hdrs_find(&r.hdrs, "Cache-Control", cc, sizeof cc) && @@ -3253,7 +3289,8 @@ static int forward_with_retry(j_conn *client, j_endpoint *ep, const j_req *rq, const char *body, size_t len, const char *as_model, const char *rq_model, const char *cache_state, double started, int head_sent, const char *vkey_name, - j_buf *capture, char *err, size_t errlen) + j_buf *capture, int capture_client_error, + char *err, size_t errlen) { int tries = CFG.retry_num > 0 ? CFG.retry_num : 0; /* Ein Schlüssel mehr im Pool ist ein Versuch mehr: sonst käme das zweite Konto @@ -3284,7 +3321,8 @@ static int forward_with_retry(j_conn *client, j_endpoint *ep, const j_req *rq, t_req_retries++; } rc = forward_step(client, ep, rq, body, len, as_model, rq_model, cache_state, - started, head_sent, vkey_name, capture, err, errlen); + started, head_sent, vkey_name, capture, capture_client_error, + err, errlen); if (rc == 0 || rc == -2) return rc; if (!err_retryable(err)) return rc; } @@ -3320,14 +3358,17 @@ static int review_serve(j_conn *client, j_endpoint *ep, const j_req *rq, as_model[0] ? as_model : rq_model); if (j_review_nonstream(body, &ns, &ns_len) != 0) { snprintf(t_review_skipped, sizeof t_review_skipped, "body"); - snprintf(err, errlen, "review_body"); - return -1; + jlog("review", "round=0 endpoint=%s skipped=body", ep->name); + return forward_with_retry(client, ep, rq, body, strlen(body), as_model, rq_model, + "off", now_s(), streaming, vkey_name, NULL, 0, + err, errlen); } j_buf cap; j_buf_init(&cap); int rc = forward_with_retry(client, ep, rq, ns, ns_len, as_model, rq_model, - "off", now_s(), streaming, vkey_name, &cap, err, errlen); + "off", now_s(), streaming, vkey_name, &cap, 1, + err, errlen); if (rc != 0) { free(ns); j_buf_free(&cap); @@ -3367,7 +3408,7 @@ static int review_serve(j_conn *client, j_endpoint *ep, const j_req *rq, } j_buf_reset(&cap); int rrc = forward_with_retry(client, ep, rq, rb, rb_len, as_model, rq_model, - "off", now_s(), streaming, vkey_name, &cap, + "off", now_s(), streaming, vkey_name, &cap, 0, err, errlen); free(rb); if (rrc != 0) { @@ -4263,7 +4304,7 @@ static void handle_client(j_conn *client) vk ? vk->name : "", err, sizeof err) : forward_with_retry(client, ep, &rq, send_body, send_len, send_model, eff_model, may_cache ? "miss" : "off", started, head_sent, - vk ? vk->name : "", NULL, err, sizeof err); + vk ? vk->name : "", NULL, 0, err, sizeof err); STATE_LOCK(); ep->inflight--; STATE_UNLOCK(); diff --git a/tests/review.sh b/tests/review.sh index f87326a..19f4aa2 100644 --- a/tests/review.sh +++ b/tests/review.sh @@ -11,6 +11,8 @@ RW=$((PORT+280)) RJ=$((PORT+281)) RT=$((PORT+282)) RJT=$((PORT+283)) +RB=$((PORT+284)) +RBJ=$((PORT+285)) STUB_STATE="$TMP/review.state" STUB_ANSWERS="first answer|second answer|third answer" \ tests/stub $RW "REVIEWER" > "$TMP/review_stub.log" 2>&1 & @@ -18,8 +20,12 @@ PIDS+=($!) STUB_STATE="$TMP/review_tools.state" STUB_ANSWERS="tool answer" STUB_TOOLS=1 \ tests/stub $RT "TOOLER" > "$TMP/review_tools_stub.log" 2>&1 & PIDS+=($!) +STUB_STATE="$TMP/review_bad.state" STUB_ANSWERS="unused" tests/stub $RB "BADREQUEST" --status 400 \ + > "$TMP/review_bad_stub.log" 2>&1 & +PIDS+=($!) wait_port $RW 10 || bad "the review stub did not come up" wait_port $RT 10 || bad "the tool-review stub did not come up" +wait_port $RB 10 || bad "the 400 stub did not come up" cat > "$TMP/review.conf" < "$TMP/review_bad.conf" < "$TMP/review_bad.log" 2>&1 & +PIDS+=($!) +wait_port "$RBJ" 10 || bad "the 400-review instance did not come up" +BADCODE=$(curl -s -D "$TMP/review_bad_h.txt" -o "$TMP/review_bad_b.json" -m 20 \ + -X POST -H 'Content-Type: application/json' \ + -d '{"model":"vm-bad","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \ + "http://127.0.0.1:$RBJ/v1/chat/completions" -w '%{http_code}') +[ "$BADCODE" = "400" ] && ok "a provider 400 reaches the review client as 400" \ + || bad "provider 400 became HTTP $BADCODE" +contains "$(cat "$TMP/review_bad_b.json")" "bad request from stub" \ + "the provider's 400 body is preserved" +BCALLS=$(tr -d '\n' < "$TMP/review_bad.state") +[ "$BCALLS" = "1" ] && ok "the 400 stopped before any review round" \ + || bad "the 400 path made '$BCALLS' provider calls" + +# Derselbe Fehler bei einem Streaming-Client: der Kopf ist schon 200, der +# Provider-Fehler muss als SSE-Event mit Code 400 ankommen. +rm -f "$TMP/review_bad.state" +BSSE=$(curl -s -N -D /dev/null -m 20 -X POST -H 'Content-Type: application/json' \ + -d '{"model":"vm-bad","stream":true,"messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \ + "http://127.0.0.1:$RBJ/v1/chat/completions") +contains "$BSSE" "400" "the streaming review error carries the 400" +BCALLS=$(tr -d '\n' < "$TMP/review_bad.state") +[ "$BCALLS" = "1" ] && ok "the streaming 400 also stopped before reviews" \ + || bad "the streaming 400 path made '$BCALLS' provider calls" + # Konfigurationsfehler müssen laut sein. printf 'endpoint e {\n base http://127.0.0.1:9995/v1\n}\nreq for "vm" review 5 pass e\n' \ > "$TMP/review_bad_count.conf" diff --git a/tests/run.sh b/tests/run.sh index 2458042..6f93a8c 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -71,7 +71,7 @@ log_has() { # answer requests with somebody else's config), so fail loudly and early. # 8157 gehört auf dieser Maschine dem Hermes-Gateway — der Port wird von keinem # Test benutzt, also darf er die Suite nicht aufhalten. -for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8415 8416 8417 8418 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 9443 9990 9991 9992 9993 9994 9995 9996; do +for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8415 8416 8417 8418 8419 8420 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 9443 9990 9991 9992 9993 9994 9995 9996; do if ss -tln 2>/dev/null | grep -q ":$port "; then echo "port $port is busy — a leftover junction/stub is still running:" ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /' diff --git a/tests/stub.c b/tests/stub.c index 09c7120..752a3fc 100644 --- a/tests/stub.c +++ b/tests/stub.c @@ -119,6 +119,7 @@ int main(int argc, char **argv) int fail = 0; double delay = 0; int fail_n = 0; + int forced_status = 0; int retry_after = 0; int retry_after_date = 0; int has_retry_date = 0; @@ -128,6 +129,7 @@ int main(int argc, char **argv) if (!strcmp(argv[i], "--fail")) fail = 1; else if (!strcmp(argv[i], "--delay") && i + 1 < argc) delay = atof(argv[++i]); else if (!strcmp(argv[i], "--fail-n") && i + 1 < argc) fail_n = atoi(argv[++i]); + else if (!strcmp(argv[i], "--status") && i + 1 < argc) forced_status = atoi(argv[++i]); else if (!strcmp(argv[i], "--deny-auth") && i + 1 < argc) deny_auth = argv[++i]; else if (!strcmp(argv[i], "--retry-after") && i + 1 < argc) retry_after = atoi(argv[++i]); else if (!strcmp(argv[i], "--retry-after-date") && i + 1 < argc) { @@ -216,7 +218,8 @@ int main(int argc, char **argv) serial = request_serial(); have_serial = 1; } - if (fail_n > 0 && counts && have_serial && serial <= fail_n) forced = 503; + if (forced_status > 0 && counts) forced = forced_status; + else if (fail_n > 0 && counts && have_serial && serial <= fail_n) forced = 503; else if (deny_auth[0] && strstr(authhdr, deny_auth)) forced = 429; /* STUB_ANSWERS="first|second|third": the n-th chat request answers with @@ -332,6 +335,7 @@ int main(int argc, char **argv) * darf. Mit --retry-after N sagt er eine Zahl, damit der Test prüfen kann, * dass junction genau so lange wartet. */ const char *b = (forced == 429) ? "{\"error\":\"rate limited\"}" + : (forced == 400) ? "{\"error\":\"bad request from stub\"}" : "{\"error\":\"not serving yet\"}"; char ra[64]; if (has_retry_date) { @@ -345,7 +349,8 @@ int main(int argc, char **argv) snprintf(body, sizeof body, "HTTP/1.1 %d %s\r\nContent-Type: application/json\r\nRetry-After: %s\r\n" "Content-Length: %zu\r\nConnection: close\r\n\r\n%s", - forced, (forced == 429) ? "Too Many Requests" : "Service Unavailable", + forced, (forced == 429) ? "Too Many Requests" + : (forced == 400) ? "Bad Request" : "Service Unavailable", ra, strlen(b), b); } else if (fail) { const char *b = "{\"error\":\"not serving\"}";