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

"halted for unexpected reason awaited" Error #595

Closed
physics515 opened this issue Aug 1, 2023 · 7 comments
Closed

"halted for unexpected reason awaited" Error #595

physics515 opened this issue Aug 1, 2023 · 7 comments
Labels

Comments

@physics515
Copy link

physics515 commented Aug 1, 2023

Hello all,

I can't seem to figure this one out. I'm trying to write an async function following the docs for 0.12 (https://github.com/rune-rs/rune/blob/0.12.x/scripts/async.rn) and I get an error.

Function:

pub async fn scoped_test_script() { 
    let timeout = 200; 
    let request = http::get(`http://httpstat.us/200?sleep=${timeout}`); 
    let timeout = time::sleep(time::Duration::from_secs(2)); 
    let result = select { _ = timeout => timeout, res = request => res, }?; 
    result.text().await? 
}

Error on the vm.call() function:

halted for unexpected reason `awaited`

Context is:

let runtime_context: RuneContext = rune_modules::default_context().unwrap();

Any idea what I'm missing here?

Thanks!

@udoprog
Copy link
Collaborator

udoprog commented Aug 1, 2023

Hi! You have to use async variants of call / execution functions when running asynchronous code in Rune, like async_call.

@physics515
Copy link
Author

@udoprog, I was able to figure that out but I'm stuck in another way. I'm building an app with Tauri and calling the VM from a Tauri Command.

I'm following this example: https://github.com/rune-rs/rune/blob/0.12.x/examples/examples/tokio_spawn.rs but I keep running into an issue with the Vm not implementing Send. Do you know any way to fix this?

@udoprog
Copy link
Collaborator

udoprog commented Aug 1, 2023

Yeah, pass the Arc<Unit> and Arc<RuntimeContext> around, and construct a Vm (which is a cheap operation) when you need to execute something with send_execute.

@physics515
Copy link
Author

But the error is on the send_execute call. I was looking into how Tauri does async and it appears their async implementation runs every await point on a new thread...? So that means send_execute is being run on a different thread than the other code, meaning it requires Vm to require a Send implementation.

I did try to spin up a new thread to get out of the Tauri runtime and run a generic tokio runtim, but I was trying to move the Vm into that thread. I'll try to pass the Uint and Runtime to the thread and create the Vm on the thread and see if that works.

I'll report back!

Thank you for your help.

@udoprog
Copy link
Collaborator

udoprog commented Aug 1, 2023

Note that send_execute returns a VmSendExecution, which is 'static and implements Send.

If you keep having issues, place put together a minimal reproduction and I might be able to help.

@physics515
Copy link
Author

@udoprog, I figured out my issue. It is actually a Rust language bug where it doesn't drop() references correctly in async runtime.
rust-lang/rust#69663

Instead of this:

let vm = Vm::new(runtime, Arc::new(unit));
let execution = vm.clone().send_execute(["main"], (5u32,))?;

It should be this:

let execution = {
    let vm = Vm::new(runtime, Arc::new(unit));
    vm.clone().send_execute(["main"], (5u32,))?;
};

So that vm is out of scope before the await in:

execution.async_complete().await.unwrap();

or else there is a potential the vm will be moved to a new thread at the await point.

Thank you for your help! I hope this helps someone else.

@udoprog
Copy link
Collaborator

udoprog commented Aug 2, 2023

Glad to hear it. Note that you can do without the .clone() too since you're constructing a Vm for the call.

@udoprog udoprog closed this as completed Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants