rust-lang / rust-lang/rust-clippy

Await in tracing span

Open
#8,722 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Check that while in a span, calls to .await are properly instrumented.

Extract from the documentation https://docs.rs/tracing/latest/tracing/struct.Span.html#in-asynchronous-code :

Warning: in asynchronous code that uses async/await syntax, Span::enter should be used very carefully or avoided entirely. Holding the drop guard returned by Span::enter across .await points will result in incorrect traces.

Lint Name

uninstrumented_await_while_span_entered

Category

correctness, suspicious

Advantage

Correct span usage. Without instrumentation of the async task, it will wrongly report the current span.

Drawbacks

No response

Example
async fn my_async_function() {
    let span = info_span!("my_async_function");
    let _enter = span.enter();
    // The `await` keyword may yield, causing the runtime
    // to switch to another task, while remaining in this span!
    some_other_async_function().await

    // ...
}

Could be written as:

use tracing::Instrument;

async fn my_async_function() {
    let span = info_span!("my_async_function");
    async move {
       some_other_async_function().await;
       //...
    }
      // instrument the async block with the span
      .instrument(span)
      .await
}

or less generally as

use tracing::Instrument;

async fn my_async_function() {
    let some_value = some_other_async_function()
       .instrument(debug_span!("my_async_function"))
       .await;

    // ...
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the tracing::Span documentation linked in the issue, especially its asynchronous-code warning, then compare the two provided examples. Define the uninstrumented_await_while_span_entered lint around detecting an await while a span-enter guard remains active, and use the examples to verify that instrumented async code is the accepted form.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.