Skip to content

Commit 3edaada

Browse files
committed
Make download_tracker thread safe.
Not however concurrency safe. This is sufficient to permit introducing an async runtime throughout rustup, but not sufficient for us to start tracking many concurrent downloads.
1 parent 42a44ff commit 3edaada

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

src/cli/common.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Just a dumping ground for cli stuff
22
3+
use std::cell::RefCell;
34
use std::fmt::Display;
45
use std::fs;
56
use std::io::{BufRead, ErrorKind, Write};
@@ -12,6 +13,7 @@ use git_testament::{git_testament, render_testament};
1213
use lazy_static::lazy_static;
1314

1415
use super::self_update;
16+
use crate::cli::download_tracker::DownloadTracker;
1517
use crate::currentprocess::{
1618
argsource::ArgSource,
1719
filesource::{StdinSource, StdoutSource},
@@ -168,18 +170,14 @@ impl NotifyOnConsole {
168170

169171
#[cfg_attr(feature = "otel", tracing::instrument)]
170172
pub(crate) fn set_globals(verbose: bool, quiet: bool) -> Result<Cfg> {
171-
use std::cell::RefCell;
172-
173-
use super::download_tracker::DownloadTracker;
174-
175-
let download_tracker = RefCell::new(DownloadTracker::new().with_display_progress(!quiet));
173+
let download_tracker = DownloadTracker::new_with_display_progress(!quiet);
176174
let console_notifier = RefCell::new(NotifyOnConsole {
177175
verbose,
178176
..Default::default()
179177
});
180178

181179
Cfg::from_env(Arc::new(move |n: Notification<'_>| {
182-
if download_tracker.borrow_mut().handle_notification(&n) {
180+
if download_tracker.lock().unwrap().handle_notification(&n) {
183181
return;
184182
}
185183
console_notifier.borrow_mut().handle(n);

src/cli/download_tracker.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::VecDeque;
22
use std::fmt;
33
use std::io::Write;
4+
use std::sync::{Arc, Mutex};
45
use std::time::{Duration, Instant};
56

67
use crate::currentprocess::{filesource::StdoutSource, process, terminalsource};
@@ -13,6 +14,9 @@ use crate::Notification;
1314
const DOWNLOAD_TRACK_COUNT: usize = 5;
1415

1516
/// Tracks download progress and displays information about it to a terminal.
17+
///
18+
/// *not* safe for tracking concurrent downloads yet - it is basically undefined
19+
/// what will happen.
1620
pub(crate) struct DownloadTracker {
1721
/// Content-Length of the to-be downloaded object.
1822
content_len: Option<usize>,
@@ -46,8 +50,8 @@ pub(crate) struct DownloadTracker {
4650

4751
impl DownloadTracker {
4852
/// Creates a new DownloadTracker.
49-
pub(crate) fn new() -> Self {
50-
Self {
53+
pub(crate) fn new_with_display_progress(display_progress: bool) -> Arc<Mutex<Self>> {
54+
Arc::new(Mutex::new(Self {
5155
content_len: None,
5256
total_downloaded: 0,
5357
downloaded_this_sec: 0,
@@ -57,13 +61,8 @@ impl DownloadTracker {
5761
term: process().stdout().terminal(),
5862
displayed_charcount: None,
5963
units: vec![Unit::B],
60-
display_progress: true,
61-
}
62-
}
63-
64-
pub(crate) fn with_display_progress(mut self, display_progress: bool) -> Self {
65-
self.display_progress = display_progress;
66-
self
64+
display_progress: display_progress,
65+
}))
6766
}
6867

6968
pub(crate) fn handle_notification(&mut self, n: &Notification<'_>) -> bool {

src/cli/self_update/windows.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::cell::RefCell;
21
use std::env::{consts::EXE_SUFFIX, split_paths};
32
use std::ffi::{OsStr, OsString};
43
use std::fmt;
@@ -181,13 +180,14 @@ pub(crate) fn try_install_msvc(opts: &InstallOpts<'_>) -> Result<ContinueInstall
181180
.context("error creating temp directory")?;
182181

183182
let visual_studio = tempdir.path().join("vs_setup.exe");
184-
let download_tracker = RefCell::new(DownloadTracker::new().with_display_progress(true));
185-
download_tracker.borrow_mut().download_finished();
183+
let download_tracker = DownloadTracker::new_with_display_progress(true);
184+
download_tracker.lock().unwrap().download_finished();
186185

187186
info!("downloading Visual Studio installer");
188187
utils::download_file(&visual_studio_url, &visual_studio, None, &move |n| {
189188
download_tracker
190-
.borrow_mut()
189+
.lock()
190+
.unwrap()
191191
.handle_notification(&crate::Notification::Install(
192192
crate::dist::Notification::Utils(n),
193193
));

0 commit comments

Comments
 (0)