fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.64k stars 256 forks source link

CI `Dart test with Valgrind` fails without noticeable code changes, seemingly caused by `fatal runtime error: failed to initiate panic, error 3` #1327

Closed fzyzcjy closed 6 months ago

fzyzcjy commented 9 months ago

Context

As is reported in https://github.com/fzyzcjy/flutter_rust_bridge/pull/1325#issuecomment-1684886886, https://github.com/fzyzcjy/flutter_rust_bridge/actions/runs/5868452973/job/15911323953 works while https://github.com/fzyzcjy/flutter_rust_bridge/actions/runs/5909307658/job/16029737370 breaks. The code change is completely unrelated to it.

This blocks

from merging, because both PRs are large and may really introduce memory-related issues, thus we should not merge without a green Valgrind CI - otherwise if is really has memory problems, it will be quite subtle and very hard to debug or fix in production.

fzyzcjy commented 9 months ago

I briefly looked at it, seems that the error is caused by fatal runtime error: failed to initiate panic, error 3 with:

2023-08-19T03:58:25.7349373Z ==5084== Process terminating with default action of signal 6 (SIGABRT)
2023-08-19T03:58:25.7349798Z ==5084==    at 0x49EEA7C: __pthread_kill_implementation (pthread_kill.c:44)
2023-08-19T03:58:25.7350214Z ==5084==    by 0x49EEA7C: __pthread_kill_internal (pthread_kill.c:78)
2023-08-19T03:58:25.7350867Z ==5084==    by 0x49EEA7C: pthread_kill@@GLIBC_2.34 (pthread_kill.c:89)
2023-08-19T03:58:25.7351202Z ==5084==    by 0x499A475: raise (raise.c:26)
2023-08-19T03:58:25.7351779Z ==5084==    by 0x49807F2: abort (abort.c:79)
2023-08-19T03:58:25.7352129Z ==5084==    by 0x5F4BFC6: std::sys::unix::abort_internal (mod.rs:350)
2023-08-19T03:58:25.7352483Z ==5084==    by 0x5F4887C: rust_panic (panicking.rs:742)
2023-08-19T03:58:25.7352879Z ==5084==    by 0x5F48711: std::panicking::rust_panic_with_hook (panicking.rs:713)
2023-08-19T03:58:25.7353318Z ==5084==    by 0x5E28809: std::panicking::begin_panic::{{closure}} (panicking.rs:611)
2023-08-19T03:58:25.7353777Z ==5084==    by 0x5E27DCC: std::sys_common::backtrace::__rust_end_short_backtrace (backtrace.rs:150)
2023-08-19T03:58:25.7354219Z ==5084==    by 0x5E28746: std::panicking::begin_panic (panicking.rs:610)
2023-08-19T03:58:25.7354668Z ==5084==    by 0x59E28C0: flutter_rust_bridge_example_pure_dart::api::handle_sync_return (api.rs:345)
2023-08-19T03:58:25.7355239Z ==5084==    by 0x5A2E947: flutter_rust_bridge_example_pure_dart::bridge_generated::wire_handle_sync_return_impl::{{closure}} (bridge_generated.rs:502)
2023-08-19T03:58:25.7355866Z ==5084==    by 0x5A82E57: <flutter_rust_bridge::handler::ThreadPoolExecutor<EH> as flutter_rust_bridge::handler::Executor>::execute_sync (handler.rs:249)

then since it fails to panic, it aborts (stop the whole process).

related source code: https://github.com/rust-lang/rust/blob/5e9d3d8a03fadf1877286528e5c9d0c056ac82b5/library/std/src/panicking.rs#L780

