Open Giovanni-Tably opened 2 months ago
I looked into this a bit and I can write up a longer explanation if you'd really like to know; the short version is that it effects are marked "clean" when they finish running, so marking it dirty while it's still dirty doesn't work as you expect (because it then marks itself clean).
I'm struggling to come up with a reason this would be a good idea, though -- could you share a use case?
Thank you for looking into this!
I looked into this a bit and I can write up a longer explanation if you'd really like to know; the short version is that it effects are marked "clean" when they finish running, so marking it dirty while it's still dirty doesn't work as you expect (because it then marks itself clean).
Aah, that makes sense. And while marking it dirty again, it also somehow clears the subscriptions the effect is supposed to be picking up? Though it might also mean the reproduction is not quite what I encountered originally then (more details below).
I'm struggling to come up with a reason this would be a good idea, though
I agree that it's best avoided if possible, especially the simple repro above which does not involve async code. But it also feels like a pattern that is easy to stumble into in simple cases, and tricky to make sure you don't stumble into in involved applications where the recursion could be many layers removed.
In practice debugging it is also tricky, because the first update will often be correct, and any run after that will be silently broken.
On a more subjective note, I personally find the behaviours above simplify the mental model in terms of leptos
code behaving as much as possible like 'normal' code, where recursion either works or panics (as in recursive RefCell
borrowing).
could you share a use case?
In my case the app/user would be modifying a complex signal that is widely shared, it needs to remain editable and be present in the UI while the edits finish, and then further while an animation runs. When all of that is done, we process the signal and then reset it.
When we wrote the code we had unscoped tasks (as in, a simple wasm_bindgen_futures::spawn_local
with cancel-on-drop), which was working as there was no actual recursion in the reactive sense happening.
Then we introduced scoping (ScopedFuture
), which triggered the problem (not quite sure why, since it would be executed asynchronously. The owner is still relevant somehow?).
The above is the simplest reproduction I could find, which turned out not to require async code, but maybe there are two separate behaviours?
The actual code would have looked something like:
// This is a widely shared signal, bound to the UI in a bunch of places.
let something: Signal<Option<_>> = todo!();
// Here we spawn a task to do something, if re-run the task is reset.
// Uses [ScopedFuture] with the effect as the owner internally.
something.for_each_async(move |v| async {
if let Some(v) = something.get() {
sleep().await;
do_stuff();
something.set(None);
}
});
// This code is not quite right (like the timing), but not in relevant-to-the-issue ways.
Reproduction including async, pardon the hackiness:
Whatever way leptos
decides to go forward with, I would be happy to PR any required change and/or document recursion behaviour.
I guess with the new reactive system coming up it might also make sense to shelve this temporarily, which would be totally fine.
You can somewhat workaround this issue by setting the signal from within a call to queue_microtask
You can somewhat workaround this issue by setting the signal from within a call to
queue_microtask
That indeed works and should be ~equivalent to using wasm_bindgen_futures::spawn_local
to mimic the reproduction cases above. It'll leak the function though, which can cause panics if the signal is disposed of in the meantime.
I would recommend using something like defer_with
to only execute if a parent scope hasn't been cleaned up yet. If you don't use a parent scope then it still won't work. Not sure if it needs to be a parent of the effect, signal, or both. In our case it was both.
Describe the bug
Effects that are recursively triggered (set signals they transitively depend on) stop reacting after recursing. That is, the recursion is prevented somehow, and afterward the effect loses the reactive links to the signals it subscribes to and never runs again.
Also relevant:
:D
set_untracked
to break the recursion, but only if that effect is the only subscriber, orqueue_microtask
. Internally we use adefer_with
function you can see here. Note that we use the parent scope and notdefer
because the issue seemed to persist without it.Leptos Dependencies
Tested in the
leptos
repo on 8686d5aabbd9720ba121fddd1fb8f5473559b670.To Reproduce
Copy the following test in
leptos_reactive/tests/effect.rs
then run:Expected behavior
I expected the test to print
0123
(if recursion is prevented) or maybe0102030
(if recursion is allowed and is up to the user to manage it). The current output is01
.