Reading a signal in a call's arguments panics when the call writes the same signal through a copied handle
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Documentation
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- rust
- Domain
- documentation, frontend
Research direction
Start with the doc comments on WritableExt::write, set and with_mut, and the guide's "Signals are Borrowed at Runtime" section. Reproduce the panic with the snippet in the issue, which passes *count.peek() + 1 into a helper that calls set on a copied Signal. Done means each of those docs states that the call panics if the value is already borrowed and points to try_write, and the guide notes that a guard in a call's arguments lives until the end of the statement.
Written by the indexing model from the issue text.
Description
Problem
This compiles cleanly, passes clippy, and panics at runtime. It isn't the await case from #4124, so the await-holding-invalid-types config doesn't catch it.
A peek() or read() guard that's a temporary inside a function call's arguments lives until the end of the statement. If that function writes the same signal through its own copy of the handle (Signal is Copy), set fails the runtime borrow check and panics.
On a single binding the borrow checker catches it (count.set(*count.peek() + 1) is E0502). It only gets through once the write goes through a copy, like a helper function or a struct holding signal handles.
I hit it with an Elm-style screen model. Each screen's logic is a plain state machine, State::update(Action) -> Vec<Effect>, with no Dioxus in it so it can be tested on its own. The component keeps a small Copy driver struct holding the screen's signal handles. dispatch runs update and then applies each effect, which mostly means setting signals. A toggle button passes the value it currently shows into the action:
onclick: move |_| {
driver.dispatch(Action::Toggle { shown: *setting.peek() });
}
update returns Effect::Show(next), the driver calls setting.set(next) through its own copy of the handle, and the peek() guard from the argument is still alive. The app crashed on the first tap. A dispatch helper like this is the normal place for writes to end up once the logic lives outside the component, so I'd expect others to hit it the same way.
Separately, the docs for WritableExt::write say it panics "if the value has been dropped", and set doesn't mention panicking. Both also panic when the value is already borrowed.
Steps To Reproduce
use dioxus::prelude::*;
// Writes through its own copy of the signal handle.
fn store(mut count: Signal<i32>, value: i32) {
count.set(value);
}
fn app() -> Element {
let count = use_signal(|| 0);
use_hook(move || {
// Compiles, then panics: the `peek()` guard is a temporary that lives
// until the end of the statement, so it is still held when `store`
// calls `set`.
store(count, *count.peek() + 1);
});
rsx! { "{count}" }
}
fn main() {
let mut dom = VirtualDom::new(app);
dom.rebuild_in_place();
}
panicked at src/main.rs:5:11:
called `Result::unwrap()` on an `Err` value: AlreadyBorrowed(AlreadyBorrowedError { borrowed_at: [Location { file: "src/main.rs", line: 14, column: 29 }] })
Same in release (without the borrowed_at location), and the same with read() instead of peek(). Both of these work:
let next = *count.peek() + 1; // the guard drops at the semicolon
store(count, next);
store(count, count() + 1); // the call syntax clones out and drops its guard
Expected behavior
Runtime borrow checking is how signals work, so the panic itself may be intended. What would help:
- the
write,setandwith_mutdocs mentioning the already-borrowed panic and pointing totry_write - if you think it's worth it, a line in the guide's "Signals are Borrowed at Runtime" section. It covers a read and a write in the same scope, and calls copying a signal an advanced way to relax the rules, but passing a copy into a helper is the ordinary case, and a guard in a call's arguments lives to the end of the statement rather than the scope
I'm happy to put up a PR for the docs if that's welcome.
Environment:
- Dioxus version: 0.7.10
- Rust version: 1.99.0
- OS info: macOS 26.6.2
- App platform: iOS, also reproduced headless with
VirtualDom
Questionnaire
I would like to fix and I have a solution. (docs only)
Thanks for Dioxus.
- Dominant language
- Rust
- Stars
- 39.8k
- Forks
- 2k
- Avg merge
- 14h 29m
- Merged PRs (30d)
- 20
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DioxusLabs/dioxus
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
DioxusLabs/dioxus#6095 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
DioxusLabs/dioxus#6089 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 83/100
DioxusLabs/dioxus#6063 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DioxusLabs/dioxus#6008 ·
Maintainers usually reply within 1 day
-
dioxus-desktop on macOS: Finder file drops never fire `ondrop` — `dragenter` is not cancelledPossibly taken @staging-devin-ai-integration claimed this 23 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
DioxusLabs/dioxus#6006 ·
Maintainers usually reply within 1 day
All issues in DioxusLabs/dioxus
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
longbridge/gpui-kit#4031 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
neuralmagic/crucible#465 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
compiler P3
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
HerringtonDarkholme/hd-lang#2162 ·
Maintainers usually reply within 1 day
-
embedded SQL: pin correlated-guard behaviour when a closed variable is reassigned inside a branchOpenpriority: low
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
jonathanong/no-mistakes#3627 ·
Maintainers usually reply within 1 day