Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context confliect in tokio single thread executor #1

Open
TommyCpp opened this issue Nov 8, 2020 · 0 comments
Open

Context confliect in tokio single thread executor #1

TommyCpp opened this issue Nov 8, 2020 · 0 comments

Comments

@TommyCpp
Copy link
Owner

TommyCpp commented Nov 8, 2020

Our trace relays on the Context in thread local storage to store global state. But in tokio single thread executor, everything runs in the same context. Thus, we you spawn a new task, the context will be the same.

Example

use opentelemetry::exporter::trace::stdout;
use opentelemetry::trace::{Tracer, TraceContextExt, StatusCode};
use opentelemetry::{Context, Key};
use std::thread::{spawn, sleep};
use std::borrow::Cow;

fn main() {
    let mut runtime = tokio::runtime::Builder::new().threaded_scheduler().enable_all().build().unwrap();
    runtime.block_on(async {
        let (tracer, _uninstall) = stdout::new_pipeline().install();
        let span = tracer.start("test op1");
        let cx = Context::current_with_span(span);
        let _guard = cx.clone().attach();
        tokio::spawn(async move {
            // in sync enviroment, we will spawn a new thread 
           // and as a result we will have a new Context in a new thread local 
           // but here we are reuse the same context and thread local
            another_operation(None); 
        }).await;
        sleep(std::time::Duration::from_secs(2));
    });
}

fn another_operation(cx: Option<Context>) {
    let cx = Context::current();
    let span = cx.span();
    span.update_name("test op2".to_string());
}

Actually this problem could also occur in multiple thread executors since there is no guarantee which thread task will run on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant