DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
19.3k stars 735 forks source link

`use_resource` cancels `spawn_forever`'s future (and even regular `spawn`) when listening to signals set by said future #2235

Open Silvea12 opened 2 months ago

Silvea12 commented 2 months ago

Problem

When setting a signal in a spawn_forever() (and even a regular spawn(), or component async fn), if it is also used by a resource, it cancels the future erroneously.

Steps To Reproduce

Note: gloo_timers here is just as a simple await point, this occurs with server functions and all other operations that yield to the async runtime. Steps to reproduce the behavior:

#[component]
fn Broken() -> Element {
    let signal = use_signal(|| false);
    let _future = use_resource(move || async move { signal() });
    let action = move |_| {
        spawn_forever(async move {
            let d = DropDetector;
            let mut signal = signal.clone();
            log::info!("Start of signal set");
            signal.set(true);
            log::info!("Set to true");
            gloo_timers::future::sleep(Duration::from_millis(500)).await;
            log::info!("Timer finished");
            signal.set(false);
            log::info!("Signal set to false");
            drop(d);
        });
    };

    rsx! {
        div {
            div { "State: {signal}" }
            button { onclick: action, "press me" }
        }
    }
}

struct DropDetector;
impl Drop for crate::DropDetector {
    fn drop(&mut self) {
        log::warn!("Dropped DropDetector!");
    }
}

Expected behavior

Resulting behavior

Environment:

Questionnaire

Silvea12 commented 2 months ago

Worth a note: this was encountered at first when doing loading screen stuff with server functions and use_server_future() - but this is a more minimal and targeted sample. The broken bit is unrelated to server fns.

Silvea12 commented 2 months ago

@jkelleyrtp it seems the bug is not fixed. Tested with git 821a650f775526847158f3109a2b2b55aa3aa6bc, same test code as above.