Panic for struct that is initializied

Hi,
I am writing a program that parses messages into structs, and when I add any more than 2 types for my enum, I get the following crash:

Finished dev [unoptimized + debuginfo] target(s) in 0.21s
     Running `target/debug/storage`
thread 'main' panicked at library/core/src/panicking.rs:136:5:
attempted to leave type `data_packet::DataPacket` uninitialized, which is invalid
stack backtrace:
   0:        0x100dfdf20 - std::backtrace_rs::backtrace::libunwind::trace::h2966c6fbfac9d426
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:        0x100dfdf20 - std::backtrace_rs::backtrace::trace_unsynchronized::h8a5f4aefe890b7c5
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:        0x100dfdf20 - std::sys_common::backtrace::_print_fmt::h7574dd98fd39c257
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:67:5
   3:        0x100dfdf20 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h298c9ab285ff3934
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:44:22
   4:        0x100e1b070 - core::fmt::rt::Argument::fmt::hf9661447f7b99899
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/fmt/rt.rs:142:9
   5:        0x100e1b070 - core::fmt::write::h4e276abdb6d0c2a1
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/fmt/mod.rs:1120:17
   6:        0x100dfbab8 - std::io::Write::write_fmt::hd421848f5f0bf9d0
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/io/mod.rs:1762:15
   7:        0x100dfdd5c - std::sys_common::backtrace::_print::h09e653c6686dbd70
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:47:5
   8:        0x100dfdd5c - std::sys_common::backtrace::print::hd8bd9ecab1f94b94
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:34:9
   9:        0x100dff268 - std::panicking::default_hook::{{closure}}::h520eeb743fc98fb4
  10:        0x100dfefb0 - std::panicking::default_hook::ha6550ffe49b63df1
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:292:9
  11:        0x100dff690 - std::panicking::rust_panic_with_hook::hddb0e884a202de7c
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:779:13
  12:        0x100dff56c - std::panicking::begin_panic_handler::{{closure}}::hd2798398a2fd9077
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:649:13
  13:        0x100dfe388 - std::sys_common::backtrace::__rust_end_short_backtrace::h9201cc364dbb8a23
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:170:18
  14:        0x100dff32c - rust_begin_unwind
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:645:5
  15:        0x100e2d9e8 - core::panicking::panic_nounwind_fmt::h04bcff98aa2e1532
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:106:14
  16:        0x100e2da68 - core::panicking::panic_nounwind::h31b3f43bd408e143
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:136:5
  17:        0x100986418 - core::mem::uninitialized::h501fe16e9d695214
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/mem/mod.rs:693:9
  18:        0x100986418 - lockfree::removable::Removable<T>::empty::ha6345cd3b4fccbb1
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lockfree-0.5.1/src/removable.rs:29:46
  19:        0x100986c54 - lockfree::queue::Queue<T>::with_incin::h80ee08b0c19330bb
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lockfree-0.5.1/src/queue.rs:29:30
  20:        0x100986dd4 - lockfree::queue::Queue<T>::new::h786810dbe104c1a6
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lockfree-0.5.1/src/queue.rs:24:9
  21:        0x100968848 - storage::main::{{closure}}::hdeaf5ed11d084533
                               at /Users/pranav/Documents/GT Trading/data-storage/src/main.rs:23:58
  22:        0x10096d144 - tokio::runtime::park::CachedParkThread::block_on::{{closure}}::heefeb773b0eceafe
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/park.rs:282:63
  23:        0x10096cfac - tokio::runtime::coop::with_budget::h31f2d451fbde0c94
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/coop.rs:107:5
  24:        0x10096cfac - tokio::runtime::coop::budget::h7afb90c3250597c1
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/coop.rs:73:5
  25:        0x10096cfac - tokio::runtime::park::CachedParkThread::block_on::haa5b48f30c391175
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/park.rs:282:31
  26:        0x100958858 - tokio::runtime::context::blocking::BlockingRegionGuard::block_on::he06de76572804254
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/context/blocking.rs:66:9
  27:        0x10095ec5c - tokio::runtime::scheduler::multi_thread::MultiThread::block_on::{{closure}}::he1cabdf786bc5dc7
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/scheduler/multi_thread/mod.rs:87:13
  28:        0x100957e00 - tokio::runtime::context::runtime::enter_runtime::hbd6a569fcf488be4
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/context/runtime.rs:65:16
  29:        0x10095ec18 - tokio::runtime::scheduler::multi_thread::MultiThread::block_on::hf4e029f179f32bd2
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/scheduler/multi_thread/mod.rs:86:9
  30:        0x100991a70 - tokio::runtime::runtime::Runtime::block_on::h27b0bb0f52f2272f
                               at /Users/pranav/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/runtime.rs:350:45
  31:        0x10097d6d8 - storage::main::h42050803589a1f5e
                               at /Users/pranav/Documents/GT Trading/data-storage/src/main.rs:37:5
  32:        0x10094b9a8 - core::ops::function::FnOnce::call_once::hd453057d6d53e5d7
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:250:5
  33:        0x10097d588 - std::sys_common::backtrace::__rust_begin_short_backtrace::h646fb791e7ba6ca1
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:154:18
  34:        0x10096d200 - std::rt::lang_start::{{closure}}::h6afe1c0b5f8945a6
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/rt.rs:167:18
  35:        0x100df8a3c - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h1a7c0e059d971da5
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:284:13
  36:        0x100df8a3c - std::panicking::try::do_call::h07a34a23e615022b
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:552:40
  37:        0x100df8a3c - std::panicking::try::h1111644420b4cc09
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:516:19
  38:        0x100df8a3c - std::panic::catch_unwind::h31a3b9d6e2ef9973
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panic.rs:142:14
  39:        0x100df8a3c - std::rt::lang_start_internal::{{closure}}::h63c3452500a36531
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/rt.rs:148:48
  40:        0x100df8a3c - std::panicking::try::do_call::h9c5c8a2a0a297bb7
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:552:40
  41:        0x100df8a3c - std::panicking::try::h424cfcafca1bde97
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:516:19
  42:        0x100df8a3c - std::panic::catch_unwind::h345d3d448041017f
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panic.rs:142:14
  43:        0x100df8a3c - std::rt::lang_start_internal::h5b246d44f1526226
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/rt.rs:148:20
  44:        0x10096d1cc - std::rt::lang_start::h023494cdbdca364a
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/rt.rs:166:17
  45:        0x10097d768 - _main
thread caused non-unwinding panic. aborting.

The only time I make a data_packet variable is when I initialize and assign a DataPacket struct that is returned through a parsing function. I'm not sure as to why this error is occurring.

Thanks for the help!

Can you share your code?

I strongly agree that it'll be hard to help with zero code.

I searched for the error anyway and I think it's related to this.

If you or a dep are using the deprecated functions, they could be causing UB (in which case you're actually lucky it panicked, instead of something worse).

Yeah, your dependency has undefined behavior (when it doesn't panic) and is unmaintained: Heads-up: UB due to misuse of mem::uninitialized will soon lead to panic (#4) · Issues · Bruno Corrêa Zimmermann / lockfree · GitLab

removable.rs - source

pub fn empty() -> Self {
    Self {
        // This is safe because we will only read from the item if present
        // is true. Present will only be true if we write to it.
        item: ManuallyDrop::new(unsafe { uninitialized() }),
        present: AtomicBool::new(false),
    }
}

It seems like in the past, it was either true or presumed that it was okay to make uninitialized values as long as they aren't read. That is no longer the case.

4 Likes

Solved the issue, the lockfree crate was deprecated so I moved to using this crate instead: crates.io: Rust Package Registry. Thanks for the help!