Dr-Emann / applesauce

Transparent compression for Apple File System Compression (AFSC)
GNU General Public License v3.0
21 stars 0 forks source link

Panics #84

Open rhizoome opened 2 months ago

rhizoome commented 2 months ago

I get this panics from time to time on a Apple M1. Nothing ever broke, but it is kind of scary.

sudo applesauce compress --verify --minimum-compression-ratio 0.9 .  
thread 'writer 4' panicked at /Users/ganwell/.cargo/registry/src/index.crates.io-6f17d22bba15001f/applesauce-0.5.4/src/threads/writer.rs:138:18:
Reader should drop file before finishing writing blocks, writer should have the only reference
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
sudo sh -c "RUST_BACKTRACE=full applesauce compress --verify --minimum-compression-ratio 0.9 ."
thread 'writer 11' panicked at /Users/ganwell/.cargo/registry/src/index.crates.io-6f17d22bba15001f/applesauce-0.5.4/src/threads/writer.rs:138:18:
Reader should drop file before finishing writing blocks, writer should have the only reference
stack backtrace:
   0:        0x1025c8f9c - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h243268f17d714c7f
   1:        0x1025e3de0 - core::fmt::write::hb3cfb8a30e72d7ff
   2:        0x1025c6b8c - std::io::Write::write_fmt::hfb2314975de9ecf1
   3:        0x1025c9ebc - std::panicking::default_hook::{{closure}}::h14c7718ccf39d316
   4:        0x1025c9ae0 - std::panicking::default_hook::hc62e60da3be2f352
   5:        0x1025ca980 - std::panicking::rust_panic_with_hook::h09e8a656f11e82b2
   6:        0x1025ca2d0 - std::panicking::begin_panic_handler::{{closure}}::h1230eb3cc91b241c
   7:        0x1025c9428 - std::sys::backtrace::__rust_end_short_backtrace::hc3491307aceda2c2
   8:        0x1025c9f98 - _rust_begin_unwind
   9:        0x1026050c4 - core::panicking::panic_fmt::ha4b80a05b9fff47a
  10:        0x102605094 - core::option::expect_failed::h5493e81bb7eff5a4
  11:        0x1025663d0 - <applesauce::threads::writer::Handler as applesauce::threads::WorkHandler<applesauce::threads::writer::WorkItem>>::handle_item::h9977b877b1603082
  12:        0x10256c680 - std::sys::backtrace::__rust_begin_short_backtrace::he6e7199277a0db02
  13:        0x1025695cc - core::ops::function::FnOnce::call_once{{vtable.shim}}::h29d035275ea2a816
  14:        0x1025cdab0 - std::sys::pal::unix::thread::Thread::new::thread_start::h1bd1b9c95010bf71
  15:        0x180f11f94 - __pthread_joiner_wake
thread 'main' panicked at /Users/ganwell/.cargo/registry/src/index.crates.io-6f17d22bba15001f/applesauce-0.5.4/src/threads/mod.rs:31:27:
called `Result::unwrap()` on an `Err` value: Any { .. }
stack backtrace:
   0:        0x1025c8f9c - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h243268f17d714c7f
   1:        0x1025e3de0 - core::fmt::write::hb3cfb8a30e72d7ff
   2:        0x1025c6b8c - std::io::Write::write_fmt::hfb2314975de9ecf1
   3:        0x1025c9ebc - std::panicking::default_hook::{{closure}}::h14c7718ccf39d316
   4:        0x1025c9ae0 - std::panicking::default_hook::hc62e60da3be2f352
   5:        0x1025ca980 - std::panicking::rust_panic_with_hook::h09e8a656f11e82b2
   6:        0x1025ca2d0 - std::panicking::begin_panic_handler::{{closure}}::h1230eb3cc91b241c
   7:        0x1025c9428 - std::sys::backtrace::__rust_end_short_backtrace::hc3491307aceda2c2
   8:        0x1025c9f98 - _rust_begin_unwind
   9:        0x1026050c4 - core::panicking::panic_fmt::ha4b80a05b9fff47a
  10:        0x10260540c - core::result::unwrap_failed::h441932a0bca0dd7f
  11:        0x10256df6c - <applesauce::threads::ThreadJoiner as core::ops::drop::Drop>::drop::hf8ecbb2847dfcfdf
  12:        0x1023d3c24 - core::ptr::drop_in_place<applesauce::threads::BgWorker<applesauce::threads::writer::Work>>::h019cbcf9e67fe6e6
  13:        0x1023d679c - applesauce::main::hf967b0b321aab85e
  14:        0x1023a96b4 - std::sys::backtrace::__rust_begin_short_backtrace::hf3f1e51fc974872e
  15:        0x1023ec91c - std::rt::lang_start::{{closure}}::h5b400a4b0b43981f
  16:        0x1025c17a8 - std::rt::lang_start_internal::hdd117cb81a316264
  17:        0x1023dcc90 - _main
rhizoome commented 2 months ago

Here is how I solved it for the time being:

diff --git a/crates/applesauce/src/threads/writer.rs b/crates/applesauce/src/threads/writer.rs
index 0a809630..89dfad7d 100644
--- a/crates/applesauce/src/threads/writer.rs
+++ b/crates/applesauce/src/threads/writer.rs
@@ -8,7 +8,8 @@ use std::io::{BufRead, BufReader, BufWriter, Seek, Write};
 use std::os::fd::AsRawFd;
 use std::os::macos::fs::MetadataExt;
 use std::sync::Arc;
-use std::{cmp, io, ptr};
+use std::time::Duration;
+use std::{cmp, io, ptr, thread};
 use tempfile::NamedTempFile;

 pub(super) type Sender = crossbeam_channel::Sender<WorkItem>;
@@ -134,8 +135,13 @@ impl Handler {
         if item.context.operation.verify {
             let _entered = tracing::info_span!("verify").entered();

-            let orig_file = Arc::get_mut(&mut item.file)
-                .expect("Reader should drop file before finishing writing blocks, writer should have the only reference");
+            let orig_file = loop {
+                if let Some(file) = Arc::get_mut(&mut item.file) {
+                    break file;
+                } else {
+                    thread::sleep(Duration::from_millis(100));
+                }
+            };
             let mut orig_file = BufReader::new(orig_file);
             let mut new_file = BufReader::new(tmp_file.as_file_mut());