View Issue Details
| ID | Project | Category | View Status | Date Submitted | Last Update |
|---|---|---|---|---|---|
| 0012175 | Taler | taler-rust | public | 2026-11-18 14:41 | 2026-11-18 14:42 |
| Reporter | bot-cg [agent] | ||||
| Priority | normal | Severity | minor | ||
| Status | new | ||||
| Summary | 0012175: taler-api: nine patches that Beekeeper carries on its vendored copy (patch series attached) | ||||
| Description | Beekeeper (git.taler.net/beekeeper.git) uses taler-rust's common/taler-api, vendored as vendor/taler-api: taler-rust at 2c6788738c5f4784874e62c91e32dab4550c4626 plus the patches in vendor/patches/. Each patch is a commit on that revision, exported with git format-patch, and written to be taken upstream; contrib/vendor-taler-api rebuilds the vendored tree from them. The child issues describe the nine patches one by one, each with its own patch attached. They touch neighbouring code (most of them error.rs or api.rs), so apply them in order; the whole series is attached here as one mbox. They were written against 2c678873 and may need a rebase onto current master (406ed50). In Beekeeper, the tests of each change pass: taler-api's own tests (25 with the series) and Beekeeper's API tests that exercise them. Patches 1-4 date from September and October 2026, 5-9 from 18 November 2026. A tenth child reports a typo in taler-common's dbinit log, with a one-line patch; it is unrelated to the vendored series. | ||||
| Tags | No tags attached. | ||||
| Attached Files | taler-api-series-2c678873.mbox (56,070 bytes)
From 17508aadb59ff7b6922e29e42c502abf9396f1a2 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Tue, 29 Sep 2026 13:48:22 +0200
Subject: [PATCH 1/9] api: accept error codes from other GANA registries
ApiError only took Taler's 16-bit ErrorCode, so a component with its own registry (Beekeeper uses 100000 to 110000) could not report its errors through it.
---
common/taler-api/src/error.rs | 161 ++++++++++++++++++++++++++++++----
1 file changed, 143 insertions(+), 18 deletions(-)
diff --git a/common/taler-api/src/error.rs b/common/taler-api/src/error.rs
index 02f1fe6..a6e5897 100644
--- a/common/taler-api/src/error.rs
+++ b/common/taler-api/src/error.rs
@@ -22,17 +22,114 @@ use axum::{
http::{HeaderMap, HeaderValue, StatusCode, header::IntoHeaderName},
response::{IntoResponse, Response},
};
-use taler_common::{
- api::{ErrorDetail, params::ParamsErr},
- error_code::ErrorCode,
- types::payto::PaytoErr,
-};
+use serde::Serialize;
+use taler_common::{api::params::ParamsErr, error_code::ErrorCode, types::payto::PaytoErr};
pub type ApiResult<T> = Result<T, ApiError>;
+/// An error code with the HTTP status it is returned with. Codes come from
+/// the GNU Taler registry ([`ErrorCode`]) or from another GANA registry, such
+/// as a component's own, whose values are above the Taler range (0 to 9999)
+/// and so may not fit in 16 bits.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct Code {
+ value: u32,
+ status: u16,
+ source: Source,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum Source {
+ Taler(ErrorCode),
+ Other {
+ name: &'static str,
+ description: &'static str,
+ },
+}
+
+impl Code {
+ /// A code from another registry.
+ pub const fn new(
+ value: u32,
+ status: u16,
+ name: &'static str,
+ description: &'static str,
+ ) -> Self {
+ Self {
+ value,
+ status,
+ source: Source::Other { name, description },
+ }
+ }
+
+ pub fn value(&self) -> u32 {
+ self.value
+ }
+
+ pub fn status(&self) -> u16 {
+ self.status
+ }
+
+ pub fn name(&self) -> &str {
+ match &self.source {
+ Source::Taler(code) => code.as_ref(),
+ Source::Other { name, .. } => name,
+ }
+ }
+
+ pub fn description(&self) -> &'static str {
+ match self.source {
+ Source::Taler(code) => code.description(),
+ Source::Other { description, .. } => description,
+ }
+ }
+
+ /// The Taler error code, if it is one.
+ pub fn taler(&self) -> Option<ErrorCode> {
+ match self.source {
+ Source::Taler(code) => Some(code),
+ Source::Other { .. } => None,
+ }
+ }
+}
+
+impl From<ErrorCode> for Code {
+ fn from(code: ErrorCode) -> Self {
+ Self {
+ value: code as u32,
+ status: code.status_code(),
+ source: Source::Taler(code),
+ }
+ }
+}
+
+impl Display for Code {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str(self.name())
+ }
+}
+
+/// The body of an error response: `ErrorDetail`, with room for codes of more
+/// than 16 bits.
+#[derive(Serialize)]
+struct ErrorBody {
+ code: u32,
+ hint: Option<Box<str>>,
+ detail: Option<Box<str>>,
+ parameter: Option<Box<str>>,
+ path: Option<Box<str>>,
+ offset: Option<Box<str>>,
+ index: Option<Box<str>>,
+ object: Option<Box<str>>,
+ currency: Option<Box<str>>,
+ type_expected: Option<Box<str>>,
+ type_actual: Option<Box<str>>,
+ extra: Option<Box<serde_json::value::RawValue>>,
+}
+
#[derive(Debug, thiserror::Error)]
pub struct ApiError {
- code: ErrorCode,
+ code: Code,
hint: Option<Box<str>>,
log: Option<Box<str>>,
status: Option<StatusCode>,
@@ -51,9 +148,9 @@ impl Display for ApiError {
}
impl ApiError {
- pub fn new(code: ErrorCode) -> Self {
+ pub fn new(code: impl Into<Code>) -> Self {
Self {
- code,
+ code: code.into(),
hint: None,
log: None,
status: None,
@@ -112,7 +209,7 @@ impl From<sqlx::Error> for ApiError {
_ => (ErrorCode::GENERIC_DB_FETCH_FAILED, StatusCode::BAD_GATEWAY),
};
Self {
- code,
+ code: code.into(),
hint: None,
status: Some(status),
log: Some(format!("{value}").into_boxed_str()),
@@ -189,15 +286,14 @@ impl IntoResponse for ApiError {
path,
headers,
} = self;
- let status_code = status.unwrap_or_else(|| {
- StatusCode::from_u16(code.status_code()).expect("Invalid status code")
- });
+ let status_code = status
+ .unwrap_or_else(|| StatusCode::from_u16(code.status()).expect("Invalid status code"));
let log = log.or(hint.clone());
let mut resp = (
status_code,
- Json(ErrorDetail {
- code: code as u16,
+ Json(ErrorBody {
+ code: code.value(),
hint,
detail: None,
parameter: None,
@@ -226,19 +322,19 @@ impl IntoResponse for ApiError {
#[derive(Debug, Clone)]
pub struct LoggedError {
- pub code: ErrorCode,
+ pub code: Code,
pub info: Option<Box<str>>,
}
-pub fn failure_code(code: ErrorCode) -> ApiError {
+pub fn failure_code(code: impl Into<Code>) -> ApiError {
ApiError::new(code)
}
-pub fn failure(code: ErrorCode, hint: impl Display) -> ApiError {
+pub fn failure(code: impl Into<Code>, hint: impl Display) -> ApiError {
ApiError::new(code).with_hint(hint)
}
-pub fn failure_status(code: ErrorCode, hint: impl Display, status: StatusCode) -> ApiError {
+pub fn failure_status(code: impl Into<Code>, hint: impl Display, status: StatusCode) -> ApiError {
ApiError::new(code).with_hint(hint).with_status(status)
}
@@ -257,3 +353,32 @@ pub fn forbidden(hint: impl Display) -> ApiError {
pub fn bad_request(hint: impl Display) -> ApiError {
ApiError::new(ErrorCode::GENERIC_JSON_INVALID).with_hint(hint)
}
+
+#[cfg(test)]
+mod tests {
+ use http_body_util::BodyExt as _;
+
+ use super::*;
+
+ async fn body(err: ApiError) -> (StatusCode, serde_json::Value) {
+ let resp = err.into_response();
+ let status = resp.status();
+ let bytes = resp.into_body().collect().await.unwrap().to_bytes();
+ (status, serde_json::from_slice(&bytes).unwrap())
+ }
+
+ #[tokio::test]
+ async fn codes_from_other_registries() {
+ const OWN: Code = Code::new(100_000, 429, "OWN_RATE_LIMITED", "Too many requests.");
+ let (status, json) = body(failure(OWN, "slow down")).await;
+ assert_eq!(status, StatusCode::TOO_MANY_REQUESTS);
+ assert_eq!(json["code"], 100_000);
+ assert_eq!(json["hint"], "slow down");
+
+ let (status, json) = body(failure_code(ErrorCode::GENERIC_JSON_INVALID)).await;
+ assert_eq!(status, StatusCode::BAD_REQUEST);
+ assert_eq!(json["code"], ErrorCode::GENERIC_JSON_INVALID as u16);
+ // Same shape as ErrorDetail: absent fields are null.
+ assert!(json["detail"].is_null() && json.get("detail").is_some());
+ }
+}
--
2.47.3
From f0675b35328085ede7b3a31b13e5bf5f1f5b0c67 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Tue, 29 Sep 2026 13:48:51 +0200
Subject: [PATCH 2/9] api: allow extra details in error responses
ErrorDetail has an extra field, but ApiError always sent it as null, so per-field validation problems had no place in the response.
---
common/taler-api/src/error.rs | 26 +++++++++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/common/taler-api/src/error.rs b/common/taler-api/src/error.rs
index a6e5897..f6494ca 100644
--- a/common/taler-api/src/error.rs
+++ b/common/taler-api/src/error.rs
@@ -135,6 +135,7 @@ pub struct ApiError {
status: Option<StatusCode>,
path: Option<Box<str>>,
headers: Option<Box<HeaderMap>>,
+ extra: Option<Box<serde_json::value::RawValue>>,
}
impl Display for ApiError {
@@ -156,6 +157,7 @@ impl ApiError {
status: None,
path: None,
headers: None,
+ extra: None,
}
}
@@ -187,6 +189,18 @@ impl ApiError {
}
}
+ /// Set the `extra` field of the error response, for details that the
+ /// other fields cannot express (e.g. a list of validation problems).
+ pub fn with_extra(self, extra: &impl Serialize) -> Self {
+ match serde_json::value::to_raw_value(extra) {
+ Ok(raw) => Self {
+ extra: Some(raw),
+ ..self
+ },
+ Err(e) => self.with_log(format!("unserializable error details: {e}")),
+ }
+ }
+
pub fn with_header(mut self, key: impl IntoHeaderName, value: HeaderValue) -> Self {
let headers = self.headers.get_or_insert_default();
headers.append(key, value);
@@ -215,6 +229,7 @@ impl From<sqlx::Error> for ApiError {
log: Some(format!("{value}").into_boxed_str()),
path: None,
headers: None,
+ extra: None,
}
}
}
@@ -285,6 +300,7 @@ impl IntoResponse for ApiError {
status,
path,
headers,
+ extra,
} = self;
let status_code = status
.unwrap_or_else(|| StatusCode::from_u16(code.status()).expect("Invalid status code"));
@@ -304,7 +320,7 @@ impl IntoResponse for ApiError {
currency: None,
type_expected: None,
type_actual: None,
- extra: None,
+ extra,
}),
)
.into_response();
@@ -381,4 +397,12 @@ mod tests {
// Same shape as ErrorDetail: absent fields are null.
assert!(json["detail"].is_null() && json.get("detail").is_some());
}
+
+ #[tokio::test]
+ async fn extra_details() {
+ let err = failure_code(ErrorCode::GENERIC_JSON_INVALID)
+ .with_extra(&serde_json::json!({"errors": {"name": ["required"]}}));
+ let (_, json) = body(err).await;
+ assert_eq!(json["extra"]["errors"]["name"][0], "required");
+ }
}
--
2.47.3
From 480b6104f9ab3bf4ff5a1fbe2f27e69c5c41c7a5 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Tue, 29 Sep 2026 13:49:29 +0200
Subject: [PATCH 3/9] api: make the request body limit configurable per router
The fixed 4 KiB suits the wire gateway, but a service that takes larger JSON documents (form definitions of up to 1 MiB) could not use the extractors; the decompression buffer also moves from the stack to the heap.
---
common/taler-api/src/extract.rs | 102 +++++++++++++++++++++++++++++---
1 file changed, 94 insertions(+), 8 deletions(-)
diff --git a/common/taler-api/src/extract.rs b/common/taler-api/src/extract.rs
index 6811672..5a6f22d 100644
--- a/common/taler-api/src/extract.rs
+++ b/common/taler-api/src/extract.rs
@@ -35,7 +35,28 @@ use crate::{
error::{ApiError, ApiResult, failure, failure_status},
};
+/// The largest request body, before and after decompression, that [`Req`] and
+/// [`OptReq`] accept; [`MAX_BODY_LENGTH`] unless set for a router with
+/// `.route_layer(axum::Extension(BodyLimit(n)))`.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct BodyLimit(pub usize);
+
+fn body_limit(parts: &Parts) -> usize {
+ parts
+ .extensions
+ .get::<BodyLimit>()
+ .map_or(MAX_BODY_LENGTH, |l| l.0)
+}
+
pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiResult<Bytes> {
+ decompressed_strict_body_limited(headers, body, MAX_BODY_LENGTH).await
+}
+
+pub async fn decompressed_strict_body_limited(
+ headers: &HeaderMap,
+ body: Body,
+ limit: usize,
+) -> ApiResult<Bytes> {
// Check content type
match headers.get(header::CONTENT_TYPE) {
Some(header) => {
@@ -61,11 +82,11 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
.get(header::CONTENT_LENGTH)
.and_then(|it| it.to_str().ok())
.and_then(|it| it.parse::<usize>().ok())
- && length > MAX_BODY_LENGTH
+ && length > limit
{
return Err(failure(
ErrorCode::GENERIC_UPLOAD_EXCEEDS_LIMIT,
- format!("Body is suspiciously big > {MAX_BODY_LENGTH}B"),
+ format!("Body is suspiciously big > {limit}B"),
));
}
@@ -88,14 +109,14 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
};
// Buffer body
- let body = http_body_util::Limited::new(body, MAX_BODY_LENGTH);
+ let body = http_body_util::Limited::new(body, limit);
let bytes = match body.collect().await {
Ok(chunks) => chunks.to_bytes(),
Err(it) => match it.downcast::<http_body_util::LengthLimitError>() {
Ok(_) => {
return Err(failure(
ErrorCode::GENERIC_UPLOAD_EXCEEDS_LIMIT,
- format!("Body is suspiciously big > {MAX_BODY_LENGTH}B"),
+ format!("Body is suspiciously big > {limit}B"),
));
}
Err(err) => {
@@ -108,7 +129,7 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
};
let bytes = if compressed {
- let mut buf = [0; MAX_BODY_LENGTH];
+ let mut buf = vec![0; limit];
let (decompressed, code) =
zlib_rs::decompress_slice(&mut buf, &bytes, InflateConfig::default());
match code {
@@ -116,7 +137,7 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
ReturnCode::BufError => {
return Err(failure(
ErrorCode::GENERIC_UPLOAD_EXCEEDS_LIMIT,
- format!("Decompressed body is suspiciously big > {MAX_BODY_LENGTH}B"),
+ format!("Decompressed body is suspiciously big > {limit}B"),
));
}
_ => {
@@ -146,7 +167,8 @@ where
async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
let (parts, body) = req.into_parts();
- let bytes = decompressed_strict_body(&parts.headers, body).await?;
+ let limit = body_limit(&parts);
+ let bytes = decompressed_strict_body_limited(&parts.headers, body, limit).await?;
Self::try_from(&bytes)
}
}
@@ -177,7 +199,8 @@ where
if !parts.headers.contains_key(CONTENT_TYPE) && body.size_hint().exact() == Some(0) {
Ok(Self(None))
} else {
- let bytes = decompressed_strict_body(&parts.headers, body).await?;
+ let limit = body_limit(&parts);
+ let bytes = decompressed_strict_body_limited(&parts.headers, body, limit).await?;
let req = Req::try_from(&bytes)?;
Ok(Self(Some(req.0)))
}
@@ -211,3 +234,66 @@ impl<T: serde::de::DeserializeOwned, S: Sync + Send> FromRequestParts<S> for Que
Ok(Query(params))
}
}
+
+#[cfg(test)]
+mod tests {
+ use axum::http::Request;
+
+ use super::*;
+
+ fn request(len: usize, limit: Option<usize>) -> Request<Body> {
+ let json = format!("\"{}\"", "x".repeat(len - 2));
+ let mut req = Request::builder()
+ .header(CONTENT_TYPE, "application/json")
+ .body(Body::from(json))
+ .unwrap();
+ if let Some(limit) = limit {
+ req.extensions_mut().insert(BodyLimit(limit));
+ }
+ req
+ }
+
+ #[tokio::test]
+ async fn body_limit() {
+ let big = MAX_BODY_LENGTH + 1;
+ assert!(
+ Req::<String>::from_request(request(big, None), &())
+ .await
+ .is_err()
+ );
+ let Req(s) = Req::<String>::from_request(request(big, Some(big)), &())
+ .await
+ .unwrap();
+ assert_eq!(s.len(), big - 2);
+ assert!(
+ Req::<String>::from_request(request(big + 1, Some(big)), &())
+ .await
+ .is_err()
+ );
+
+ // The limit also applies after decompression.
+ let json = format!("\"{}\"", "x".repeat(big));
+ let compressed = {
+ let mut out = vec![0; big * 2];
+ let (c, code) = zlib_rs::compress_slice(
+ &mut out,
+ json.as_bytes(),
+ zlib_rs::DeflateConfig::default(),
+ );
+ assert_eq!(code, ReturnCode::Ok);
+ c.to_vec()
+ };
+ assert!(compressed.len() < MAX_BODY_LENGTH);
+ let req = |limit: usize| {
+ let mut req = Request::builder()
+ .header(CONTENT_TYPE, "application/json")
+ .header(header::CONTENT_ENCODING, "deflate")
+ .body(Body::from(compressed.clone()))
+ .unwrap();
+ req.extensions_mut().insert(BodyLimit(limit));
+ req
+ };
+ assert!(Req::<String>::from_request(req(big), &()).await.is_err());
+ assert!(Req::<String>::from_request(req(big + 2), &()).await.is_ok());
+ }
+}
--
2.47.3
From 48332fa76f4411f1500cca0593a660b74d097fb9 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 14 Oct 2026 16:15:14 +0200
Subject: [PATCH 4/9] api: remember whether a database error is worth retrying
Every connection runs with SERIALIZABLE isolation, so a handler that reads,
decides and then writes can lose the race and has to repeat the whole thing;
turning the sqlx error into an ApiError threw away the one bit that says so.
---
common/taler-api/src/error.rs | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/common/taler-api/src/error.rs b/common/taler-api/src/error.rs
index f6494ca..50bed41 100644
--- a/common/taler-api/src/error.rs
+++ b/common/taler-api/src/error.rs
@@ -136,6 +136,8 @@ pub struct ApiError {
path: Option<Box<str>>,
headers: Option<Box<HeaderMap>>,
extra: Option<Box<serde_json::value::RawValue>>,
+ /// Set when this was made from a database error that a retry can clear.
+ retryable: bool,
}
impl Display for ApiError {
@@ -158,9 +160,23 @@ impl ApiError {
path: None,
headers: None,
extra: None,
+ retryable: false,
}
}
+ /// Whether the database asked for the work to be done again: a
+ /// serialization failure or a deadlock (40001, 40P01) under the
+ /// `SERIALIZABLE` isolation every connection runs with.
+ ///
+ /// A handler that reads, decides and then writes cannot retry such a
+ /// failure where it happens -- the whole read-modify-write has to be
+ /// repeated on a snapshot that includes the other writer -- so the one
+ /// bit that says whether repeating it is worth anything has to survive
+ /// the trip up to it.
+ pub fn is_retryable(&self) -> bool {
+ self.retryable
+ }
+
pub fn with_hint(self, hint: impl Display) -> Self {
Self {
hint: Some(hint.to_string().into_boxed_str()),
@@ -210,6 +226,8 @@ impl ApiError {
impl From<sqlx::Error> for ApiError {
fn from(value: sqlx::Error) -> Self {
+ use crate::db::PgError;
+ let retryable = value.is_retryable_err();
let (code, status) = match value {
sqlx::Error::Configuration(_) => {
(ErrorCode::GENERIC_DB_SETUP_FAILED, StatusCode::BAD_GATEWAY)
@@ -230,6 +248,7 @@ impl From<sqlx::Error> for ApiError {
path: None,
headers: None,
extra: None,
+ retryable,
}
}
}
@@ -301,6 +320,8 @@ impl IntoResponse for ApiError {
path,
headers,
extra,
+ // Ours, not the client's business.
+ retryable: _,
} = self;
let status_code = status
.unwrap_or_else(|| StatusCode::from_u16(code.status()).expect("Invalid status code"));
--
2.47.3
From 1df49fce6c793dd33cfddedeb61256288974252a Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 10:44:34 +0100
Subject: [PATCH 5/9] api: log the route, not the path and query
The request logger wrote the whole path and query at every level, so bearer
secrets in a path (an OAuth2 nonce) and personal data in a query (a search
for an e-mail address) ended up in the journal.
Assisted by LLM
---
common/taler-api/Cargo.toml | 2 +
common/taler-api/src/api.rs | 130 +++++++++++++++++++++++++++++++++---
2 files changed, 124 insertions(+), 8 deletions(-)
diff --git a/common/taler-api/Cargo.toml b/common/taler-api/Cargo.toml
index 10538a0..2ea77e4 100644
--- a/common/taler-api/Cargo.toml
+++ b/common/taler-api/Cargo.toml
@@ -39,6 +39,8 @@ prometheus-client.workspace = true
taler-test-utils.workspace = true
criterion.workspace = true
rand.workspace = true
+tower = { version = "0.5", features = ["util"] }
+tracing-subscriber.workspace = true
[[bench]]
name = "subject"
diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs
index 1f59e04..38617aa 100644
--- a/common/taler-api/src/api.rs
+++ b/common/taler-api/src/api.rs
@@ -23,7 +23,7 @@ use std::{
};
use axum::{
- extract::{Request, State},
+ extract::{MatchedPath, Request, State},
middleware::{self, Next},
response::Response,
};
@@ -234,6 +234,9 @@ macro_rules! dyn_event {
};
}
+/// What the logger writes in place of the route when no route matched.
+const UNMATCHED_ROUTE: &str = "-";
+
/** Taler API logger */
async fn logger_middleware(request: Request, next: Next) -> Response {
let now = Instant::now();
@@ -249,14 +252,17 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
unsafe { CompactString::from_utf8_unchecked(ansi) }
};
let method = request.method().clone();
- let path_and_query = request.uri().path_and_query().cloned();
- let path_and_query = path_and_query
+ // The route's template, never the path and query themselves: those can
+ // hold bearer secrets (a nonce in the path, a token in the query) and
+ // personal data (a search term), which do not belong in a log.
+ let route = request.extensions().get::<MatchedPath>().cloned();
+ let route = route
.as_ref()
- .map(|it| it.as_str())
- .unwrap_or_default();
+ .map(MatchedPath::as_str)
+ .unwrap_or(UNMATCHED_ROUTE);
LOG_TASK_ID
.scope(request_id, async {
- debug!(target: "api", "{method} {path_and_query}");
+ debug!(target: "api", "{method} {route}");
let response = next.run(request).await;
let elapsed = now.elapsed();
let status = response.status();
@@ -269,7 +275,7 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
if let Some(log) = response.extensions().get::<LoggedError>() {
let LoggedError { code, info } = log;
dyn_event!(level, target: "api",
- "{} {method} {path_and_query} {}ms: {code}{}",
+ "{} {method} {route} {}ms: {code}{}",
response.status(),
elapsed.as_millis(),
std::fmt::from_fn(|f|{
@@ -281,7 +287,7 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
);
} else {
dyn_event!(level, target: "api",
- "{} {method} {path_and_query} {}ms",
+ "{} {method} {route} {}ms",
response.status(),
elapsed.as_millis()
);
@@ -290,3 +296,111 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
})
.await
}
+
+#[cfg(test)]
+mod tests {
+ use std::sync::{Arc, Mutex};
+
+ use axum::{
+ body::Body,
+ http::{Method, Request, StatusCode},
+ routing::get,
+ };
+ use tower::ServiceExt as _;
+
+ use super::*;
+
+ /// Collects what the subscriber writes.
+ #[derive(Clone, Default)]
+ struct Captured(Arc<Mutex<Vec<u8>>>);
+
+ impl Captured {
+ fn text(&self) -> String {
+ String::from_utf8(self.0.lock().unwrap().clone()).unwrap()
+ }
+ }
+
+ impl std::io::Write for Captured {
+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
+ self.0.lock().unwrap().extend_from_slice(buf);
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+ }
+
+ impl tracing_subscriber::fmt::MakeWriter<'_> for Captured {
+ type Writer = Self;
+
+ fn make_writer(&self) -> Self {
+ self.clone()
+ }
+ }
+
+ /// Sends this thread's log messages, of every level, to a buffer. A test
+ /// that sends requests installs one before the first: tracing caches
+ /// whether a log statement is of interest, and a statement first reached
+ /// on a thread without a subscriber may stay silent for the others.
+ fn capture() -> (Captured, tracing::subscriber::DefaultGuard) {
+ let captured = Captured::default();
+ let subscriber = tracing_subscriber::fmt()
+ .with_max_level(Level::TRACE)
+ .with_ansi(false)
+ .with_writer(captured.clone())
+ .finish();
+ // The test runs on one thread, so the subscriber sees every request.
+ let guard = tracing::subscriber::set_default(subscriber);
+ tracing::callsite::rebuild_interest_cache();
+ (captured, guard)
+ }
+
+ async fn send(router: &Router, method: Method, uri: &str) -> StatusCode {
+ let request = Request::builder()
+ .method(method)
+ .uri(uri)
+ .body(Body::empty())
+ .unwrap();
+ router.clone().oneshot(request).await.unwrap().status()
+ }
+
+ #[tokio::test]
+ async fn logger_writes_routes_not_paths() {
+ let (captured, _guard) = capture();
+ let router = Router::new()
+ .route("/authorize/{nonce}", get(async || "ok"))
+ .finalize();
+
+ let status = send(
+ &router,
+ Method::GET,
+ "/authorize/nonce-secret?q=bert@example.org",
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK);
+ let status = send(&router, Method::POST, "/authorize/nonce-secret").await;
+ assert_eq!(status, StatusCode::METHOD_NOT_ALLOWED);
+ let status = send(
+ &router,
+ Method::GET,
+ "/unknown/path-secret?q=bert@example.org",
+ )
+ .await;
+ assert_eq!(status, StatusCode::NOT_FOUND);
+
+ let log = captured.text();
+ for secret in ["secret", "bert", "?"] {
+ assert!(!log.contains(secret), "{secret} in the log:\n{log}");
+ }
+ for line in [
+ "GET /authorize/{nonce}",
+ "200 OK GET /authorize/{nonce} ",
+ "405 Method Not Allowed POST /authorize/{nonce} ",
+ "GET -",
+ "404 Not Found GET - ",
+ ] {
+ assert!(log.contains(line), "no {line} in the log:\n{log}");
+ }
+ }
+}
--
2.47.3
From 86898857d6f6089c365ec7a5c085784769df1c69 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 10:51:22 +0100
Subject: [PATCH 6/9] api: let routes answer OPTIONS requests that are not CORS
preflights
tower-http's CorsLayer answered every OPTIONS request as a preflight, so a
route's own OPTIONS handler never ran; a tus server, for one, could not tell
its clients the largest upload it takes. The CORS middleware now answers
only real preflights, those with Access-Control-Request-Method, and passes
every other OPTIONS request to its route.
A preflight is allowed the request headers that it names in
Access-Control-Request-Headers. The wildcard "*" that CorsLayer sent does
not cover Authorization (Fetch standard), so browsers refused every request
with a bearer token from another origin.
Assisted by LLM
---
common/taler-api/Cargo.toml | 1 -
common/taler-api/src/api.rs | 151 ++++++++++++++++++++++++++++++++----
2 files changed, 138 insertions(+), 14 deletions(-)
diff --git a/common/taler-api/Cargo.toml b/common/taler-api/Cargo.toml
index 2ea77e4..6bed8d6 100644
--- a/common/taler-api/Cargo.toml
+++ b/common/taler-api/Cargo.toml
@@ -16,7 +16,6 @@ listenfd = "1.0.0"
dashmap = "6.1"
http-body-util.workspace = true
zlib-rs = "0.6.3"
-tower-http = { version = "0.7", features = ["cors"] }
tokio = { workspace = true, features = ["signal"] }
rand.workspace = true
serde.workspace = true
diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs
index 38617aa..789841b 100644
--- a/common/taler-api/src/api.rs
+++ b/common/taler-api/src/api.rs
@@ -24,6 +24,7 @@ use std::{
use axum::{
extract::{MatchedPath, Request, State},
+ http::{HeaderValue, Method, header},
middleware::{self, Next},
response::Response,
};
@@ -36,7 +37,6 @@ use taler_common::{
types::amount::{Amount, Currency},
};
use tokio::signal;
-use tower_http::cors::{Any, CorsLayer};
use tracing::{Level, debug, info};
use wire::WireGateway;
@@ -123,12 +123,7 @@ impl TalerRouter for Router {
fn finalize(self) -> Router {
self.method_not_allowed_fallback(async || failure_code(ErrorCode::GENERIC_METHOD_INVALID))
.fallback(async || failure_code(ErrorCode::GENERIC_ENDPOINT_UNKNOWN))
- .layer(
- CorsLayer::new()
- .allow_origin(Any)
- .allow_methods(Any)
- .allow_headers(Any),
- )
+ .layer(middleware::from_fn(cors_middleware))
.layer(middleware::from_fn(logger_middleware))
}
@@ -165,6 +160,57 @@ impl TalerRouter for Router {
}
}
+/// Allow cross-origin requests from any origin, with any method and headers.
+///
+/// Only a CORS preflight, an OPTIONS request with Access-Control-Request-Method
+/// (Fetch standard, "CORS-preflight request"), is answered here. Any other
+/// OPTIONS request goes to its route like every other method, so that a route
+/// can answer OPTIONS itself (tus, for example, announces its limits there);
+/// tower-http's CorsLayer answered every OPTIONS request as a preflight.
+///
+/// A preflight is allowed the request headers that it names in
+/// Access-Control-Request-Headers, as CorsLayer's `mirror_request` does. The
+/// wildcard `*` does not cover `Authorization` (Fetch standard, "CORS
+/// non-wildcard request-header name"), so with `*` alone a browser refuses
+/// every request with a bearer token from another origin. A preflight that
+/// names no headers gets `*`.
+async fn cors_middleware(request: Request, next: Next) -> Response {
+ const ANY: HeaderValue = HeaderValue::from_static("*");
+ let preflight = request.method() == Method::OPTIONS
+ && request
+ .headers()
+ .contains_key(header::ACCESS_CONTROL_REQUEST_METHOD);
+ let mut response = if preflight {
+ let requested: Vec<HeaderValue> = request
+ .headers()
+ .get_all(header::ACCESS_CONTROL_REQUEST_HEADERS)
+ .iter()
+ .cloned()
+ .collect();
+ let mut response = Response::default();
+ let headers = response.headers_mut();
+ headers.insert(header::ACCESS_CONTROL_ALLOW_METHODS, ANY);
+ if requested.is_empty() {
+ headers.insert(header::ACCESS_CONTROL_ALLOW_HEADERS, ANY);
+ }
+ for value in requested {
+ headers.append(header::ACCESS_CONTROL_ALLOW_HEADERS, value);
+ }
+ response
+ } else {
+ next.run(request).await
+ };
+ let headers = response.headers_mut();
+ headers.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, ANY);
+ headers.append(
+ header::VARY,
+ HeaderValue::from_static(
+ "origin, access-control-request-method, access-control-request-headers",
+ ),
+ );
+ response
+}
+
struct LifetimeMiddlewareState {
lifetime: AtomicU32,
notify: Arc<tokio::sync::Notify>,
@@ -357,12 +403,91 @@ mod tests {
}
async fn send(router: &Router, method: Method, uri: &str) -> StatusCode {
- let request = Request::builder()
- .method(method)
- .uri(uri)
- .body(Body::empty())
- .unwrap();
- router.clone().oneshot(request).await.unwrap().status()
+ request(router, method, uri, &[]).await.status()
+ }
+
+ async fn request(
+ router: &Router,
+ method: Method,
+ uri: &str,
+ headers: &[(&str, &str)],
+ ) -> Response {
+ let mut request = Request::builder().method(method).uri(uri);
+ for (k, v) in headers {
+ request = request.header(*k, *v);
+ }
+ let request = request.body(Body::empty()).unwrap();
+ router.clone().oneshot(request).await.unwrap()
+ }
+
+ fn header<'a>(response: &'a Response, name: &str) -> Option<&'a str> {
+ response.headers().get(name).map(|v| v.to_str().unwrap())
+ }
+
+ #[tokio::test]
+ async fn cors_leaves_plain_options_to_the_route() {
+ let (_captured, _guard) = capture();
+ let router = Router::new()
+ .route(
+ "/upload",
+ get(async || "ok")
+ .options(async || (StatusCode::NO_CONTENT, [("x-limit", "1024")])),
+ )
+ .route("/config", get(async || "ok"))
+ .finalize();
+ let origin = ("origin", "https://example.com");
+ let preflight = [origin, ("access-control-request-method", "POST")];
+
+ // A preflight is answered by the CORS layer, not by the route.
+ for uri in ["/upload", "/config", "/unknown"] {
+ let r = request(&router, Method::OPTIONS, uri, &preflight).await;
+ assert_eq!(r.status(), StatusCode::OK, "{uri}");
+ assert_eq!(header(&r, "access-control-allow-origin"), Some("*"));
+ assert_eq!(header(&r, "access-control-allow-methods"), Some("*"));
+ assert_eq!(header(&r, "access-control-allow-headers"), Some("*"));
+ assert_eq!(header(&r, "x-limit"), None);
+ }
+
+ // Any other OPTIONS request reaches the route, with or without Origin.
+ for headers in [&[][..], &[origin][..]] {
+ let r = request(&router, Method::OPTIONS, "/upload", headers).await;
+ assert_eq!(r.status(), StatusCode::NO_CONTENT);
+ assert_eq!(header(&r, "x-limit"), Some("1024"));
+ assert_eq!(header(&r, "access-control-allow-origin"), Some("*"));
+ assert_eq!(header(&r, "access-control-allow-methods"), None);
+ }
+ let r = request(&router, Method::OPTIONS, "/config", &[origin]).await;
+ assert_eq!(r.status(), StatusCode::METHOD_NOT_ALLOWED);
+ assert_eq!(header(&r, "access-control-allow-origin"), Some("*"));
+
+ // A preflight that names request headers is allowed exactly those:
+ // `*` would not cover Authorization.
+ let asking = [
+ origin,
+ ("access-control-request-method", "PATCH"),
+ (
+ "access-control-request-headers",
+ "authorization,content-type,tus-resumable,upload-offset",
+ ),
+ ];
+ let r = request(&router, Method::OPTIONS, "/upload", &asking).await;
+ assert_eq!(r.status(), StatusCode::OK);
+ assert_eq!(
+ header(&r, "access-control-allow-headers"),
+ Some("authorization,content-type,tus-resumable,upload-offset")
+ );
+ assert_eq!(header(&r, "access-control-allow-methods"), Some("*"));
+ assert_eq!(header(&r, "access-control-allow-origin"), Some("*"));
+ assert!(header(&r, "vary").is_some_and(|v| v.contains("access-control-request-headers")));
+
+ // Ordinary requests get the CORS headers too.
+ let r = request(&router, Method::GET, "/config", &[origin]).await;
+ assert_eq!(r.status(), StatusCode::OK);
+ assert_eq!(header(&r, "access-control-allow-origin"), Some("*"));
+ assert!(header(&r, "vary").is_some_and(|v| v.contains("origin")));
+ let r = request(&router, Method::GET, "/unknown", &[origin]).await;
+ assert_eq!(r.status(), StatusCode::NOT_FOUND);
+ assert_eq!(header(&r, "access-control-allow-origin"), Some("*"));
}
#[tokio::test]
--
2.47.3
From 8dc2579ca36fa38ca3004c85e0a3b8f45e1d0ec9 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 10:43:58 +0100
Subject: [PATCH 7/9] api: answer a body that cannot be read with 400
GENERIC_UNEXPECTED_REQUEST_ERROR has no HTTP status in the registry, so
building the response for a failed body read panicked.
---
common/taler-api/src/extract.rs | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/common/taler-api/src/extract.rs b/common/taler-api/src/extract.rs
index 5a6f22d..3bc2676 100644
--- a/common/taler-api/src/extract.rs
+++ b/common/taler-api/src/extract.rs
@@ -120,9 +120,12 @@ pub async fn decompressed_strict_body_limited(
));
}
Err(err) => {
- return Err(failure(
+ // The registry gives this code no HTTP status, and building
+ // the response would panic.
+ return Err(failure_status(
ErrorCode::GENERIC_UNEXPECTED_REQUEST_ERROR,
format!("Failed to read body: {err}"),
+ StatusCode::BAD_REQUEST,
));
}
},
--
2.47.3
From 94fc2be776cd5f02f1d4fdaa6b814dffa9cbbf4b Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 14:55:05 +0100
Subject: [PATCH 8/9] api: keep the values of a refused body or query out of
the log
When a JSON body or a query string does not deserialize, the error's hint
is serde's message, which quotes the value that failed ("invalid type:
string "hunter2", expected u32"). The request logger wrote that hint at
WARN, so passwords and personal data sent with the wrong type ended up in
the log. The logged line now names the error code, the path and the kind
of error, such as "pin: invalid type"; the client still gets the whole
message as the hint. json_error_kind() gives the kind to callers that
parse JSON themselves.
Assisted by LLM
---
common/taler-api/src/api.rs | 66 +++++++++++++++++++++++++++++++++++
common/taler-api/src/error.rs | 58 ++++++++++++++++++++++++------
2 files changed, 113 insertions(+), 11 deletions(-)
diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs
index 789841b..e7ba351 100644
--- a/common/taler-api/src/api.rs
+++ b/common/taler-api/src/api.rs
@@ -528,4 +528,70 @@ mod tests {
assert!(log.contains(line), "no {line} in the log:\n{log}");
}
}
+
+ /// The log line of a refused body or query names the code, the path and
+ /// the kind of error, but not the value, which only the client sees.
+ #[tokio::test]
+ async fn logger_writes_no_values_of_a_bad_request() {
+ #[derive(serde::Deserialize)]
+ struct Login {
+ #[allow(dead_code)]
+ pin: u32,
+ }
+ #[derive(serde::Deserialize)]
+ #[serde(rename_all = "lowercase")]
+ enum Kind {
+ Plain,
+ }
+ #[derive(serde::Deserialize)]
+ struct Search {
+ #[allow(dead_code)]
+ kind: Kind,
+ }
+ let (captured, _guard) = capture();
+ let router = Router::new()
+ .route(
+ "/login",
+ axum::routing::post(
+ async |crate::extract::Req(_): crate::extract::Req<Login>| "ok",
+ ),
+ )
+ .route(
+ "/search",
+ get(async |crate::extract::Query(_): crate::extract::Query<Search>| "ok"),
+ )
+ .finalize();
+
+ let login = Request::builder()
+ .method(Method::POST)
+ .uri("/login")
+ .header("content-type", "application/json")
+ .body(Body::from(r#"{"pin": "hunter2-secret"}"#))
+ .unwrap();
+ let r = router.clone().oneshot(login).await.unwrap();
+ assert_eq!(r.status(), StatusCode::BAD_REQUEST);
+ let body = axum::body::to_bytes(r.into_body(), 4096).await.unwrap();
+ let body = String::from_utf8(body.to_vec()).unwrap();
+ assert!(body.contains("hunter2-secret"), "the client's hint: {body}");
+ let r = request(&router, Method::GET, "/search?kind=search-secret", &[]).await;
+ assert_eq!(r.status(), StatusCode::BAD_REQUEST);
+
+ // The request body is logged at TRACE only.
+ let log = captured.text();
+ let warnings: Vec<&str> = log.lines().filter(|l| l.contains("WARN")).collect();
+ assert_eq!(warnings.len(), 2, "{log}");
+ for line in &warnings {
+ assert!(!line.contains("secret"), "a value in the log:\n{line}");
+ }
+ assert!(
+ warnings[0].contains("POST /login") && warnings[0].contains("pin: invalid type"),
+ "{}",
+ warnings[0]
+ );
+ assert!(
+ warnings[1].contains("GET /search") && warnings[1].contains("kind: unknown variant"),
+ "{}",
+ warnings[1]
+ );
+ }
}
diff --git a/common/taler-api/src/error.rs b/common/taler-api/src/error.rs
index 50bed41..191e476 100644
--- a/common/taler-api/src/error.rs
+++ b/common/taler-api/src/error.rs
@@ -265,26 +265,62 @@ impl From<ParamsErr> for ApiError {
}
}
+/// The kind of error at the start of serde's own messages, such as
+/// `invalid type: string "hunter2", expected u32`.
+fn serde_error_kind(message: &str) -> Option<&'static str> {
+ const KINDS: [&str; 7] = [
+ "invalid type",
+ "invalid value",
+ "invalid length",
+ "unknown variant",
+ "unknown field",
+ "missing field",
+ "duplicate field",
+ ];
+ KINDS.into_iter().find(|kind| message.starts_with(kind))
+}
+
+/// The kind of error that serde_json found, for the log. The message itself
+/// goes to the client only: it may quote the value that failed, and request
+/// bodies hold passwords and personal data.
+pub fn json_error_kind(e: &serde_json::Error) -> &'static str {
+ use serde_json::error::Category;
+ match e.classify() {
+ Category::Io => "unreadable input",
+ Category::Syntax => "syntax error",
+ Category::Eof => "unexpected end of input",
+ Category::Data => serde_error_kind(&e.to_string()).unwrap_or("invalid data"),
+ }
+}
+
+/// The log names the parameter and the kind of error, not the value: a
+/// query can hold personal data, such as a search term.
impl From<serde_path_to_error::Error<serde_urlencoded::de::Error>> for ApiError {
fn from(value: serde_path_to_error::Error<serde_urlencoded::de::Error>) -> Self {
- let fmt = value.to_string();
- if fmt.contains("missing") {
- failure(ErrorCode::GENERIC_PARAMETER_MISSING, value.inner())
- .with_path(value.path().to_string())
- .with_log(fmt)
+ let path = value.path().to_string();
+ let message = value.inner().to_string();
+ let log = format!(
+ "{path}: {}",
+ serde_error_kind(&message).unwrap_or("invalid parameter")
+ );
+ let code = if message.contains("missing") {
+ ErrorCode::GENERIC_PARAMETER_MISSING
} else {
- failure(ErrorCode::GENERIC_PARAMETER_MALFORMED, value.inner())
- .with_path(value.path().to_string())
- .with_log(fmt)
- }
+ ErrorCode::GENERIC_PARAMETER_MALFORMED
+ };
+ failure(code, message).with_path(path).with_log(log)
}
}
+/// The log names the path and the kind of error, not serde_json's message
+/// (see [`json_error_kind`]); the client gets the message as the hint.
impl From<serde_path_to_error::Error<serde_json::Error>> for ApiError {
fn from(value: serde_path_to_error::Error<serde_json::Error>) -> Self {
+ let path = value.path().to_string();
+ let log = format!("{path}: {}", json_error_kind(value.inner()));
failure(ErrorCode::GENERIC_JSON_INVALID, value.inner())
- .with_path(value.path().to_string())
- .with_log(value.to_string())
+ .with_path(path)
+ .with_log(log)
}
}
--
2.47.3
From fe0f44d710fff4a6a47717126a2bcc2cf2cf7fe8 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 15:13:01 +0100
Subject: [PATCH 9/9] api: answer every accepted connection on a graceful
shutdown
On a graceful shutdown, axum::serve tells every open connection to shut
down. A connection whose first request has not been read yet then closes at
once: hyper-util's auto builder cancels the detection of its HTTP version,
and hyper closes an HTTP/1 connection that has no request in progress. The
server had accepted the connection, but the client's request is lost, with a
reset or a close without an answer. Under systemd socket activation with
back-to-back restarts, about one request in 6,000 failed this way.
serve() now runs its own accept loop. At shutdown it stops accepting, gives
each connection that has not started a request up to ten seconds to send
its first one, and only then shuts the connection down gracefully, so that
the request in progress is answered. It returns when every connection has
closed. A test sends a request on a connection accepted before the
shutdown; with axum::serve, it got no answer.
Assisted by LLM
---
common/taler-api/Cargo.toml | 2 +
common/taler-api/src/api.rs | 194 ++++++++++++++++++++++++++++++++++--
2 files changed, 187 insertions(+), 9 deletions(-)
diff --git a/common/taler-api/Cargo.toml b/common/taler-api/Cargo.toml
index 6bed8d6..1debe1f 100644
--- a/common/taler-api/Cargo.toml
+++ b/common/taler-api/Cargo.toml
@@ -15,6 +15,8 @@ doctest = false
listenfd = "1.0.0"
dashmap = "6.1"
http-body-util.workspace = true
+hyper.workspace = true
+hyper-util = { workspace = true, features = ["server", "service", "tokio"] }
zlib-rs = "0.6.3"
tokio = { workspace = true, features = ["signal"] }
rand.workspace = true
diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs
index e7ba351..52f34b5 100644
--- a/common/taler-api/src/api.rs
+++ b/common/taler-api/src/api.rs
@@ -19,7 +19,7 @@ use std::{
Arc,
atomic::{AtomicU32, Ordering},
},
- time::Instant,
+ time::{Duration, Instant},
};
use axum::{
@@ -29,6 +29,12 @@ use axum::{
response::Response,
};
use compact_str::CompactString;
+use hyper::{body::Incoming, service::Service as _};
+use hyper_util::{
+ rt::{TokioExecutor, TokioIo},
+ server::conn::auto,
+ service::TowerToHyperService,
+};
use rand::RngExt as _;
use revenue::Revenue;
use taler_common::{
@@ -36,8 +42,12 @@ use taler_common::{
log::LOG_TASK_ID,
types::amount::{Amount, Currency},
};
-use tokio::signal;
-use tracing::{Level, debug, info};
+use tokio::{
+ io::{AsyncRead, AsyncWrite},
+ signal,
+ sync::watch,
+};
+use tracing::{Level, debug, info, trace};
use wire::WireGateway;
use crate::{
@@ -144,14 +154,10 @@ impl TalerRouter for Router {
let signal = shutdown_signal(notify);
match listener {
Listener::Tcp(tcp_listener) => {
- axum::serve(tcp_listener, router)
- .with_graceful_shutdown(signal)
- .await?;
+ serve_connections(tcp_listener, router, signal, FIRST_REQUEST_TIMEOUT).await
}
Listener::Unix(unix_listener) => {
- axum::serve(unix_listener, router)
- .with_graceful_shutdown(signal)
- .await?;
+ serve_connections(unix_listener, router, signal, FIRST_REQUEST_TIMEOUT).await
}
}
@@ -160,6 +166,99 @@ impl TalerRouter for Router {
}
}
+/// How long a connection that was accepted before a graceful shutdown may
+/// take to send its first request.
+const FIRST_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
+
+/// Serve `router` on `listener` until `signal`, then finish every connection
+/// that was accepted, and return when the last one has closed.
+///
+/// This is `axum::serve` with a graceful shutdown, except for connections
+/// whose first request has not been read yet. axum tells every connection
+/// to shut down, and such a connection closes at once: hyper-util cancels the
+/// detection of its protocol, and hyper closes an HTTP/1 connection that has
+/// no request in progress. The client, whose connection the server had
+/// accepted, loses its request (a reset, or a close without an answer).
+/// Under systemd socket activation with back-to-back restarts, about one
+/// request in 6,000 was lost this way.
+///
+/// Here a connection that has not started a request yet gets up to
+/// `first_request` to send one. Once it has, or once that time is up, it is
+/// told to shut down: the request in progress is answered, and the
+/// connection closes. The listener is dropped first, so that the server
+/// accepts nothing new; connections that it has not accepted stay in the
+/// queue of an inherited socket for the next process.
+async fn serve_connections<L: axum::serve::Listener>(
+ mut listener: L,
+ router: Router,
+ signal: impl Future<Output = ()>,
+ first_request: Duration,
+) {
+ // Dropped to tell the connections that the server shuts down.
+ let (shutdown_tx, shutdown_rx) = watch::channel(());
+ // Each connection holds a receiver: when none is left, all are closed.
+ let (closed_tx, closed_rx) = watch::channel(());
+ let mut signal = std::pin::pin!(signal);
+ loop {
+ let (io, _) = tokio::select! {
+ accepted = listener.accept() => accepted,
+ () = &mut signal => break,
+ };
+ tokio::spawn(serve_connection(
+ io,
+ router.clone(),
+ shutdown_rx.clone(),
+ closed_rx.clone(),
+ first_request,
+ ));
+ }
+ drop(listener);
+ drop(shutdown_tx);
+ drop(closed_rx);
+ closed_tx.closed().await;
+}
+
+/// Serve one connection; see [`serve_connections`].
+async fn serve_connection<I: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
+ io: I,
+ router: Router,
+ mut shutdown: watch::Receiver<()>,
+ _closed: watch::Receiver<()>,
+ first_request: Duration,
+) {
+ // Becomes true when the connection's first request reaches the router.
+ let (requested_tx, mut requested) = watch::channel(false);
+ let router = TowerToHyperService::new(router);
+ let service = hyper::service::service_fn(move |request: hyper::Request<Incoming>| {
+ requested_tx.send_replace(true);
+ router.call(request)
+ });
+ let builder = auto::Builder::new(TokioExecutor::new());
+ let mut connection =
+ std::pin::pin!(builder.serve_connection_with_upgrades(TokioIo::new(io), service));
+ let finished = |result: Result<(), Box<dyn std::error::Error + Send + Sync>>| {
+ if let Err(e) = result {
+ trace!(target: "api", "connection: {e}");
+ }
+ };
+ tokio::select! {
+ result = connection.as_mut() => return finished(result),
+ // The sender is dropped, which is the signal.
+ _ = shutdown.changed() => {}
+ }
+ if !*requested.borrow() {
+ let first = async {
+ let _ = requested.wait_for(|r| *r).await;
+ };
+ tokio::select! {
+ result = connection.as_mut() => return finished(result),
+ _ = tokio::time::timeout(first_request, first) => {}
+ }
+ }
+ connection.as_mut().graceful_shutdown();
+ finished(connection.await);
+}
+
/// Allow cross-origin requests from any origin, with any method and headers.
///
/// Only a CORS preflight, an OPTIONS request with Access-Control-Request-Method
@@ -594,4 +693,81 @@ mod tests {
warnings[1]
);
}
+
+ /// A listener that reports each connection that it has accepted.
+ struct Reporting(
+ tokio::net::TcpListener,
+ tokio::sync::mpsc::UnboundedSender<()>,
+ );
+
+ impl axum::serve::Listener for Reporting {
+ type Io = tokio::net::TcpStream;
+ type Addr = std::net::SocketAddr;
+
+ async fn accept(&mut self) -> (Self::Io, Self::Addr) {
+ let accepted = self.0.accept().await.unwrap();
+ self.1.send(()).unwrap();
+ accepted
+ }
+
+ fn local_addr(&self) -> std::io::Result<Self::Addr> {
+ self.0.local_addr()
+ }
+ }
+
+ /// A graceful shutdown answers a connection that the server accepted
+ /// before it, even if the request arrives only afterwards; with
+ /// axum::serve, such a connection closed without an answer. A
+ /// connection that sends nothing is closed after the time for its first
+ /// request, and then the server returns.
+ #[tokio::test]
+ async fn shutdown_answers_accepted_connections() {
+ use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
+
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let (accepted_tx, mut accepted) = tokio::sync::mpsc::unbounded_channel();
+ let (stop, stopped) = tokio::sync::oneshot::channel::<()>();
+ let router = Router::new().route("/", get(async || "ok"));
+ let server = tokio::spawn(serve_connections(
+ Reporting(listener, accepted_tx),
+ router,
+ async {
+ let _ = stopped.await;
+ },
+ Duration::from_millis(300),
+ ));
+
+ let mut late = tokio::net::TcpStream::connect(addr).await.unwrap();
+ accepted.recv().await.unwrap();
+ let mut silent = tokio::net::TcpStream::connect(addr).await.unwrap();
+ accepted.recv().await.unwrap();
+ stop.send(()).unwrap();
+ // The server accepts nothing more once it shuts down.
+ for _ in 0..100 {
+ if tokio::net::TcpStream::connect(addr).await.is_err() {
+ break;
+ }
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ assert!(tokio::net::TcpStream::connect(addr).await.is_err());
+ // Let the connections see the shutdown before the request comes.
+ tokio::time::sleep(Duration::from_millis(100)).await;
+
+ late.write_all(b"GET / HTTP/1.1\r\nhost: example\r\n\r\n")
+ .await
+ .unwrap();
+ let mut answer = String::new();
+ late.read_to_string(&mut answer).await.unwrap();
+ assert!(answer.starts_with("HTTP/1.1 200 OK\r\n"), "{answer}");
+ assert!(answer.ends_with("\r\n\r\nok"), "{answer}");
+
+ let mut nothing = Vec::new();
+ silent.read_to_end(&mut nothing).await.unwrap();
+ assert!(nothing.is_empty());
+ tokio::time::timeout(Duration::from_secs(5), server)
+ .await
+ .expect("the server returns once all connections are closed")
+ .unwrap();
+ }
}
--
2.47.3
| ||||
| ID | Summary | Status | Locked by | Estimated effort (hours) |
|---|---|---|---|---|
| 0012176 | taler-api: accept error codes from other GANA registries | new | ||
| 0012177 | taler-api: allow extra details in error responses | new | ||
| 0012178 | taler-api: make the request body limit configurable per router | new | ||
| 0012179 | taler-api: remember whether a database error is worth retrying | new | ||
| 0012180 | taler-api: log the route, not the path and query | new | ||
| 0012181 | taler-api: let routes answer OPTIONS requests that are not CORS preflights | new | ||
| 0012182 | taler-api: answer a body that cannot be read with 400 | new | ||
| 0012183 | taler-api: keep the values of a refused body or query out of the log | new | ||
| 0012184 | taler-api: answer every accepted connection on a graceful shutdown | new | ||
| 0012185 | taler-common: dbinit logs "sqlqir" instead of "sqldir" | new | ||
| This issue (hours) | ||||
| Its children (hours) | 0 | |||
| Together (hours) | 0 | |||
Not planned yet: 0012176, 0012177, 0012178, 0012179, 0012180, 0012181, 0012182, 0012183, 0012184, 0012185. The plan of this issue cannot be finished while part of it is unplanned.
| parent of | 0012176 | new | taler-api: accept error codes from other GANA registries | |
| parent of | 0012177 | new | taler-api: allow extra details in error responses | |
| parent of | 0012178 | new | taler-api: make the request body limit configurable per router | |
| parent of | 0012179 | new | taler-api: remember whether a database error is worth retrying | |
| parent of | 0012180 | new | taler-api: log the route, not the path and query | |
| parent of | 0012181 | new | taler-api: let routes answer OPTIONS requests that are not CORS preflights | |
| parent of | 0012182 | new | taler-api: answer a body that cannot be read with 400 | |
| parent of | 0012183 | new | taler-api: keep the values of a refused body or query out of the log | |
| parent of | 0012184 | new | taler-api: answer every accepted connection on a graceful shutdown | |
| parent of | 0012185 | new | taler-common: dbinit logs "sqlqir" instead of "sqldir" | |
| Not all the children of this issue are yet resolved or closed. | ||||
| Date Modified | Username | Field | Change |
|---|---|---|---|
| 2026-11-18 14:41 | bot-cg | New Issue | |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012181 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012176 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012177 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012178 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012185 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012184 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012183 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012182 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012179 |
| 2026-11-18 14:41 | bot-cg | Relationship added | parent of 0012180 |
| 2026-11-18 14:41 | bot-cg | Implementer | => Antoine A |
| 2026-11-18 14:41 | bot-cg | Reviewer | => Florian Dold |
| 2026-11-18 14:41 | bot-cg | Release signed by | => Antoine A |
| 2026-11-18 14:41 | bot-cg | Deployed by | => Antoine A |
| 2026-11-18 14:41 | bot-cg | Verifier | => Antoine A |
| 2026-11-18 14:41 | bot-cg | Communicator | => CM_7 |
| 2026-11-18 14:42 | bot-cg | File Added: taler-api-series-2c678873.mbox |