good: ``` ==5072== HEAP SUMMARY: ==5072== in use at exit: 21,217 bytes in 208 blocks ==5072== total heap usage: 47,410 allocs, 47,202 frees, 8,477,757 bytes allocated ==5072== ==5072== 8 bytes in 1 blocks are definitely lost in loss record 2 of 196 ==5072== at 0x484A2F3: operator new[](unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==5072== by 0x3192AC: MallocExtension::Register(MallocExtension*) (in /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe) ==5072== by 0x314268: ??? (in /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe) ==5072== by 0x59025C: __libc_csu_init (in /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe) ==5072== by 0x4981E1B: __libc_start_main@@GLIBC_2.34 (libc-start.c:375) ==5072== by 0x2E37A9: (below main) (in /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe) ==5072== ==5072== 608 bytes in 2 blocks are possibly lost in loss record 187 of 196 ==5072== at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==5072== by 0x[4014](https://github.com/fzyzcjy/flutter_rust_bridge/actions/runs/5868452973/job/15911323953#step:7:4015)7D9: calloc (rtld-malloc.h:44) ==5072== by 0x40147D9: allocate_dtv (dl-tls.c:375) ==5072== by 0x40147D9: _dl_allocate_tls (dl-tls.c:634) ==5072== by 0x49ED834: allocate_stack (allocatestack.c:430) ==5072== by 0x49ED834: pthread_create@@GLIBC_2.34 (pthread_create.c:647) ==5072== by 0x5F4A8CE: std::sys::unix::thread::Thread::new (thread.rs:87) ==5072== by 0x5DBB585: std::thread::Builder::spawn_unchecked_ (mod.rs:560) ==5072== by 0x5DBAE77: std::thread::Builder::spawn_unchecked (mod.rs:454) ==5072== by 0x5DBBC26: std::thread::Builder::spawn (mod.rs:386) ==5072== by 0x5DC2E88: threadpool::spawn_in_pool (lib.rs:738) ==5072== by 0x5DC2947: threadpool::Builder::build (lib.rs:301) ==5072== by 0x5DC2BAF: threadpool::ThreadPool::with_name (lib.rs:398) ==5072== by 0x5DAF841: core::ops::function::FnOnce::call_once (thread.rs:42) ==5072== by 0x5DAC72F: lazy_static::lazy::Lazy::get::{{closure}} (inline_lazy.rs:31) ==5072== ==5072== LEAK SUMMARY: ==5072== definitely lost: 8 bytes in 1 blocks ==5072== indirectly lost: 0 bytes in 0 blocks ==5072== possibly lost: 608 bytes in 2 blocks ==5072== still reachable: 20,601 bytes in 205 blocks ==5072== suppressed: 0 bytes in 0 blocks ==5072== Reachable blocks (those to which a pointer was found) are not shown. ==5072== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==5072== ==5072== For lists of detected and suppressed errors, rerun with: -s ==5072== ERROR SUMMARY: 3463 errors from 122 contexts (suppressed: 0 from 0) process code= 1 valgrind_util thinks it is ok ``` bad: ``` ==5084== LEAK SUMMARY: ==5084== definitely lost: 104 bytes in 2 blocks ==5084== indirectly lost: 16 bytes in 1 blocks ==5084== possibly lost: 4,704 bytes in 9 blocks ==5084== still reachable: 1,192,392 bytes in 600 blocks ==5084== of which reachable via heuristic: ==5084== newarray : 6,784 bytes in 2 blocks ==5084== suppressed: 0 bytes in 0 blocks ==5084== Reachable blocks (those to which a pointer was found) are not shown. ==5084== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==5084== ==5084== Use --track-origins=yes to see where uninitialised values come from ==5084== For lists of detected and suppressed errors, rerun with: -s ==5084== ERROR SUMMARY: 29 errors from 11 contexts (suppressed: 0 from 0) process code= -6 error: valgrind_util does not find "All tests passed!", thus dart test seems failed error: Recipe `dart_test_valgrind` failed on line 82 with exit code 1 error: Recipe `ci_valgrind` failed on line 190 with exit code 1 Error: Process completed with exit code 1. ``` Direct error: ``` error: valgrind_util does not find "All tests passed!", thus dart test seems failed ``` old: ``` 2023-08-15T14:57:17.7232988Z 00:07 +31: dart call handle_sync_return 2023-08-15T14:57:17.7233732Z 2023-08-15T14:57:17.7986947Z dart catch e: FfiException(EXECUTE_SYNC, RESULT_ERROR: deliberate error in handle_sync_return_err, null) 2023-08-15T14:57:17.7992038Z 2023-08-15T14:57:17.8039530Z thread '' panicked at 'deliberate panic in handle_sync_return_panic', frb_example/pure_dart/rust/src/api.rs:345:20 2023-08-15T14:57:17.8043521Z note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace 2023-08-15T14:57:17.8454183Z dart catch e: FfiException(EXECUTE_SYNC, PANIC_ERROR: deliberate panic in handle_sync_return_panic, null) 2023-08-15T14:57:17.8454928Z 2023-08-15T14:57:17.8463149Z 00:07 +32: dart call handle_stream 2023-08-15T14:57:17.8464307Z 2023-08-15T14:57:17.8703709Z [INFO flutter_rust_bridge_example_pure_dart::api] handle_stream arg=hello 2023-08-15T14:57:17.8995932Z output from handle_stream's stream: (thread=normal, i=0, old_cnt=0) 2023-08-15T14:57:17.8996476Z 2023-08-15T14:57:17.9062237Z output from handle_stream's stream: (thread=child, i=0, old_cnt=1) 2023-08-15T14:57:17.9062646Z 2023-08-15T14:57:17.9391177Z output from handle_stream's stream: (thread=normal, i=1, old_cnt=2) 2023-08-15T14:57:17.9392167Z 2023-08-15T14:57:17.9896902Z output from handle_stream's stream: (thread=normal, i=2, old_cnt=3) 2023-08-15T14:57:17.9897344Z 2023-08-15T14:57:17.9955376Z output from handle_stream's stream: (thread=child, i=1, old_cnt=4) 2023-08-15T14:57:17.9955797Z 2023-08-15T14:57:18.0404119Z output from handle_stream's stream: (thread=normal, i=3, old_cnt=5) 2023-08-15T14:57:18.0404536Z 2023-08-15T14:57:18.0911014Z output from handle_stream's stream: (thread=normal, i=4, old_cnt=6) 2023-08-15T14:57:18.0911420Z 2023-08-15T14:57:18.0959777Z output from handle_stream's stream: (thread=child, i=2, old_cnt=7) 2023-08-15T14:57:18.0960171Z 2023-08-15T14:57:18.1968420Z output from handle_stream's stream: (thread=child, i=3, old_cnt=8) 2023-08-15T14:57:18.1969438Z 2023-08-15T14:57:18.2975576Z output from handle_stream's stream: (thread=child, i=4, old_cnt=9) 2023-08-15T14:57:18.2976011Z 2023-08-15T14:57:18.4189340Z 00:07 +33: dart call handle_stream_sink_at_1 2023-08-15T14:57:18.4190046Z 2023-08-15T14:57:18.4297002Z 00:07 +34: dart call handle_stream_sink_at_2 ... 2023-08-15T14:57:27.7292896Z 00:17 +142: sync return mirror 2023-08-15T14:57:27.7293416Z 2023-08-15T14:57:27.7387403Z 00:17 +143: macro struct 2023-08-15T14:57:27.7387776Z 2023-08-15T14:57:27.7571877Z 00:17 +144: (tearDownAll) 2023-08-15T14:57:27.7572403Z 2023-08-15T14:57:27.7926893Z 00:17 +144: All tests passed! ``` new: ``` value_vec_u8: [110, 110, 110, 110, 110], value_boolean: true, children: [MyTreeNode { value_i32: 111, value_vec_u8: [111, 111, 111, 111, 111], value_boolean: true, children: [] }] }, MyTreeNode { value_i32: 120, value_vec_u8: [120, 120, 120, 120, 120], value_boolean: true, children: [] }] }, weekday: Friday }) 2023-08-19T03:58:25.6089046Z 00:07 +30: dart call handleComplexStructSync 2023-08-19T03:58:25.6089605Z 2023-08-19T03:58:25.6165262Z [INFO flutter_rust_bridge_example_pure_dart::api] handle_complex_struct_sync(MyTreeNode { value_i32: 100, value_vec_u8: [100, 100, 100, 100, 100], value_boolean: true, children: [MyTreeNode { value_i32: 110, value_vec_u8: [110, 110, 110, 110, 110], value_boolean: true, children: [MyTreeNode { value_i32: 111, value_vec_u8: [111, 111, 111, 111, 111], value_boolean: true, children: [] }] }, MyTreeNode { value_i32: 120, value_vec_u8: [120, 120, 120, 120, 120], value_boolean: true, children: [] }] }) 2023-08-19T03:58:25.6248863Z 00:07 +31: dart call handle_sync_return 2023-08-19T03:58:25.6249417Z 2023-08-19T03:58:25.6996964Z dart catch e: FfiException(EXECUTE_SYNC, RESULT_ERROR: deliberate error in handle_sync_return_err, null) 2023-08-19T03:58:25.6997718Z 2023-08-19T03:58:25.7048692Z thread '' panicked at 'deliberate panic in handle_sync_return_panic', frb_example/pure_dart/rust/src/api.rs:345:20 2023-08-19T03:58:25.7053317Z note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace 2023-08-19T03:58:25.7321319Z ==5084== Thread 3 DartWorker: 2023-08-19T03:58:25.7322215Z ==5084== Conditional jump or move depends on uninitialised value(s) 2023-08-19T03:58:25.7323090Z ==5084== at 0x5F4C6CC: rust_eh_personality (eh.rs:90) 2023-08-19T03:58:25.7323811Z ==5084== by 0x543B65: _Unwind_RaiseException (in /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe) 2023-08-19T03:58:25.7324710Z ==5084== by 0x5F487CC: rust_panic (panicking.rs:741) 2023-08-19T03:58:25.7325347Z ==5084== by 0x5F48711: std::panicking::rust_panic_with_hook (panicking.rs:713) 2023-08-19T03:58:25.7326166Z ==5084== by 0x5E28809: std::panicking::begin_panic::{{closure}} (panicking.rs:611) 2023-08-19T03:58:25.7326812Z ==5084== by 0x5E27DCC: std::sys_common::backtrace::__rust_end_short_backtrace (backtrace.rs:150) 2023-08-19T03:58:25.7327644Z ==5084== by 0x5E28746: std::panicking::begin_panic (panicking.rs:610) 2023-08-19T03:58:25.7328290Z ==5084== by 0x59E28C0: flutter_rust_bridge_example_pure_dart::api::handle_sync_return (api.rs:345) 2023-08-19T03:58:25.7329823Z ==5084== by 0x5A2E947: flutter_rust_bridge_example_pure_dart::bridge_generated::wire_handle_sync_return_impl::{{closure}} (bridge_generated.rs:502) 2023-08-19T03:58:25.7330692Z ==5084== by 0x5A82E57: as flutter_rust_bridge::handler::Executor>::execute_sync (handler.rs:249) 2023-08-19T03:58:25.7331789Z ==5084== by 0x5A80844: as flutter_rust_bridge::handler::Handler>::wrap_sync::{{closure}}::{{closure}} (handler.rs:138) 2023-08-19T03:58:25.7332505Z ==5084== by 0x59A767C: std::panicking::try::do_call (panicking.rs:485) 2023-08-19T03:58:25.7333199Z ==5084== 2023-08-19T03:58:25.7333781Z ==5084== Conditional jump or move depends on uninitialised value(s) 2023-08-19T03:58:25.7334559Z ==5084== at 0x5F4C6DA: rust_eh_personality (eh.rs:93) 2023-08-19T03:58:25.7335247Z ==5084== by 0x543B65: _Unwind_RaiseException (in /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe) 2023-08-19T03:58:25.7336116Z ==5084== by 0x5F487CC: rust_panic (panicking.rs:741) 2023-08-19T03:58:25.7336681Z ==5084== by 0x5F48711: std::panicking::rust_panic_with_hook (panicking.rs:713) 2023-08-19T03:58:25.7337507Z ==5084== by 0x5E28809: std::panicking::begin_panic::{{closure}} (panicking.rs:611) 2023-08-19T03:58:25.7338148Z ==5084== by 0x5E27DCC: std::sys_common::backtrace::__rust_end_short_backtrace (backtrace.rs:150) 2023-08-19T03:58:25.7339483Z ==5084== by 0x5E28746: std::panicking::begin_panic (panicking.rs:610) 2023-08-19T03:58:25.7340108Z ==5084== by 0x59E28C0: flutter_rust_bridge_example_pure_dart::api::handle_sync_return (api.rs:345) 2023-08-19T03:58:25.7341073Z ==5084== by 0x5A2E947: flutter_rust_bridge_example_pure_dart::bridge_generated::wire_handle_sync_return_impl::{{closure}} (bridge_generated.rs:502) 2023-08-19T03:58:25.7343311Z ==5084== by 0x5A82E57: as flutter_rust_bridge::handler::Executor>::execute_sync (handler.rs:249) 2023-08-19T03:58:25.7346025Z ==5084== by 0x5A80844: as flutter_rust_bridge::handler::Handler>::wrap_sync::{{closure}}::{{closure}} (handler.rs:138) 2023-08-19T03:58:25.7348160Z ==5084== by 0x59A767C: std::panicking::try::do_call (panicking.rs:485) 2023-08-19T03:58:25.7348456Z ==5084== 2023-08-19T03:58:25.7348760Z fatal runtime error: failed to initiate panic, error 3 2023-08-19T03:58:25.7349069Z ==5084== 2023-08-19T03:58:25.7349373Z ==5084== Process terminating with default action of signal 6 (SIGABRT) 2023-08-19T03:58:25.7349798Z ==5084== at 0x49EEA7C: __pthread_kill_implementation (pthread_kill.c:44) 2023-08-19T03:58:25.7350214Z ==5084== by 0x49EEA7C: __pthread_kill_internal (pthread_kill.c:78) 2023-08-19T03:58:25.7350867Z ==5084== by 0x49EEA7C: pthread_kill@@GLIBC_2.34 (pthread_kill.c:89) 2023-08-19T03:58:25.7351202Z ==5084== by 0x499A475: raise (raise.c:26) 2023-08-19T03:58:25.7351779Z ==5084== by 0x49807F2: abort (abort.c:79) 2023-08-19T03:58:25.7352129Z ==5084== by 0x5F4BFC6: std::sys::unix::abort_internal (mod.rs:350) 2023-08-19T03:58:25.7352483Z ==5084== by 0x5F4887C: rust_panic (panicking.rs:742) 2023-08-19T03:58:25.7352879Z ==5084== by 0x5F48711: std::panicking::rust_panic_with_hook (panicking.rs:713) 2023-08-19T03:58:25.7353318Z ==5084== by 0x5E28809: std::panicking::begin_panic::{{closure}} (panicking.rs:611) 2023-08-19T03:58:25.7353777Z ==5084== by 0x5E27DCC: std::sys_common::backtrace::__rust_end_short_backtrace (backtrace.rs:150) 2023-08-19T03:58:25.7354219Z ==5084== by 0x5E28746: std::panicking::begin_panic (panicking.rs:610) 2023-08-19T03:58:25.7354668Z ==5084== by 0x59E28C0: flutter_rust_bridge_example_pure_dart::api::handle_sync_return (api.rs:345) 2023-08-19T03:58:25.7355239Z ==5084== by 0x5A2E947: flutter_rust_bridge_example_pure_dart::bridge_generated::wire_handle_sync_return_impl::{{closure}} (bridge_generated.rs:502) 2023-08-19T03:58:25.7355866Z ==5084== by 0x5A82E57: as flutter_rust_bridge::handler::Executor>::execute_sync (handler.rs:249) 2023-08-19T03:58:25.7356284Z ==5084== 2023-08-19T03:58:25.7356516Z ==5084== HEAP SUMMARY: 2023-08-19T03:58:25.7356805Z ==5084== in use at exit: 1,197,216 bytes in 612 blocks 2023-08-19T03:58:25.7357154Z ==5084== total heap usage: 2,170 allocs, 1,558 frees, 4,455,531 bytes allocated ``` thus, looks like this problem: ``` fatal runtime error: failed to initiate panic, error 3 2023-08-19T03:58:25.7349373Z ==5084== Process terminating with default action of signal 6 (SIGABRT) 2023-08-19T03:58:25.7349798Z ==5084== at 0x49EEA7C: __pthread_kill_implementation (pthread_kill.c:44) 2023-08-19T03:58:25.7350214Z ==5084== by 0x49EEA7C: __pthread_kill_internal (pthread_kill.c:78) 2023-08-19T03:58:25.7350867Z ==5084== by 0x49EEA7C: pthread_kill@@GLIBC_2.34 (pthread_kill.c:89) 2023-08-19T03:58:25.7351202Z ==5084== by 0x499A475: raise (raise.c:26) 2023-08-19T03:58:25.7351779Z ==5084== by 0x49807F2: abort (abort.c:79) 2023-08-19T03:58:25.7352129Z ==5084== by 0x5F4BFC6: std::sys::unix::abort_internal (mod.rs:350) 2023-08-19T03:58:25.7352483Z ==5084== by 0x5F4887C: rust_panic (panicking.rs:742) 2023-08-19T03:58:25.7352879Z ==5084== by 0x5F48711: std::panicking::rust_panic_with_hook (panicking.rs:713) 2023-08-19T03:58:25.7353318Z ==5084== by 0x5E28809: std::panicking::begin_panic::{{closure}} (panicking.rs:611) 2023-08-19T03:58:25.7353777Z ==5084== by 0x5E27DCC: std::sys_common::backtrace::__rust_end_short_backtrace (backtrace.rs:150) 2023-08-19T03:58:25.7354219Z ==5084== by 0x5E28746: std::panicking::begin_panic (panicking.rs:610) 2023-08-19T03:58:25.7354668Z ==5084== by 0x59E28C0: flutter_rust_bridge_example_pure_dart::api::handle_sync_return (api.rs:345) 2023-08-19T03:58:25.7355239Z ==5084== by 0x5A2E947: flutter_rust_bridge_example_pure_dart::bridge_generated::wire_handle_sync_return_impl::{{closure}} (bridge_generated.rs:502) 2023-08-19T03:58:25.7355866Z ==5084== by 0x5A82E57: as flutter_rust_bridge::handler::Executor>::execute_sync (handler.rs:249) ``` look at versions: old ``` Generated: /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe + PYTHONUNBUFFERED=1 + ./valgrind_util.py ./main.exe /home/runner/debug/libflutter_rust_bridge_example_pure_dart.so --chain-stack-traces valgrind_util.py start run command: valgrind --error-exitcode=1 --leak-check=full --trace-children=yes --ignore-ranges=0x000-0xFFF --vex-iropt-level=1 ./main.exe /home/runner/debug/libflutter_rust_bridge_example_pure_dart.so --chain-stack-traces ==5072== Memcheck, a memory error detector ==5072== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==5072== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info ==5072== Command: ./main.exe /home/runner/debug/libflutter_rust_bridge_example_pure_dart.so --chain-stack-traces ==5072== ``` new ``` 2023-08-19T03:58:15.6705389Z Generated: /home/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe 2023-08-19T03:58:15.6987273Z + PYTHONUNBUFFERED=1 2023-08-19T03:58:15.6988123Z + ./valgrind_util.py ./main.exe /home/runner/debug/libflutter_rust_bridge_example_pure_dart.so --chain-stack-traces 2023-08-19T03:58:15.7527961Z valgrind_util.py start 2023-08-19T03:58:15.7529613Z run command: valgrind --error-exitcode=1 --leak-check=full --trace-children=yes --ignore-ranges=0x000-0xFFF --vex-iropt-level=1 ./main.exe /home/runner/debug/libflutter_rust_bridge_example_pure_dart.so --chain-stack-traces 2023-08-19T03:58:15.7647676Z ==5084== Memcheck, a memory error detector 2023-08-19T03:58:15.7648867Z ==5084== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. 2023-08-19T03:58:15.7698340Z ==5084== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info 2023-08-19T03:58:15.7699355Z ==5084== Command: ./main.exe /home/runner/debug/libflutter_rust_bridge_example_pure_dart.so --chain-stack-traces 2023-08-19T03:58:15.7699698Z ==5084== ``` same version but may have diff version in other things
JustSimplyKyle commented 9 months ago
7c7
< Get:7 http://azure.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [894 kB]
---
> Get:7 http://azure.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [889 kB]
10,16c10,16
< Get:10 http://azure.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [714 kB]
< Get:11 http://azure.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [114 kB]
< Get:12 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [969 kB]
< Get:13 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [211 kB]
< Get:14 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [21.5 kB]
< Get:15 http://azure.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [41.6 kB]
< Get:16 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
---
> Get:10 http://azure.archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [710 kB]
> Get:11 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
> Get:12 http://azure.archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [113 kB]
> Get:13 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [965 kB]
> Get:14 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [210 kB]
> Get:15 http://azure.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [21.5 kB]
> Get:16 http://azure.archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [41.6 kB]
18,20c18,20
< Hit:18 https://ppa.launchpadcontent.net/ubuntu-toolchain-r/test/ubuntu jammy InRelease
< Get:19 http://azure.archive.ubuntu.com/ubuntu jammy-security/main amd64 Packages [680 kB]
< Get:20 http://azure.archive.ubuntu.com/ubuntu jammy-security/main Translation-en [155 kB]
---
> Get:18 http://azure.archive.ubuntu.com/ubuntu jammy-security/main amd64 Packages [657 kB]
> Hit:19 https://ppa.launchpadcontent.net/ubuntu-toolchain-r/test/ubuntu jammy InRelease
> Get:20 http://azure.archive.ubuntu.com/ubuntu jammy-security/main Translation-en [153 kB]
22,23c22,23
< Get:22 http://azure.archive.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [694 kB]
< Get:23 http://azure.archive.ubuntu.com/ubuntu jammy-security/restricted Translation-en [110 kB]
---
> Get:22 http://azure.archive.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [662 kB]
> Get:23 http://azure.archive.ubuntu.com/ubuntu jammy-security/restricted Translation-en [105 kB]
28,30c28,30
< Get:28 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main armhf Packages [9004 B]
< Get:29 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 Packages [87.2 kB]
< Get:30 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main arm64 Packages [18.1 kB]
---
> Get:28 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 Packages [85.7 kB]
> Get:29 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main arm64 Packages [17.9 kB]
> Get:30 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main armhf Packages [8877 B]
36,38c36,38
< Get:36 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [680 kB]
< Get:37 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 c-n-f Metadata [488 B]
< Get:38 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [894 kB]
---
> Get:36 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 c-n-f Metadata [488 B]
> Get:37 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [657 kB]
> Get:38 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [889 kB]
41,42c41,42
< Get:41 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [714 kB]
< Get:42 http://archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [114 kB]
---
> Get:41 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [710 kB]
> Get:42 http://archive.ubuntu.com/ubuntu jammy-updates/restricted Translation-en [113 kB]
44c44
< Get:44 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [155 kB]
---
> Get:44 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [153 kB]
46,47c46,47
< Get:46 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [694 kB]
< Get:47 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [110 kB]
---
> Get:46 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [662 kB]
> Get:47 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [105 kB]

