seed icon indicating copy to clipboard operation
seed copied to clipboard

Panicked at 'already borrowed: BorrowMutError'

Open torhovland opened this issue 4 years ago • 5 comments

I'm getting some runtime errors that I'm unable to work around, when I'm interacting with a map through leaflet-rs.

I have a reproducible sample on https://github.com/torhovland/seed-runtime-errors.

I found an explanation @MartinKavik gave to a similar error on Discord (https://discord.com/channels/656436070086606858/676742707246399489/827563775112380438):

When you want to handle a Msg, Seed has to borrow Model to invoke update function with the argument &mut Model. The problem appears when your are in the update body (on in a nested function) and want to handle another Msg directly through msg_sender - it fails because Model is already borrowed. Also it's pretty simple to create an infinite loop this way. That's why msg_sender should be used only in event listeners/callbacks and a similar concept doesn't exist in Elm. So to fix it, I think you have to do some big changes in Seed code / architecture and resolve somehow the danger of infinite loop or stack overflow

If anybody can point out how I can change the sample so that it works, that would be great!

These are the errors I'm getting:

package.js:856 panicked at 'already borrowed: BorrowMutError', /home/tor/.cargo/registry/src/github.com-1ecc6299db9ec823/seed-0.8.0/src/app.rs:400:38

Stack:

Error
    at http://localhost:8000/pkg/package.js:862:19
    at http://localhost:8000/pkg/package.js:219:22
    at console_error_panic_hook::Error::new::hd9f604bf56b7e78e (http://localhost:8000/pkg/package_bg.wasm:wasm-function[5422]:0x15016e)
    at console_error_panic_hook::hook_impl::h2c6256fab72bb030 (http://localhost:8000/pkg/package_bg.wasm:wasm-function[704]:0xab889)
    at console_error_panic_hook::hook::h329dcc4db7d7b91d (http://localhost:8000/pkg/package_bg.wasm:wasm-function[6019]:0x1583f3)
    at core::ops::function::Fn::call::hc897d66f9881f157 (http://localhost:8000/pkg/package_bg.wasm:wasm-function[5050]:0x14a5ca)
    at std::panicking::rust_panic_with_hook::h364974e717ed59ec (http://localhost:8000/pkg/package_bg.wasm:wasm-function[1434]:0xe16f7)
    at std::panicking::begin_panic_handler::{{closure}}::h9c6751687bfc5521 (http://localhost:8000/pkg/package_bg.wasm:wasm-function[2446]:0x10cddf)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h438eceaf49d629cd (http://localhost:8000/pkg/package_bg.wasm:wasm-function[6909]:0x16090d)
    at rust_begin_unwind (http://localhost:8000/pkg/package_bg.wasm:wasm-function[5723]:0x154631)


(anonymous) @ package.js:856
(anonymous) @ package.js:219
$console_error_panic_hook::error::h20b2d0bb64fcd3de @ package_bg.wasm:0xfa87f
$console_error_panic_hook::hook_impl::h2c6256fab72bb030 @ package_bg.wasm:0xab981
$console_error_panic_hook::hook::h329dcc4db7d7b91d @ package_bg.wasm:0x1583f3
$core::ops::function::Fn::call::hc897d66f9881f157 @ package_bg.wasm:0x14a5ca
$std::panicking::rust_panic_with_hook::h364974e717ed59ec @ package_bg.wasm:0xe16f7
$std::panicking::begin_panic_handler::{{closure}}::h9c6751687bfc5521 @ package_bg.wasm:0x10cddf
$std::sys_common::backtrace::__rust_end_short_backtrace::h438eceaf49d629cd @ package_bg.wasm:0x16090d
$rust_begin_unwind @ package_bg.wasm:0x154631
$core::panicking::panic_fmt::hf69c8b08bc9d2ee5 @ package_bg.wasm:0x15a73c
$core::result::unwrap_failed::h694db13a45aa6740 @ package_bg.wasm:0x115bcb
$core::result::Result<T,E>::expect::h76a5fac5227fb2e0 @ package_bg.wasm:0xfacad
$core::cell::RefCell<T>::borrow_mut::h1717f73af2cc77cc @ package_bg.wasm:0x10652c
$seed::app::App<Ms,Mdl,INodes>::process_queue_message::h7c554415873a51ff @ package_bg.wasm:0x6061b
$seed::app::App<Ms,Mdl,INodes>::process_effect_queue::hae5b53df38d0104f @ package_bg.wasm:0x7c9e3
$seed::app::App<Ms,Mdl,INodes>::update_with_option::h2bc52f490e789949 @ package_bg.wasm:0xd8df7
$seed::app::App<Ms,Mdl,INodes>::update::h1ae5310f83ab96d9 @ package_bg.wasm:0x12ec13
$seed_runtime_errors::init::{{closure}}::{{closure}}::hbb017692c9137299 @ package_bg.wasm:0xda0bf
$<dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hb6afaa47babbe211 @ package_bg.wasm:0xf87db
__wbg_adapter_26 @ package.js:253
real @ package.js:201
fire @ Events.js:190
_moveStart @ Map.js:1222
_resetView @ Map.js:1196
setView @ Map.js:206
panTo @ Map.js:308
(anonymous) @ package.js:353
(anonymous) @ package.js:219
$leaflet::Map::panTo::h6e86d34d09d91eea @ package_bg.wasm:0x1377f1
$seed_runtime_errors::update::h6554ae90c998e622 @ package_bg.wasm:0xad15e
$core::ops::function::FnOnce::call_once::h5177f1f44480f7e4 @ package_bg.wasm:0x126cdb
$seed::app::App<Ms,Mdl,INodes>::start::{{closure}}::h2446f7a4eef87cfb @ package_bg.wasm:0x107d3f
$<alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hd50e2ca7c09fdd4c @ package_bg.wasm:0xe5f8b
$seed::app::App<Ms,Mdl,INodes>::process_queue_message::h7c554415873a51ff @ package_bg.wasm:0x606d8
$seed::app::App<Ms,Mdl,INodes>::process_effect_queue::hae5b53df38d0104f @ package_bg.wasm:0x7c9e3
$seed::app::App<Ms,Mdl,INodes>::update_with_option::h2bc52f490e789949 @ package_bg.wasm:0xd8df7
$seed::app::App<Ms,Mdl,INodes>::mailbox::{{closure}}::h9c58bf1b7066ceb1 @ package_bg.wasm:0x1461c1
$seed::virtual_dom::mailbox::Mailbox<Ms>::send::hbf3e65002b6d7bf0 @ package_bg.wasm:0xeaf40
$seed::virtual_dom::event_handler_manager::listener::Listener<Ms>::new::{{closure}}::h7c94d49f83ee689a @ package_bg.wasm:0x7c26b
$<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h0e5f1a165e3d1d4b @ package_bg.wasm:0xe911c
__wbg_adapter_20 @ package.js:241
real @ package.js:201
Show 13 more frames
package.js:229 wasm-bindgen: imported JS function that was not marked as `catch` threw an error: unreachable

Stack:
RuntimeError: unreachable
    at __rust_start_panic (http://localhost:8000/pkg/package_bg.wasm:wasm-function[7018]:0x160e75)
    at rust_panic (http://localhost:8000/pkg/package_bg.wasm:wasm-function[6425]:0x15cdc8)
    at std::panicking::rust_panic_with_hook::h364974e717ed59ec (http://localhost:8000/pkg/package_bg.wasm:wasm-function[1434]:0xe171e)
    at std::panicking::begin_panic_handler::{{closure}}::h9c6751687bfc5521 (http://localhost:8000/pkg/package_bg.wasm:wasm-function[2446]:0x10cddf)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h438eceaf49d629cd (http://localhost:8000/pkg/package_bg.wasm:wasm-function[6909]:0x16090d)
    at rust_begin_unwind (http://localhost:8000/pkg/package_bg.wasm:wasm-function[5723]:0x154631)
    at core::panicking::panic_fmt::hf69c8b08bc9d2ee5 (http://localhost:8000/pkg/package_bg.wasm:wasm-function[6198]:0x15a73c)
    at core::result::unwrap_failed::h694db13a45aa6740 (http://localhost:8000/pkg/package_bg.wasm:wasm-function[2723]:0x115bcb)
    at core::result::Result<T,E>::expect::h76a5fac5227fb2e0 (http://localhost:8000/pkg/package_bg.wasm:wasm-function[1959]:0xfacad)
    at core::cell::RefCell<T>::borrow_mut::h1717f73af2cc77cc (http://localhost:8000/pkg/package_bg.wasm:wasm-function[2257]:0x10652c)
(anonymous) @ package.js:229
$leaflet::Map::panTo::h6e86d34d09d91eea @ package_bg.wasm:0x1377f1
$seed_runtime_errors::update::h6554ae90c998e622 @ package_bg.wasm:0xad15e
$core::ops::function::FnOnce::call_once::h5177f1f44480f7e4 @ package_bg.wasm:0x126cdb
$seed::app::App<Ms,Mdl,INodes>::start::{{closure}}::h2446f7a4eef87cfb @ package_bg.wasm:0x107d3f
$<alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hd50e2ca7c09fdd4c @ package_bg.wasm:0xe5f8b
$seed::app::App<Ms,Mdl,INodes>::process_queue_message::h7c554415873a51ff @ package_bg.wasm:0x606d8
$seed::app::App<Ms,Mdl,INodes>::process_effect_queue::hae5b53df38d0104f @ package_bg.wasm:0x7c9e3
$seed::app::App<Ms,Mdl,INodes>::update_with_option::h2bc52f490e789949 @ package_bg.wasm:0xd8df7
$seed::app::App<Ms,Mdl,INodes>::mailbox::{{closure}}::h9c58bf1b7066ceb1 @ package_bg.wasm:0x1461c1
$seed::virtual_dom::mailbox::Mailbox<Ms>::send::hbf3e65002b6d7bf0 @ package_bg.wasm:0xeaf40
$seed::virtual_dom::event_handler_manager::listener::Listener<Ms>::new::{{closure}}::h7c94d49f83ee689a @ package_bg.wasm:0x7c26b
$<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h0e5f1a165e3d1d4b @ package_bg.wasm:0xe911c
__wbg_adapter_20 @ package.js:241
real @ package.js:201
package.js:230 Uncaught RuntimeError: unreachable
    at __rust_start_panic (package_bg.wasm:0x160e75)
    at rust_panic (package_bg.wasm:0x15cdc8)
    at std::panicking::rust_panic_with_hook::h364974e717ed59ec (package_bg.wasm:0xe171e)
    at std::panicking::begin_panic_handler::{{closure}}::h9c6751687bfc5521 (package_bg.wasm:0x10cddf)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h438eceaf49d629cd (package_bg.wasm:0x16090d)
    at rust_begin_unwind (package_bg.wasm:0x154631)
    at core::panicking::panic_fmt::hf69c8b08bc9d2ee5 (package_bg.wasm:0x15a73c)
    at core::result::unwrap_failed::h694db13a45aa6740 (package_bg.wasm:0x115bcb)
    at core::result::Result<T,E>::expect::h76a5fac5227fb2e0 (package_bg.wasm:0xfacad)
    at core::cell::RefCell<T>::borrow_mut::h1717f73af2cc77cc (package_bg.wasm:0x10652c)
$__rust_start_panic @ package_bg.wasm:0x160e75
$rust_panic @ package_bg.wasm:0x15cdc8
$std::panicking::rust_panic_with_hook::h364974e717ed59ec @ package_bg.wasm:0xe171e
$std::panicking::begin_panic_handler::{{closure}}::h9c6751687bfc5521 @ package_bg.wasm:0x10cddf
$std::sys_common::backtrace::__rust_end_short_backtrace::h438eceaf49d629cd @ package_bg.wasm:0x16090d
$rust_begin_unwind @ package_bg.wasm:0x154631
$core::panicking::panic_fmt::hf69c8b08bc9d2ee5 @ package_bg.wasm:0x15a73c
$core::result::unwrap_failed::h694db13a45aa6740 @ package_bg.wasm:0x115bcb
$core::result::Result<T,E>::expect::h76a5fac5227fb2e0 @ package_bg.wasm:0xfacad
$core::cell::RefCell<T>::borrow_mut::h1717f73af2cc77cc @ package_bg.wasm:0x10652c
$seed::app::App<Ms,Mdl,INodes>::process_queue_message::h7c554415873a51ff @ package_bg.wasm:0x6061b
$seed::app::App<Ms,Mdl,INodes>::process_effect_queue::hae5b53df38d0104f @ package_bg.wasm:0x7c9e3
$seed::app::App<Ms,Mdl,INodes>::update_with_option::h2bc52f490e789949 @ package_bg.wasm:0xd8df7
$seed::app::App<Ms,Mdl,INodes>::update::h1ae5310f83ab96d9 @ package_bg.wasm:0x12ec13
$seed_runtime_errors::init::{{closure}}::{{closure}}::hbb017692c9137299 @ package_bg.wasm:0xda0bf
$<dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::hb6afaa47babbe211 @ package_bg.wasm:0xf87db
__wbg_adapter_26 @ package.js:253
real @ package.js:201
fire @ Events.js:190
_moveStart @ Map.js:1222
_resetView @ Map.js:1196
setView @ Map.js:206
panTo @ Map.js:308
(anonymous) @ package.js:353
(anonymous) @ package.js:219
$leaflet::Map::panTo::h6e86d34d09d91eea @ package_bg.wasm:0x1377f1
$seed_runtime_errors::update::h6554ae90c998e622 @ package_bg.wasm:0xad15e
$core::ops::function::FnOnce::call_once::h5177f1f44480f7e4 @ package_bg.wasm:0x126cdb
$seed::app::App<Ms,Mdl,INodes>::start::{{closure}}::h2446f7a4eef87cfb @ package_bg.wasm:0x107d3f
$<alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hd50e2ca7c09fdd4c @ package_bg.wasm:0xe5f8b
$seed::app::App<Ms,Mdl,INodes>::process_queue_message::h7c554415873a51ff @ package_bg.wasm:0x606d8
$seed::app::App<Ms,Mdl,INodes>::process_effect_queue::hae5b53df38d0104f @ package_bg.wasm:0x7c9e3
$seed::app::App<Ms,Mdl,INodes>::update_with_option::h2bc52f490e789949 @ package_bg.wasm:0xd8df7
$seed::app::App<Ms,Mdl,INodes>::mailbox::{{closure}}::h9c58bf1b7066ceb1 @ package_bg.wasm:0x1461c1
$seed::virtual_dom::mailbox::Mailbox<Ms>::send::hbf3e65002b6d7bf0 @ package_bg.wasm:0xeaf40
$seed::virtual_dom::event_handler_manager::listener::Listener<Ms>::new::{{closure}}::h7c94d49f83ee689a @ package_bg.wasm:0x7c26b
$<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h0e5f1a165e3d1d4b @ package_bg.wasm:0xe911c
__wbg_adapter_20 @ package.js:241
real @ package.js:201
Show 9 more frames

torhovland avatar Nov 11 '21 21:11 torhovland

I've had success using leaflet through web components. It provides a much nicer declarative API for use with seed, although writing the web components in JavaScript can get a little messy.

It's not in a state where I feel like I can publish it yet, but here's the raw code for what I have so far, if you feel adventurous: https://gist.github.com/glennsl/93c272ba42f954fcf5eec99b1311ac05

glennsl avatar Nov 11 '21 22:11 glennsl

Also, your repro link 404s.

glennsl avatar Nov 11 '21 22:11 glennsl

Also, your repro link 404s.

Thanks for the hint. The repo was initially private. Fixed now.

Also, thanks for the code. I will try it and see if doing it that way gets rid of the errors.

torhovland avatar Nov 12 '21 06:11 torhovland

@glennsl Unfortunately, I can't get your code to compile. It would be cool if you could submit a working app.

torhovland avatar Nov 12 '21 19:11 torhovland

@torhovland Here you go: https://github.com/glennsl/leafy-example

Modified the bindings slightly to remove the use of some internal types and not very important plugins/extensions for leaflet.

glennsl avatar Nov 28 '21 22:11 glennsl