From d292546b5491be42c88149285ebbeec3dabc14cc Mon Sep 17 00:00:00 2001
From: Joel Wurtz <jwurtz@jolicode.com>
Date: Fri, 13 Dec 2024 15:49:54 +0100
Subject: [PATCH] feat(files): remove block on, as it does more harm than good
 here

---
 actix-files/CHANGES.md     |  1 +
 actix-files/src/chunked.rs | 21 ++++++++-------------
 2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md
index afb2d5d2..c89c627f 100644
--- a/actix-files/CHANGES.md
+++ b/actix-files/CHANGES.md
@@ -3,6 +3,7 @@
 ## Unreleased
 
 - Minimum supported Rust version (MSRV) is now 1.75.
+- Avoid spawning a thread for each file which does more harm than good
 
 ## 0.6.6
 
diff --git a/actix-files/src/chunked.rs b/actix-files/src/chunked.rs
index c6c01903..24f2e32a 100644
--- a/actix-files/src/chunked.rs
+++ b/actix-files/src/chunked.rs
@@ -80,22 +80,17 @@ async fn chunked_read_file_callback(
 ) -> Result<(File, Bytes), Error> {
     use io::{Read as _, Seek as _};
 
-    let res = actix_web::web::block(move || {
-        let mut buf = Vec::with_capacity(max_bytes);
+    let mut buf = Vec::with_capacity(max_bytes);
 
-        file.seek(io::SeekFrom::Start(offset))?;
+    file.seek(io::SeekFrom::Start(offset))?;
 
-        let n_bytes = file.by_ref().take(max_bytes as u64).read_to_end(&mut buf)?;
+    let n_bytes = file.by_ref().take(max_bytes as u64).read_to_end(&mut buf)?;
 
-        if n_bytes == 0 {
-            Err(io::Error::from(io::ErrorKind::UnexpectedEof))
-        } else {
-            Ok((file, Bytes::from(buf)))
-        }
-    })
-    .await??;
-
-    Ok(res)
+    if n_bytes == 0 {
+        Err(Error::from(io::Error::from(io::ErrorKind::UnexpectedEof)))
+    } else {
+        Ok((file, Bytes::from(buf)))
+    }
 }
 
 #[cfg(feature = "experimental-io-uring")]