Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.7] panick when loading a blocking resource #2995

Open
winteler opened this issue Sep 19, 2024 · 1 comment
Open

[0.7] panick when loading a blocking resource #2995

winteler opened this issue Sep 19, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@winteler
Copy link

winteler commented Sep 19, 2024

Hello,

I believe I found an race condition with blocking resources. When loading a blocking resource in a <Suspense/>, I'm sometimes getting:

thread 'tokio-runtime-worker' panicked at reactive_graph-0.1.0-beta5/src/computed/async_derived/future_impls.rs:83:29:
At reproduce-leptos-0-7-beta5/src/app.rs:70:33, you tried to access a reactive value which was defined at src/app.rs:61:20, but it has already been disposed.

From my short tests, it looks like this is more likely to happen when the resource takes some time to load (hence the sleep in the reproduction below).

Leptos Dependencies

leptos = { version = "0.7.0-beta5", features = ["nightly"] }
leptos_axum = { version = "0.7.0-beta5", optional = true }
leptos_meta = { version = "0.7.0-beta5" }
leptos_router = { version = "0.7.0-beta5", features = ["nightly"] }

To Reproduce

use leptos::either::Either;
use leptos::prelude::*;
use leptos_meta::{provide_meta_context, MetaTags, Stylesheet, Title};
use leptos_router::{
    components::{Route, Router, Routes},
    StaticSegment,
};

pub fn shell(options: LeptosOptions) -> impl IntoView {
    view! {
        <!DOCTYPE html>
        <html lang="en">
            <head>
                <meta charset="utf-8"/>
                <meta name="viewport" content="width=device-width, initial-scale=1"/>
                <AutoReload options=options.clone() />
                <HydrationScripts options/>
                <MetaTags/>
            </head>
            <body>
                <App/>
            </body>
        </html>
    }
}

#[server]
pub async fn data() -> Result<String, ServerFnError> {
    tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
    log!("Get data.");
    Ok(String::from("hello world"))
}

#[component]
pub fn App() -> impl IntoView {
    // Provides context that manages stylesheets, titles, meta tags, etc.
    provide_meta_context();

    view! {
        // injects a stylesheet into the document <head>
        // id=leptos means cargo-leptos will hot-reload this stylesheet
        <Stylesheet id="leptos" href="/pkg/reproduce-leptos-0-7-beta5.css"/>

        // sets the document title
        <Title text="Welcome to Leptos"/>

        // content for this welcome page
        <Router>
            <main>
                <Routes fallback=|| "Page not found.".into_view()>
                    <Route path=StaticSegment("") view=HomePage/>
                </Routes>
            </main>
        </Router>
    }
}

/// Renders the home page of your application.
#[component]
fn HomePage() -> impl IntoView {
    let resource = Resource::new_blocking(
        move || (),
        move |_| data()
    );
    view! {
        <h1>"Welcome to Leptos!"</h1>
        <Suspense>
        {
            move || Suspend::new(async move {
                match &resource.await {
                    Ok(value) => Either::Left(view! { <div>{value.clone()}</div> }),
                    Err(_e) => Either::Right(view! { <div>"Error test"</div> }),
                }
            })
        }
        </Suspense>
    }
}

As mentioned before, the error doesn't systematically appear, so maybe you'll need to try a few times or play with the tokio::time::sleep to reproduce it. Please tell me if there is any way I can help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants
@gbj @winteler and others