cunarist / rinf

Rust for native business logic, Flutter for flexible and beautiful GUI
MIT License
1.82k stars 64 forks source link

send_signal_to_dart fails with unsafe precondition(s) violated: NonNull::new_unchecked requires that the pointer is non-null #371

Closed TheTharin closed 2 months ago

TheTharin commented 2 months ago

Report

send_signal_to_dart fails with unsafe precondition(s) violated: NonNull::new_unchecked requires that the pointer is non-null on 1.78.0 toolchain. Works OK on 1.77.2 toolchain.

Here's a full stack trace:

running 1 test
thread 'ecs::systems::dialogue_systems::tests::test_dialogue_option_chosen_event_handler' panicked at library/core/src/panicking.rs:156:5:
unsafe precondition(s) violated: NonNull::new_unchecked requires that the pointer is non-null
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:110:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:123:9
   3: core::panicking::panic_nounwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:156:5
   4: core::ptr::non_null::NonNull<T>::new_unchecked::precondition_check
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/intrinsics.rs:2799:21
   5: core::ptr::non_null::NonNull<T>::new_unchecked
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ptr/non_null.rs:220:13
   6: core::ptr::unique::Unique<T>::new_unchecked
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ptr/unique.rs:89:36
   7: alloc::raw_vec::RawVec<T,A>::from_raw_parts_in
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/raw_vec.rs:250:30
   8: alloc::vec::Vec<T,A>::from_raw_parts_in
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/vec/mod.rs:820:29
   9: alloc::vec::Vec<T>::from_raw_parts
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/vec/mod.rs:604:18
  10: <<allo_isolate::ffi::DartCObject as core::ops::drop::Drop>::drop::MyVisitor as allo_isolate::into_dart::DartTypedDataTypeVisitor>::visit
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/allo-isolate-0.1.24/src/ffi.rs:186:29
  11: allo_isolate::into_dart::visit_dart_typed_data_type
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/allo-isolate-0.1.24/src/into_dart.rs:268:35
  12: <allo_isolate::ffi::DartCObject as core::ops::drop::Drop>::drop
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/allo-isolate-0.1.24/src/ffi.rs:196:17
  13: core::ptr::drop_in_place<allo_isolate::ffi::DartCObject>
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ptr/mod.rs:515:1
  14: core::ptr::drop_in_place<alloc::boxed::Box<allo_isolate::ffi::DartCObject>>
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ptr/mod.rs:515:1
  15: <allo_isolate::dart_array::DartArray as core::ops::drop::Drop>::drop
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/allo-isolate-0.1.24/src/dart_array.rs:68:42
  16: core::ptr::drop_in_place<allo_isolate::dart_array::DartArray>
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ptr/mod.rs:515:1
  17: <allo_isolate::ffi::DartCObject as core::ops::drop::Drop>::drop
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/allo-isolate-0.1.24/src/ffi.rs:179:72
  18: core::ptr::drop_in_place<allo_isolate::ffi::DartCObject>
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ptr/mod.rs:515:1
  19: allo_isolate::Isolate::post
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/allo-isolate-0.1.24/src/lib.rs:152:5
  20: rinf::interface_os::send_rust_signal_extern
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rinf-6.7.0/src/interface_os.rs:32:5
  21: rinf::interface::send_rust_signal
             at /Users/tharin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rinf-6.7.0/src/interface.rs:24:5

Steps to Reproduce

1) add

extern "C" { fn prepare_isolate_extern(port: i64); }

to your tests module

2) call

unsafe {
    prepare_isolate_extern(42);
}

in your test.

3) call

send_signal_to_dart

in your test.

System Information

Please provide the output from the command below, using markdown codeblock syntax.

rustc --version 1.78.0
rinf 6.7.0
temeddix commented 2 months ago

Hi there :)

This problem should have been fixed in Rinf 6.8.0. Would you try upgrading Rinf and try running the app?

TheTharin commented 2 months ago

I've upgraded to Rinf 6.8.0, but now I get these errors:

lib.rs:

rinf::write_interface!(); 

mismatched types
expected enum `std::option::Option<std::vec::Vec<_>>`
 found struct `std::vec::Vec<_>`

messages/my_message.rs

use rinf::SharedCell;

unresolved import `rinf::SharedCell`
no `SharedCell` in the root
TheTharin commented 2 months ago

UPD: I think I get why it's throwing me these errors, rinf v6.8.0 is not present in cargo packages list, so it always defaults to v6.12.0 for me: Downloaded rinf v6.12.1 Okay, specifiying it that way worked:

rinf = { git = "https://github.com/cunarist/rinf.git", tag = 'v6.7.0' }
tokio_with_wasm = "0.4.3"

I decided to fall back to rustc 1.77.2 and rinf 6.7.0 for the time being.

temeddix commented 2 months ago

That error message happens when you need to run rinf message with the newer version. Message channels are becoming more efficient under the hood as version increases.

Would you try rinf message with the newest Rind version if you don't mind?

TheTharin commented 2 months ago

Welp, thanks for your answers, as I said, I fell back to 6.7.0 and regenerated the message, everything seemed to start working, except for one thing: Only one send_signal_to_dart worked at a time, and any other subsequent rust code didn't run:

println!("Hello"); // this line runs
SceneActionsChange { actions }.send_signal_to_dart(None); // this line runs (I see the message on the Dart side appear in the Stream
println!("World"); // this line doesn't run

So I decided to instead switch to rustc 1.78.0 and rinf 6.21.1 and see if it fixes the issue. 1) I've updated both pubspec.yaml Rinf to 6.12.1 2) and cargo Rinf to 6.12.1 3) + installed a new rinf v.6.12.1 binary 4) removed the generated messages and re-ran rinf message 5) changed the code a bit to remove unnecessary Blobs from send_signal_to_dart() call

Now I get only one error about: rinf::write_interface!(); dereferencing a raw pointer and not being marked unsafe.

Everything works fine aside this warning in my VSCode, the compilation process works.

temeddix commented 2 months ago

Oh, that error was solved a couple of days ago with the PR below:

Rinf 6.13 will solve that error. It will be relased in a few days. Thanks for your patience.

TheTharin commented 2 months ago

There is one last thing: It seems that tokio_with_wasm crate stopped using tokio with time feature. I use it for my tests to wait some time for the events to be handled. I know it's a different crate, but as we're still here in this thread, would you be so kind to check into that as well? Thank you very much for your answers!

temeddix commented 2 months ago

From tokio_with_wasm 0.5.0, you need to import tokio_with_wasm::alias as tokio, not tokio_with_wasm::tokio. Those features including time remain unchanged(only more efficient). The linked section below will explain things in more detail:

You might need to add time feature at Cargo.toml, too.

TheTharin commented 2 months ago

Oh, I didn't realise I had to use tokio alongside tokio_with_wasm now in my dependencies. Now it works fine, thanks! Have a wonderful day! I hope every library maintainer was as noob friendly as you are :D