oak icon indicating copy to clipboard operation
oak copied to clipboard

Add support for deadlines and cancellations to Oak Functions (on Oak Containers, at least)

Open andrisaar opened this issue 2 years ago • 1 comments

When handling a user request, we just call the wasm main function and let it run, no matter how long it takes.

This means that if the incoming RPC is cancelled, or it has a timeout and the wasm execution exceeds that, we will keep using resources (and hog a thread) to compute an answer nobody is listening for any longer.

Once we've determined that the incoming RPC has been cancelled or timed out, we should terminate the wasm runtime.

wasmi has a fairly generic concept of "fuel" that wasm execution consumes, and if it's out of fuel, the call will terminate. At that point, you can add more fuel, and resume execution.

Thus, the easiest way would be to implement something like this: a) have some kind of flag that can be asynchronously set that denotes that there's no point in continuing the wasm execution b) in oak_functions_service, have something like this:

  1. add about 100 ms worth of fuel (each wasmi instruction is ~1 unit of fuel)
  2. use call_resumable to invoke main
  3. check result, if the termination flag has not been sent and we're out of fuel, add some fuel, and continue execution

andrisaar avatar Dec 14 '23 19:12 andrisaar

A closer look suggests that you can't actually resume execution after you've ran out of fuel :(

andrisaar avatar Dec 14 '23 20:12 andrisaar