the diff file of the apt packages. doesn't seem like it tho. currently trying to find more possible causes dart last update -> May 10 python -> 2022/10/24 rust -> a set toolchain valgrind -> april 2023

fzyzcjy commented 9 months ago

(btw I also suspect it is GitHub CI machine update that causes this problem)

JustSimplyKyle commented 9 months ago

https://github.com/actions/runner-images/commits/main/images/linux/Ubuntu2204-Readme.md The failing of the test happened three days ago, so not that either Maybe it has to do with the dart and rust deps, will look into it more

JustSimplyKyle commented 9 months ago

image just throwing ideas(left is old, right is new)

fzyzcjy commented 9 months ago

Another way is to make a minimal reproducible sample and create a bug on the related package. For example, maybe we can write a minimal rust program that panics, and check whether it still fails like this ("failed to initiate panic") under valgrind. If so, we can create an issue at Valgrind, and (maybe) another issue at Rust.

SiongSng commented 9 months ago

After a series of tests, I found that the issues seems to have nothing to do with Rust, and a simple Dart hello world will also fail.

https://github.com/dart-lang/sdk/issues/47346

image image

fzyzcjy commented 9 months ago

Yes,

Thus I have allowed those "mismatched free" to happen (i.e. ci will still be green).

The main problem here is that, after we ignore those "mismatched free" things, we still have "fatal runtime error: failed to initiate panic, error 3".

