From 82e29a541cf9ed3005c1ff4e50bbae0cd5a369ec Mon Sep 17 00:00:00 2001 From: Jakub Koralewski <43069023+JakubKoralewski@users.noreply.github.com> Date: Thu, 20 Nov 2025 16:49:08 +0000 Subject: [PATCH] fix: don't block executor with integrity checking --- src/content/read.rs | 16 ++++++++++++---- src/errors.rs | 6 ++++++ src/get.rs | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/content/read.rs b/src/content/read.rs index 39efbdb..8284c4d 100644 --- a/src/content/read.rs +++ b/src/content/read.rs @@ -118,15 +118,23 @@ pub fn read(cache: &Path, sri: &Integrity) -> Result> { } #[cfg(any(feature = "async-std", feature = "tokio"))] -pub async fn read_async<'a>(cache: &'a Path, sri: &'a Integrity) -> Result> { - let cpath = path::content_path(cache, sri); +pub async fn read_async<'c, 's>(cache: &'c Path, sri: Integrity) -> Result> { + // let cache = cache.to_path_buf(); + let cpath = path::content_path(&cache, &sri); let ret = crate::async_lib::read(&cpath).await.with_context(|| { format!( "Failed to read contents for file at {}", - path::content_path(cache, sri).display() + path::content_path(&cache, &sri).display() ) })?; - sri.check(&ret)?; + let integrity_check = move || { + sri.check(&ret)?; + Ok::<_, ssri::Error>(ret) + }; + #[cfg(feature = "tokio")] + let ret = tokio::task::spawn_blocking(integrity_check).await??; + #[cfg(feature = "async-std")] + let ret = async_std::task::spawn_blocking(integrity_check).await?; Ok(ret) } diff --git a/src/errors.rs b/src/errors.rs index ccb864d..30e1cd5 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -31,6 +31,12 @@ pub enum Error { #[error(transparent)] #[diagnostic(code(cacache::integrity_error), url(docsrs))] IntegrityError(#[from] ssri::Error), + + /// Returned when a Tokio join error occured. + #[cfg(feature="tokio")] + #[error(transparent)] + #[diagnostic(code(cacache::tokio_joinerror), url(docsrs))] + TokioJoinError(#[from] tokio::task::JoinError) } /// The result type returned by calls to this library diff --git a/src/get.rs b/src/get.rs index b4f8101..b481e8f 100644 --- a/src/get.rs +++ b/src/get.rs @@ -159,7 +159,7 @@ where { async fn inner(cache: &Path, key: &str) -> Result> { if let Some(entry) = index::find_async(cache, key).await? { - read_hash(cache, &entry.integrity).await + read_hash(cache, entry.integrity).await } else { Err(Error::EntryNotFound(cache.to_path_buf(), key.into())) } @@ -183,7 +183,7 @@ where /// } /// ``` #[cfg(any(feature = "async-std", feature = "tokio"))] -pub async fn read_hash

(cache: P, sri: &Integrity) -> Result> +pub async fn read_hash

(cache: P, sri: Integrity) -> Result> where P: AsRef, {