ntex-rs / ntex

framework for composable networking services
Apache License 2.0
1.84k stars 105 forks source link

Ntex runtime panic #335

Closed pavlospt closed 2 months ago

pavlospt commented 2 months ago

Hello, I have opened a PR on Shuttle where I am trying to provide an integration with the Ntex framework. Unfortunately there is a runtime error when we are running the basic example.

thread 'tokio-runtime-worker' panicked at /home/jonaro00/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ntex-rt-0.4.12/src/lib.rs:192:9:
`spawn_local` called from outside of a `task::LocalSet`

The relevant PR is: https://github.com/shuttle-hq/shuttle/pull/1707

Are there any indications on what I might be doing wrong?

Thank you in advance!

fafhrd91 commented 2 months ago

I quickly look into pr. I don't really understand how shuttle work. technically, ntex should be similar to Actix, but I am not sure how Actix works nowadays. as I remember Actix team decided tight connect Actix and Tokio, on other hand ntex is as neutral as possible, so you might need to construct Tokio runtime via ntex. unfortunately, I don't have much free time to invest

pavlospt commented 2 months ago

I quickly look into pr. I don't really understand how shuttle work. technically, ntex should be similar to Actix, but I am not sure how Actix works nowadays. as I remember Actix team decided tight connect Actix and Tokio, on other hand ntex is as neutral as possible, so you might need to construct Tokio runtime via ntex. unfortunately, I don't have much free time to invest

Just one question, when you say "construct Tokio runtime" could you explain a bit more what you mean? I am just trying to identify the root cause so that I can resolve it :)

fafhrd91 commented 2 months ago

someone needs to construct tokio runtime, i dont see this in your pr. all async operations must run from within

fafhrd91 commented 2 months ago

https://github.com/ntex-rs/ntex/blob/395cf694e5c86f82a67edbd42514c435a35b674d/ntex-rt/src/lib.rs#L170

pavlospt commented 2 months ago

Cool thank you very much for your time and insights. I will try to look deeper into it!

pavlospt commented 2 months ago

@fafhrd91 I have given it a try but apparently since I can get an access to Shuttle's runtime with Handle::current, while I cannot pass it to Ntex I cannot think of a way to avoid having Ntex creating a new runtime and essentially panicking. Am I thinking it wrong?

fafhrd91 commented 2 months ago

you can try to modify System builder and allow to run it with tokio’s Handler

pavlospt commented 2 months ago

Also from what I have been reading about Actix's similar documentation, the behaviour between Ntex and Actix looks similar, regarding LocalSet etc!

pavlospt commented 2 months ago

you can try to modify System builder and allow to run it with tokio’s Handler

You mean by submitting a PR to Ntex to add that feature, right?

fafhrd91 commented 2 months ago

you can try to modify System builder and allow to run it with tokio’s Handler

You mean by submitting a PR to Ntex to add that feature, right?

right, but just for tokio feature. i dont want to depend on tokio in mid term

pavlospt commented 2 months ago

you can try to modify System builder and allow to run it with tokio’s Handler

You mean by submitting a PR to Ntex to add that feature, right?

right, but just for tokio feature. i dont want to depend on tokio in mid term

Ok got it, I will give it a try but since I am new to Rust, I might struggle a bit 😄

pavlospt commented 2 months ago

@fafhrd91 I have opened this. Am I towards a totally wrong direction? https://github.com/ntex-rs/ntex/pull/336

fafhrd91 commented 2 months ago

use ntex::rt::System::new("main").run_local(async {}) in your pr

pavlospt commented 2 months ago

Amazing! Thank you very much @fafhrd91 ❤️

photino commented 2 months ago

use ntex::rt::System::new("main").run_local(async {}) in your pr

@fafhrd91 I cannot find the run_local method in the docs for SystemRunner.

fafhrd91 commented 2 months ago

this method available only for tokio feature, probably docs gen params needs to be adjusted

photino commented 2 months ago

@fafhrd91 Thanks!

How can we run async jobs in the background? I have tried

System::new("scheduler")
    .system()
    .arbiter()
    .spawn(Box::pin(async move {
        loop {
            scheduler.tick().await;

            // Cannot use `std::thread::sleep` because it blocks the Tokio runtime.
            time::sleep(scheduler.time_till_next_job()).await;
        }
    }));

But it does not work as expected. The code is from our integration with ntex.

fafhrd91 commented 2 months ago

ntex_rt::spawn(some_fut)

photino commented 2 months ago

ntex_rt::spawn(some_fut)

It returns an error:

`spawn_local` called from outside of a `task::LocalSet`
fafhrd91 commented 2 months ago

you have to pass System::current() from block_on. and the use System::arbiter().spawn()

here is example https://github.com/ntex-rs/ntex/blob/c60e7f52b77bb99eca545a11312cfacead1745a0/ntex/tests/server.rs#L27