dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

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

Open Silvea12 opened this issue 3 months ago • 2 comments

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

  • Page: "State: false"
  • Press button
  • Log: "Start of signal set"
  • Log: "Set to true"
  • Page: "State: true"
  • [pause]
  • Log: "Timer finished"
  • Log: "Signal set to false"
  • Log: "Dropped DropDetector!"
  • Page: "State: false"

Resulting behavior

  • Page: "State: false"
  • Press button
  • Log: "Start of signal set"
  • Log: "Set to true"
  • Log: "Dropped DropDetector!"
  • Page: "State: true"

Environment:

  • Dioxus version: Git 11bf5ae34fc52d2ec1c1a5a76d18b3ba199b20db (fix firefox refreshing loop in debug mode)
  • Rust version: 1.77.1
  • OS info: Linux
  • App platform: Fullstack

Questionnaire

  • [x] I'm interested in fixing this myself but don't know where to start
  • [ ] I would like to fix and I have a solution
  • [ ] I don't have time to fix this right now, but maybe later

Silvea12 avatar Apr 03 '24 23:04 Silvea12

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 avatar Apr 03 '24 23:04 Silvea12

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

Silvea12 avatar Apr 04 '24 20:04 Silvea12