SiongSng commented 9 months ago

Only testing returnPanic doesn't print this error message fatal runtime error: failed to initiate panic, error 3.

00:00 +0: dart call returnPanic

thread 'frb_workerpool' panicked at 'return_panic() is called, thus deliberately panic', frb_example/pure_dart/rust/src/api.rs:396:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
dart catch e: Instance of 'PanicException'

==1065== Mismatched free() / delete / delete []
==1065==    at 0x484412F: free (vg_replace_malloc.c:974)
==1065==    by 0x48475A: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x484E7D: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x485391: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x519B88: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x519E52: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x4E228F: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x49D29EA: start_thread (pthread_create.c:444)
==1065==    by 0x4A56C83: clone (clone.S:100)
==1065==  Address 0x4c20dc0 is 0 bytes inside a block of size 56 alloc'd
==1065==    at 0x4843223: operator new[](unsigned long) (vg_replace_malloc.c:714)
==1065==    by 0x485A69: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x4882AA: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x5B7EB9: ??? (in /mnt/d/Coding/Github/flutter_rust_bridge/frb_example/pure_dart/dart/main.exe)
==1065==    by 0x5989FB7: allo_isolate::Isolate::post (lib.rs:139)
==1065==    by 0x5990783: flutter_rust_bridge::rust2dart::Rust2Dart::panic (rust2dart.rs:41)
==1065==    by 0x598CBA3: <flutter_rust_bridge::handler::ReportDartErrorHandler as flutter_rust_bridge::handler::ErrorHandler>::handle_error (handler.rs:319)
==1065==    by 0x55AD418: <flutter_rust_bridge::handler::ThreadPoolExecutor<EH> as flutter_rust_bridge::handler::Executor>::execute::{{closure}} (handler.rs:240)
==1065==    by 0x55E929E: <F as threadpool::FnBox>::call_box (lib.rs:95)
==1065==    by 0x59A2450: threadpool::spawn_in_pool::{{closure}} (lib.rs:769)
==1065==    by 0x599D06D: std::sys_common::backtrace::__rust_begin_short_backtrace (backtrace.rs:135)
==1065==    by 0x5997A7A: std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}} (mod.rs:529)
==1065==
00:00 +1: (tearDownAll)

00:00 +1: All tests passed!
fzyzcjy commented 9 months ago

Interesting! Then we may be able to bisect (or whatever) to reduce the testing

(btw, does the full test fail on your local machine?)

SiongSng commented 9 months ago

The unit tests are successful on my local machine.

fzyzcjy commented 9 months ago

Hmm then this looks like a github ci - only issue, at least maybe dependent on the OS version etc?

JustSimplyKyle commented 9 months ago

The errors occurs on my arch linux machine as well

JustSimplyKyle commented 9 months ago

Update: (at least on my machine) it seems like it's not an valgrind error running cargo test in frb_codegen is enough to cause the error image

so maybe the error originates is about panicing in FFI boundaries are UB?(should be handled in frb_rust/src/handle.rs)

fzyzcjy commented 9 months ago

Looks great!

panicing in FFI boundaries are UB

IIRC we never panic across ffi boundaries (except for bugs). Instead, we have error handlers catching panic.

fzyzcjy commented 9 months ago

see:

https://github.com/fzyzcjy/flutter_rust_bridge/blob/e248cbc2df2328043843a12d61fc551b004fb9a4/frb_rust/src/handler.rs#L104-L114

(I even wrote double catch_unwind to be extra safe, see comments there)

fzyzcjy commented 9 months ago

