Skip to content

Commit

Permalink
Remove update_read_async method and use update_read instead
Browse files Browse the repository at this point in the history
The implementation of `update_read_async` caused stack overflows on
Windows only, with exact details being quite a mystery. We did not
really need a super performant async version of this code, as we are
just using it to compute checksums of files residing in well known
local filesystems, and thus the plain old `spawn_blocking` call should
do as fine job as the removed code was expected to.

Signed-off-by: Marek Kaput <[email protected]>
  • Loading branch information
mkaput committed Nov 21, 2023
1 parent 96074ac commit 8381189
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 46 deletions.
25 changes: 0 additions & 25 deletions scarb/src/core/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use anyhow::{bail, ensure, Context, Result};
use data_encoding::{Encoding, HEXLOWER_PERMISSIVE};
use serde::{Deserialize, Serialize};
use sha2::Digest as _;
use tokio::io::{AsyncRead, AsyncReadExt};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
Expand Down Expand Up @@ -133,20 +132,6 @@ impl Digest {
}
}

pub async fn update_read_async(
&mut self,
mut input: impl AsyncRead + Unpin,
) -> Result<&mut Self> {
let mut buf = [0; 64 * 1024];
loop {
let n = input.read(&mut buf).await?;
if n == 0 {
break Ok(self);
}
self.update(&buf[..n]);
}
}

pub fn finish(&mut self) -> Checksum {
Checksum(self.0.finalize_reset().into())
}
Expand Down Expand Up @@ -211,14 +196,4 @@ mod tests {
.finish();
assert_eq!(actual, lorem_checksum());
}

#[tokio::test]
async fn digest_read_async() {
let actual = Digest::recommended()
.update_read_async(Cursor::new(LOREM))
.await
.unwrap()
.finish();
assert_eq!(actual, lorem_checksum());
}
}
41 changes: 20 additions & 21 deletions scarb/src/core/registry/client/cache.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::io::SeekFrom;
use std::io::{Seek, SeekFrom};

use anyhow::{anyhow, bail, ensure, Context, Result};
use camino::Utf8Path;
use redb::{MultimapTableDefinition, ReadableMultimapTable, ReadableTable, TableDefinition};
use semver::Version;
use tokio::io::{AsyncSeekExt, BufReader};
use tokio::sync::OnceCell;
use tokio::task::block_in_place;
use tokio::task::{block_in_place, spawn_blocking};
use tracing::trace;

use scarb_ui::Ui;
Expand Down Expand Up @@ -214,25 +213,25 @@ impl<'c> RegistryClientCache<'c> {
&self,
package: PackageId,
checksum: &Checksum,
file: FileLockGuard,
mut file: FileLockGuard,
) -> Result<FileLockGuard> {
let mut file = file.into_async();

file.seek(SeekFrom::Start(0)).await?;
let actual = checksum
.digest()
.update_read_async(BufReader::new(&mut *file))
.await
.with_context(|| format!("failed to calculate checksum of: {package}"))?
.finish();

ensure!(
actual == *checksum,
"failed to verify the checksum of downloaded archive"
);

let file = file.into_sync().await;
Ok(file)
let checksum = checksum.clone();
spawn_blocking(move || -> Result<_> {
file.seek(SeekFrom::Start(0))?;
let actual = checksum
.digest()
.update_read(&mut *file)
.with_context(|| format!("failed to calculate checksum of: {package}"))?
.finish();

ensure!(
actual == checksum,
"failed to verify the checksum of downloaded archive"
);

Ok(file)
})
.await?
}

async fn get_record_maybe_uncached(&self, package: PackageId) -> Result<IndexRecord> {
Expand Down

0 comments on commit 8381189

Please sign in to comment.