-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Problem
Fullstack web app client side crashes with a panicked at already borrowed: BorrowMutError
when a user triggers browser autofill on a form.
I think the cause is that the browser autofill can dispatch synthetic keyboard events where modifier keys (altKey, ctrlKey, etc.) have a value of undefined. The Dioxus WASM bindings expect these properties to always be booleans
. When the code attempts to access these properties (e.g., via event.data.modifiers()
), the wasm-bindgen glue code fails with an expected a boolean argument, found undefined
error.
Steps To Reproduce
Steps to reproduce the behavior:
- Create a Dioxus web application with a form containing
<input>
fields - Add a keyboard event handler (e.g.,
onkeydown
) that accesses the event's modifier keys usingevent.data.modifiers()
- Use the browser's autofill feature to complete the form fields
- The application crashes with a
BorrowMutError
when autofill triggers synthetic keyboard events
Minimal reproduction example:
rsx! {
div {
// This handler will be triggered by autofill's synthetic events
onkeydown: move |event| {
// This line panics because autofill events have undefined modifier keys
let modifiers = event.data.modifiers();
println!("Modifiers: {:?}", modifiers);
},
input { name: "username", placeholder: "Username" }
input { name: "password", r#type: "password", placeholder: "Password" }
}
}
I know that many browsers all handle autofill in different ways, this error happened on microsoft edge (chromium based), although I think it would be a good thing to handle regardless.
Expected behavior
The application should handle synthetic keyboard events from browser autofill gracefully without crashing. When modifier key values are undefined, they should be safely coerced to false, allowing the event handler to execute without error.
Environment:
- Dioxus version: main
- Rust version: 1.88
- OS info: Windows (but I think this applies to any)
- App platform: web (fullstack)
Questionnaire
I would like to fix and I have a solution.
Add #[serde(default)]
attributes to the modifier key fields in SerializedKeyboardData
struct (packages/html/src/events/keyboard.rs
):
alt_key: bool,
#[serde(default)]
ctrl_key: bool,
#[serde(default)]
meta_key: bool,
#[serde(default)]
shift_key: bool,```