@JustSimplyKyle Btw, what is your full logs? We should be sure it is the "failed to initiate panic" error instead of any other errors, otherwise we may be risk working on a non-goal. From the screenshot I am not very sure yet.

JustSimplyKyle commented 9 months ago

Sure! fulllog.log

fzyzcjy commented 9 months ago

(copy-pasted the last parts for the convenience of future readers)

``` Size { width: 42, height: 100 }, MySize { width: 420, height: 1000 }]) 00:00 +25: dart call handleListOfStructSync [INFO flutter_rust_bridge_example_pure_dart::api] handle_list_of_struct_sync([MySize { width: 42, height: 100 }, MySize { width: 420, height: 1000 }]) 00:00 +26: dart call handleStringList [INFO flutter_rust_bridge_example_pure_dart::api] Hello, Steve [INFO flutter_rust_bridge_example_pure_dart::api] Hello, Bob [INFO flutter_rust_bridge_example_pure_dart::api] Hello, Alex 00:00 +27: dart call handleStringListSync [INFO flutter_rust_bridge_example_pure_dart::api] Hello, Steve [INFO flutter_rust_bridge_example_pure_dart::api] Hello, Bob [INFO flutter_rust_bridge_example_pure_dart::api] Hello, Alex 00:00 +28: dart call handleComplexStruct [INFO flutter_rust_bridge_example_pure_dart::api] handle_complex_struct(MyTreeNode { value_i32: 100, value_vec_u8: [100, 100, 100, 100, 100], value_boolean: true, children: [MyTreeNode { value_i32: 110, value_vec_u8: [110, 110, 110, 110, 110], value_boolean: true, children: [MyTreeNode { value_i32: 111, value_vec_u8: [111, 111, 111, 111, 111], value_boolean: true, children: [] }] }, MyTreeNode { value_i32: 120, value_vec_u8: [120, 120, 120, 120, 120], value_boolean: true, children: [] }] }) 00:00 +29: dart call handleNestedStruct handle_nested_struct(MyNestedStruct { tree_node: MyTreeNode { value_i32: 100, value_vec_u8: [100, 100, 100, 100, 100], value_boolean: true, children: [MyTreeNode { value_i32: 110, value_vec_u8: [110, 110, 110, 110, 110], value_boolean: true, children: [MyTreeNode { value_i32: 111, value_vec_u8: [111, 111, 111, 111, 111], value_boolean: true, children: [] }] }, MyTreeNode { value_i32: 120, value_vec_u8: [120, 120, 120, 120, 120], value_boolean: true, children: [] }] }, weekday: Friday }) 00:00 +30: dart call handleComplexStructSync [INFO flutter_rust_bridge_example_pure_dart::api] handle_complex_struct_sync(MyTreeNode { value_i32: 100, value_vec_u8: [100, 100, 100, 100, 100], value_boolean: true, children: [MyTreeNode { value_i32: 110, value_vec_u8: [110, 110, 110, 110, 110], value_boolean: true, children: [MyTreeNode { value_i32: 111, value_vec_u8: [111, 111, 111, 111, 111], value_boolean: true, children: [] }] }, MyTreeNode { value_i32: 120, value_vec_u8: [120, 120, 120, 120, 120], value_boolean: true, children: [] }] }) 00:00 +31: dart call handle_sync_return ===== CRASH ===== si_signo=Segmentation fault(11), si_code=1, si_addr=0x10 version=3.1.0 (stable) (Tue Aug 15 21:33:36 2023 +0000) on "linux_x64" pid=1584299, thread=1584881, isolate_group=main(0x55a4fb586390), isolate=main(0x55a4fb588bf0) os=linux, arch=x64, comp=no, sim=no isolate_instructions=55a4fac054e0, vm_instructions=55a4fac054e0 fp=ffffffff, sp=7f769a17c9c0, pc=55a4fb2d6472 Stack dump aborted because GetAndValidateThreadStackBounds failed. pc 0x000055a4fb2d6472 fp 0x00000000ffffffff unw_get_reg+0x72 2023/08/30 17:54:31 [ERROR] panicked at 'pure_dart failed', frb_codegen/src/main.rs:149:9 test tests::pure_dart ... FAILED failures: ---- tests::pure_dart stdout ---- the path is "/home/kyle/things/github/flutter_rust_bridge/frb_codegen/../frb_example/pure_dart/rust/c_output_path/c_output.h" the path is "/home/kyle/things/github/flutter_rust_bridge/frb_codegen/../frb_example/pure_dart/rust/c_output_path_extra/c_output.h" Absolute path to output: "/home/kyle/things/github/flutter_rust_bridge/target/debug/libflutter_rust_bridge_example_pure_dart.so" Output file exists thread 'tests::pure_dart' panicked at 'pure_dart failed', frb_codegen/src/main.rs:149:9 stack backtrace: 0: 0x55abbe5ae291 - std::backtrace_rs::backtrace::libunwind::trace::h6aeaf83abc038fe6 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x55abbe5ae291 - std::backtrace_rs::backtrace::trace_unsynchronized::h4f9875212db0ad97 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x55abbe5ae291 - std::sys_common::backtrace::_print_fmt::h3f820027e9c39d3b at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:65:5 3: 0x55abbe5ae291 - ::fmt::hded4932df41373b3 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:44:22 4: 0x55abbe5d836f - core::fmt::rt::Argument::fmt::hc8ead7746b2406d6 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/fmt/rt.rs:138:9 5: 0x55abbe5d836f - core::fmt::write::hb1cb56105a082ad9 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/fmt/mod.rs:1094:21 6: 0x55abbe5aabd1 - std::io::Write::write_fmt::hd97a6689e2c41217 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/io/mod.rs:1713:15 7: 0x55abbe5ae0a5 - std::sys_common::backtrace::_print::h492d3c92d7400346 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:47:5 8: 0x55abbe5ae0a5 - std::sys_common::backtrace::print::hf74aa2eef05af215 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:34:9 9: 0x55abbe5afc97 - std::panicking::default_hook::{{closure}}::h8cad394227ea3de8 10: 0x55abbe5afa10 - std::panicking::default_hook::h249cc184fec99a8a at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:285:9 11: 0x55abbd983944 - as core::ops::function::Fn>::call::h55b77d8758f50ca3 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/alloc/src/boxed.rs:1999:9 12: 0x55abbd983944 - test::test_main::{{closure}}::h2c3e62235fd05a97 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/test/src/lib.rs:134:21 13: 0x55abbda20b13 - as core::ops::function::Fn>::call::h25bd7851ce2a7dbe at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/alloc/src/boxed.rs:1999:9 14: 0x55abbda1f83d - lib_flutter_rust_bridge_codegen::logs::init_logger::{{closure}}::h6f55735f048f68df at /home/kyle/things/github/flutter_rust_bridge/frb_codegen/src/logs.rs:66:9 15: 0x55abbe5b02ad - as core::ops::function::Fn>::call::h0be7fc2421582b49 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/alloc/src/boxed.rs:1999:9 16: 0x55abbe5b02ad - std::panicking::rust_panic_with_hook::h82ebcd5d5ed2fad4 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:709:13 17: 0x55abbe545974 - std::panicking::begin_panic::{{closure}}::hde4cc46429e7fec6 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:626:9 18: 0x55abbe54589d - std::sys_common::backtrace::__rust_end_short_backtrace::h2923fae0ab015206 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:151:18 19: 0x55abbe5458e7 - std::panicking::begin_panic::h442a5ef17584d808 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:625:12 20: 0x55abbd94cdab - flutter_rust_bridge_codegen::tests::pure_dart::hb2854a8755013285 at /home/kyle/things/github/flutter_rust_bridge/frb_codegen/src/main.rs:149:9 21: 0x55abbd94bf77 - flutter_rust_bridge_codegen::tests::pure_dart::{{closure}}::h391d6fef01c80fc3 at /home/kyle/things/github/flutter_rust_bridge/frb_codegen/src/main.rs:92:20 22: 0x55abbd94b865 - core::ops::function::FnOnce::call_once::h8098241c0feff840 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/ops/function.rs:250:5 23: 0x55abbd988dff - core::ops::function::FnOnce::call_once::hb75e2f7a6955c2a2 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/ops/function.rs:250:5 24: 0x55abbd988dff - test::__rust_begin_short_backtrace::h95acc83b06096772 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/test/src/lib.rs:655:18 25: 0x55abbd954bfc - test::run_test::{{closure}}::hd4f468284892ca14 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/test/src/lib.rs:646:30 26: 0x55abbd954bfc - core::ops::function::FnOnce::call_once{{vtable.shim}}::h5a9aa91b38c63678 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/ops/function.rs:250:5 27: 0x55abbd987ca1 - as core::ops::function::FnOnce>::call_once::h195f6305f7136c56 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/alloc/src/boxed.rs:1985:9 28: 0x55abbd987ca1 - as core::ops::function::FnOnce<()>>::call_once::h181a9c16633ba4de at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/panic/unwind_safe.rs:271:9 29: 0x55abbd987ca1 - std::panicking::try::do_call::h905ac218bf796bfa at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:500:40 30: 0x55abbd987ca1 - std::panicking::try::h9dcc205b8bf33b80 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:464:19 31: 0x55abbd987ca1 - std::panic::catch_unwind::h621a7bf3fca30d94 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panic.rs:142:14 32: 0x55abbd987ca1 - test::run_test_in_process::heb61cdfd3e6494b4 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/test/src/lib.rs:678:27 33: 0x55abbd987ca1 - test::run_test::run_test_inner::{{closure}}::hf557b542136e877c at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/test/src/lib.rs:572:39 34: 0x55abbd94f5c8 - test::run_test::run_test_inner::{{closure}}::h58c1fb96d2dec2ea at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/test/src/lib.rs:599:37 35: 0x55abbd94f5c8 - std::sys_common::backtrace::__rust_begin_short_backtrace::hbdb280633d467d73 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:135:18 36: 0x55abbd954e0f - std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}::hb3d9a3f91486778b at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/thread/mod.rs:529:17 37: 0x55abbd954e0f - as core::ops::function::FnOnce<()>>::call_once::hd640337ac275f2a9 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/panic/unwind_safe.rs:271:9 38: 0x55abbd954e0f - std::panicking::try::do_call::h38f30414e82e71ec at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:500:40 39: 0x55abbd954e0f - std::panicking::try::h8093f4a2ed8491c5 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:464:19 40: 0x55abbd954e0f - std::panic::catch_unwind::h5b641cafac111b65 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panic.rs:142:14 41: 0x55abbd954e0f - std::thread::Builder::spawn_unchecked_::{{closure}}::he4f4b706e4c9f84b at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/thread/mod.rs:528:30 42: 0x55abbd954e0f - core::ops::function::FnOnce::call_once{{vtable.shim}}::hae0e245f49617ce8 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/ops/function.rs:250:5 43: 0x55abbe5b5c75 - as core::ops::function::FnOnce>::call_once::h9adfc2ae43657457 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/alloc/src/boxed.rs:1985:9 44: 0x55abbe5b5c75 - as core::ops::function::FnOnce>::call_once::h14fefbfa7b574396 at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/alloc/src/boxed.rs:1985:9 45: 0x55abbe5b5c75 - std::sys::unix::thread::Thread::new::thread_start::ha211bb47f6f5cedc at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys/unix/thread.rs:108:17 46: 0x7f2d5588c9eb - 47: 0x7f2d55910dfc - 48: 0x0 - failures: tests::pure_dart test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 12.97s error: test failed, to rerun pass `--bin flutter_rust_bridge_codegen` error: Recipe `_rust_build_and_test_single` failed on line 59 with exit code 101 error: Recipe `rust_build_and_test` failed on line 48 with exit code 101 ```
fzyzcjy commented 9 months ago

Interesting! Looks like we are near (eg further reduce to minimal sample)

JustSimplyKyle commented 9 months ago

I condensed the frb_examples in into just these and it still can't initiate main.dart

import 'package:flutter_rust_bridge/flutter_rust_bridge.dart';
import 'package:test/test.dart';
import 'ffi.io.dart' if (dart.library.html) 'ffi.web.dart';

void main(List<String> args) async {
  String dylibPath = args[0];
  print('flutter_rust_bridge example program start (dylibPath=$dylibPath)');
  print('construct api');
  final api = initializeExternalLibrary(dylibPath);
  tearDownAll(() => api.dispose());

  // Test if sync return is working as expected.
  test('dart call panic', () async {
    try {
      api.panic();
      fail("exception not thrown");
    } on FfiException catch (e) {
      print('dart catch e: $e');
    }
  });
}

api.rs

// Test if sync return is working as expected by using Vec<u8> as return value.
pub fn panic() {
    panic!("will panic");
}

image

generated

  Future<void> panic({dynamic hint}) {
    return _platform.executeNormal(FlutterRustBridgeTask(
      callFfi: (port_) => _platform.inner.wire_panic(port_),
      parseSuccessData: _wire2api_unit,
      constMeta: kPanicConstMeta,
      argValues: [],
      hint: hint,
    ));
  }
  Future<S> executeNormal<S>(FlutterRustBridgeTask<S> task) {
    final completer = Completer<dynamic>();
    final sendPort = singleCompletePort(completer);
    task.callFfi(sendPort.nativePort);
    return completer.future.then((dynamic raw) =>
        _transformRust2DartMessage(raw, task.parseSuccessData));
  }

