fix(router,handlers): strip trailing slash from rewritten URL + log upstream failures
Some checks failed
build-prerelease / Resolve version stamps (push) Successful in 32s
CI / Format (push) Successful in 33s
CI / Clippy (push) Successful in 2m20s
CI / Test (push) Successful in 4m41s
build-prerelease / Build neuron-blackwell (push) Successful in 3m34s
CI / Build cortex SRPM (push) Has been skipped
CI / Build neuron SRPM (push) Has been skipped
CI / Publish cortex to COPR (push) Has been skipped
CI / Publish neuron to COPR (push) Has been skipped
CI / Bump version in source (push) Has been skipped
build-prerelease / Build cortex binary (push) Successful in 4m31s
build-prerelease / Package cortex RPM (push) Successful in 1m21s
build-prerelease / Build neuron-ada (push) Has been cancelled
build-prerelease / Package helexa-neuron-ada RPM (push) Has been cancelled
build-prerelease / Package helexa-neuron-ampere RPM (push) Has been cancelled
build-prerelease / Package helexa-neuron-blackwell RPM (push) Has been cancelled
build-prerelease / Publish to rpm.lair.cafe (unstable) (push) Has been cancelled
build-prerelease / Build neuron-ampere (push) Has been cancelled

Two coupled bugs surfaced after 9b0ed0b:

1. url::Url::parse("http://host:port").to_string() normalises the
   empty path to "/", so rewrite_loopback_host was returning
   "http://beast:13131/". Downstream callers then did
   format!("{endpoint}/v1/chat/completions") and produced a
   double-slash path that neuron's axum router 404'd with an empty
   body. Strip the trailing slash in the rewriter so the endpoint is
   a clean base string for concatenation.

2. The anthropic_messages handler returned the upstream's empty body
   to the API caller as `"upstream error: "` with no journal log on
   the cortex side. Operators had no way to see what happened. Add
   warn-level tracing on both upstream failure paths (network error
   and non-2xx) with model, node, target URL, status, and a 512-char
   body snippet. The API response now carries just `"upstream
   returned <status>"` — the implementation detail lives in the log.

Updates the two existing rewrite tests for the no-trailing-slash
output.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-22 07:10:39 +03:00
parent 9b0ed0b57f
commit 0f00f72b47
2 changed files with 29 additions and 6 deletions

View File

@@ -138,9 +138,10 @@ async fn anthropic_messages(
}
} else {
// Non-streaming: proxy, buffer full response, translate back to Anthropic.
let target_url = format!("{}/v1/chat/completions", route.endpoint);
let upstream_resp = fleet
.http_client
.post(format!("{}/v1/chat/completions", route.endpoint))
.post(&target_url)
.body(openai_body)
.header("content-type", "application/json")
.send()
@@ -150,7 +151,14 @@ async fn anthropic_messages(
Ok(r) => r,
Err(e) => {
metrics::counter!("cortex_request_errors_total", &labels).increment(1);
return error_response(502, &format!("upstream request failed: {e}"));
tracing::warn!(
model = %model_id,
node = %route.node_name,
target = %target_url,
error = %e,
"anthropic proxy: upstream request failed (network)"
);
return error_response(502, "upstream request failed");
}
};
@@ -158,7 +166,16 @@ async fn anthropic_messages(
metrics::counter!("cortex_request_errors_total", &labels).increment(1);
let status = upstream_resp.status().as_u16();
let body = upstream_resp.text().await.unwrap_or_default();
return error_response(status, &format!("upstream error: {body}"));
let body_snippet = body.chars().take(512).collect::<String>();
tracing::warn!(
model = %model_id,
node = %route.node_name,
target = %target_url,
status,
body = %body_snippet,
"anthropic proxy: upstream returned non-2xx"
);
return error_response(status, &format!("upstream returned {status}"));
}
let body_bytes = match upstream_resp.bytes().await {

View File

@@ -335,7 +335,13 @@ fn rewrite_loopback_host(inference_url: &str, neuron_endpoint: &str) -> Option<S
let new_host = neuron.host_str()?;
let mut out = inf.clone();
out.set_host(Some(new_host)).ok()?;
Some(out.to_string())
// url::Url::to_string normalises an empty path to "/", which then
// breaks downstream callers that do format!("{endpoint}/v1/...")
// and produce a double slash. The proxy URL is treated as a base
// string that the caller appends paths to, so strip the trailing
// slash here.
let s = out.to_string();
Some(s.trim_end_matches('/').to_string())
}
#[cfg(test)]
@@ -350,14 +356,14 @@ mod tests {
);
assert_eq!(
out.as_deref(),
Some("http://beast.hanzalova.internal:13131/")
Some("http://beast.hanzalova.internal:13131")
);
}
#[test]
fn rewrites_loopback_with_distinct_inference_port() {
let out = rewrite_loopback_host("http://127.0.0.1:8080", "http://beast.lan:13131");
assert_eq!(out.as_deref(), Some("http://beast.lan:8080/"));
assert_eq!(out.as_deref(), Some("http://beast.lan:8080"));
}
#[test]