Skip to content

BorrowMutError with 'onkeydown' listener #5375

@Thibaud379

Description

@Thibaud379

I had a weird panic and managed to recreate it in a minimal example bellow

When clicking or selecting a suggestion using the keyboard on an <input> linked to a <data-list>, I get the following error

19:39:37 [web] wasm-bindgen: imported JS function that was not marked as `catch` threw an error:
19:39:38 [web] panicked at [...]/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/dioxus-core-0.7.3/src/events.rs:665:28:
already borrowed: BorrowMutError
Stack:
Error
    at http://127.0.0.1:8081/wasm/front.js:954:25
    at logError (http://127.0.0.1:8081/wasm/front.js:1815:18)
    at __wbg_new_194e7e70e82f7747 (http://127.0.0.1:8081/wasm/front.js:953:57)
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[2946]:0xdd6a6
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[720]:0x7e13c
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[1393]:0xb9d37
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[1637]:0xc5742
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[3619]:0xe00fb
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[2566]:0xda8ce
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[2567]:0xda8fa

19:39:38 [web] panicked at  [...]/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/dioxus-core-0.7.3/src/events.rs:665:28:
already borrowed: BorrowMutError
Stack:
Error
    at http://127.0.0.1:8081/wasm/front.js:954:25
    at logError (http://127.0.0.1:8081/wasm/front.js:1815:18)
    at __wbg_new_194e7e70e82f7747 (http://127.0.0.1:8081/wasm/front.js:953:57)
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[2946]:0xdd6a6
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[720]:0x7e13c
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[1393]:0xb9d37
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[1637]:0xc5742
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[3619]:0xe00fb
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[2566]:0xda8ce
    at http://127.0.0.1:8081/wasm/front_bg.wasm:wasm-function[2567]:0xda8fa

Steps To Reproduce
With the following setup and using dx serve on the web target

use dioxus::prelude::*;

fn main() {
    launch(App);
}

#[component]
fn App() -> Element {
    rsx! {
       div{
           onkeydown: |e: KeyboardEvent| {
             println!("{:?}",e.key());
           },
           input {
                id: "tag",
                list: "tag-list",
           },
           datalist { id: "tag-list",
                option { value: "a" }
           }
       }
    }
}
  • Just clicking the suggested text for the input triggers the error
  • I suspect the multiple events triggered at the same time (input, change and keydown at least) are to blame
  • Strange note: the panic occurs only when interacting with the KeyboardEvent in the handler i.e. replacing the line println!("{:?}",e.key()); with println!("Hello"); no longer triggers the error

Expected behavior

The input would get the value a and no panic would occur

Screenshots
Image
Environment:

  • Dioxus version: 0.7.3-22b06ba
  • Rust version: rustc 1.88.0-nightly
  • OS info: macOS Tahoe V26.3
  • App platform: web (Chrome Version 145.0.7632.160)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions