task_local, logging context and tokio::task::spawn_blocking #6404
Unanswered
mgumbley-resilient
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Unfortunately, the You could make your own |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I'm using Rocket 0.5 (Tokio 1.6.1) for a microservice. The incoming requests are JSON documents which all contain a correlation id.
In earlier versions that were thread-based, we added this correlation id to thread-local storage, and used log4rs/log_mdc to emit the id on every log statement emitted during processing of the call, so we could easily see which downstream systems/crates were used for this call.
This does not work in Rocket 0.5 since it's async.. the various async tasks in our code are switched between threads so that a task may generate logging with the wrong correlation id. The upgrade to Rocket 0.5 meant we had to bridge between async and sync code using tokio::task::spawn_blocking and tokio::task::block_in_place(move || handle.block_on(... The system works, but is messy.
I see that tokio has task_local! and after reading "How does task_local! work" at #6111 tried to switch to it. The correlation id logging works until the first tokio::task::spawn_blocking, then the thread changes, and it's not subsequently logged.
Is task_local! incompatible with sync/async bridging? Must we fully convert all code to async to get this working?
Thank you in advance,
Matt Gumbley
Beta Was this translation helpful? Give feedback.
All reactions