I'm not entirely sure whether if executeNormal calls execute, but https://github.com/fzyzcjy/flutter_rust_bridge/blob/e248cbc2df2328043843a12d61fc551b004fb9a4/frb_rust/src/handler.rs#L203 it seems like here, catch_unwind is only "capturing once" (and naively wrapping the full function in catch_unwind doesn't fix it)

EDIT: after further testing image

the dbg! call(I add the dbg! call after the spawn call in execute linked above)seems like it's racing with the panic, so it seems like the error is indeed in the spawn call

EDIT2:

                dbg!("test from execute(after spawn call)");
                let ret = task(TaskCallback::new(rust2dart.clone()))
                    .map(|e| e.into_into_dart().into_dart());

if we put the dbg! call after the ret, it won't get called! hmm... so when task try to execute our function(which is just panic) it failes to do so... but we also failed to catch the panic??

this is my modified handler execute, I removed all the error handling code for now

        let _ = panic::catch_unwind(move || {
            let WrapInfo { port, .. } = wrap_info;

            let _ = panic::catch_unwind(move || {
                let port2 = port.as_ref().cloned();
                let port2 = port2.expect("(worker) thread");
                #[allow(clippy::clone_on_copy)]
                let rust2dart = Rust2Dart::new(port2.clone());

                let _ = panic::catch_unwind(move || {
                    task(TaskCallback::new(rust2dart.clone()))
                        .map(|e| e.into_into_dart().into_dart())
                });
                unreachable!();
            });
        });
fzyzcjy commented 9 months ago

and naively wrapping the full function in catch_unwind doesn't fix it

so

// Test if sync return is working as expected by using Vec<u8> as return value.
pub fn panic() {
  catch_unwind(||{
    panic!("will panic");
  });
}

does not work?

JustSimplyKyle commented 9 months ago

yep, doesn't still errors with fatal runtime error: failed to initiate panic, error 3

fzyzcjy commented 9 months ago

that looks expected error, since our major problem is "failed to initiate panic" - so the panic itself should cause abort IMHO

(note: I am working on mathematics today, so my brain may be quite silly about programming!)

fzyzcjy commented 9 months ago

so next step may be further reduce it. for example, some naive bisect ideas:

fzyzcjy commented 8 months ago

Let me come back to this problem...

fzyzcjy commented 8 months ago
fzyzcjy commented 8 months ago

As shown in the issues above, it is reproduced using naive Dart + naive Rust, without flutter_rust_bridge.

fzyzcjy commented 8 months ago

With https://github.com/dart-lang/sdk/issues/53545#issuecomment-1722421459, I checked the dart version between the two CI that fails and passes:

stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 5 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.