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

bug: progressbar not cleared after span closed #10

Open
mokurin000 opened this issue Dec 14, 2024 · 4 comments
Open

bug: progressbar not cleared after span closed #10

mokurin000 opened this issue Dec 14, 2024 · 4 comments

Comments

@mokurin000
Copy link

mokurin000 commented Dec 14, 2024

#![feature(mpmc_channel)]

use std::{error::Error, time::Duration};

use indicatif::ProgressStyle;
use rand::random;
use tracing::{info_span, Level};
use tracing_core::LevelFilter;
use tracing_indicatif::{span_ext::IndicatifSpanExt, IndicatifLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};

type Result<T> = std::result::Result<T, Box<dyn Error + Sync + Send>>;

const WORKER_NUM: Option<&str> = option_env!("WORKER_NUM");

#[tokio::main]
async fn main() -> Result<()> {
    let indicatif_layer = IndicatifLayer::new()
        .with_progress_style(
            ProgressStyle::default_bar()
                .template(
                    "{spinner:.green} {msg} [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})",
                )?
                .progress_chars("#>-"),
        )
        .with_max_progress_bars(u64::MAX, None);
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer()
                .with_level(true)
                .with_writer(indicatif_layer.get_stderr_writer())
                .with_filter(LevelFilter::from_level(Level::DEBUG)),
        )
        .with(indicatif_layer)
        .init();

    let (tx, rx) = std::sync::mpmc::channel();

    for _ in 0..WORKER_NUM.unwrap_or("4").parse()? {
        let rx = rx.clone();
        tokio::spawn(async move {
            while let Ok(_) = rx.recv() {
                payload().await
            }
        });
    }
    drop(rx);

    loop {
        let _ = tx.send(());
    }
}

async fn payload() {
    let target: u32 = random::<u16>() as u32 * 1000;

    let span = info_span!("download");
    span.pb_set_length(target as _);
    let _enter = span.enter();

    let mut cur: u32 = 0;
    let speed = 1024 * 1024;
    loop {
        cur += speed;

        span.pb_set_position(cur.min(target) as _);
        if cur >= target {
            drop(_enter);
            drop(span);
            break;
        }

        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

image

mokurin000 added a commit to kawaii-hentai-dev/tracing-indicatif that referenced this issue Dec 14, 2024
this is originally a workaround for the `deadlock` found in issue emersonford#10
@emersonford
Copy link
Owner

Is it a deadlock in that the program never terminates? Or just that the progress bar doesn't disappear, but the program still exits?

@mokurin000
Copy link
Author

Is it a deadlock in that the program never terminates? Or just that the progress bar doesn't disappear, but the program still exits?

Nope. It's the progress bar never disappear.
on_close triggered, but the pb_manager lock cannot be acquired

@mokurin000
Copy link
Author

Is it a deadlock in that the program never terminates? Or just that the progress bar doesn't disappear, but the program still exits?

I will write a minimum reproduce code later. Also, in the multithread example, everything works fine; the main difference to the example is, tasks were spawned on tokio multithread runtime.

mokurin000 added a commit to kawaii-hentai-dev/tracing-indicatif that referenced this issue Dec 15, 2024
@mokurin000
Copy link
Author

mokurin000 commented Dec 15, 2024

@emersonford could you check my example code?

https://github.com/kawaii-hentai-dev/tracing-indicatif/blob/fix-deadlock/examples/tokio-multithread.rs

Update: Oops. It's not a deadlock issue

@mokurin000 mokurin000 changed the title bug: deadlock at pb_manager.lock() inside on_close bug: progressbar not cleared after span closed Dec 15, 2024
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

2 participants