Holding span.enter() across an await in async Rust mixed up trace IDs between requests
An engineer investigating logs found that entries from one request sometimes carried another request's trace ID in an async Rust service. The root cause was an axum middleware that held a span.enter() guard across an .await, which keeps the span active while other tasks run on the same thread. The recommended fix is to attach the span to the future with .instrument(span), which enters the span on each poll and exits it on each yield. A test with two interleaved requests on one thread should confirm each log line sits in exactly one correct span, and it should fail when the buggy pattern is reintroduced. The same approach was applied to queue-processing loops, where each message runs in its own span with a late-filled message_id field and early exits converted from continue to return.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in