DioxusLabs / DioxusLabs/dioxus

Unexpected Err(Finished) when receiving from an eval

Open
#3,084 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**
I'm experiencing occasional failures when using the `eval` hook to run Javascript that in turn calls `dioxus.send()` to send a message back to Rust where I've called `eval.recv().await`. The problem is that `eval.recv().await` will sometimes return an `Err(Finished)`.

**Steps To Reproduce**
I've published [a public repo](https://github.com/StevenLove/dexamples/tree/eval-1) where you can see my approach, in a project just barely modified from the template project created by `dx new`.

At its core, my approach can be seen below where a button's onclick handler creates a loop that will repeatedly calculate the current timestamp and send it (using `eval`) to Javascript where Javascript will send it back to Rust which is using `eval.recv().await`. I have tried adding timeouts to delay the javascript call to `dioxus.send()` and tokio sleeps to delay calling `eval.recv().await` and have not gotten reliable success with either approach.

```rust
#[component]
fn Home() -> Element {
let mut count = use_signal(|| 0);
let mut local_timestamps = use_signal::>(|| vec![]);
let mut remote_timestamps = use_signal::>(|| vec![]);

rsx! {
Link { to: Route::Blog { id: count() }, "Go to blog" }
div {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
button {
onclick: move |_| async move {
loop {
let t: u128 = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis();
local_timestamps.push(t);
let mut eval = eval(
&format!(
r#"
let t = {t};
console.log("local timestamp",t);
dioxus.send(t);
"#,
),
);
let received = eval.recv().await;
let Ok(received) = received else {
println!("Failure to recv! {:?}", received);
return;
};
let parsed = serde_json::from_value::(received).unwrap();
remote_timestamps.push(parsed);
}
},
"Send timestamps in a loop"
}
}

div { {format!("local_timestamps: {:?}",local_timestamps) } }
div { {format!("remote_timestamps: {:?}", remote_timestamps) } }
}
}
```

**Expected behavior**
No Errors when sending simple messages back and forth with `eval`, `dioxus.send()`, and `eval.recv().await`

**Environment:**
- Dioxus version: v0.5.6
- Rust version: 1.81.0
- OS info: MacOS 15.0.1
- App platform: Desktop

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.