Description
As we all know, because of the halting problem, it isn't possible to know whether the execution of a Wasm function will finish in a finite time.
While a Wasm function is being executed, maybe everything is okay and it is simply taking a long time, or maybe the Wasm execution is stuck in an infinite loop. It isn't possible to know in which of the two scenario we currently are.
Because the program that is interpreting the Wasm doesn't want, itself, to be stuck infinitely, it needs some sort of protection against the Wasm being stuck in an infinite loop.
While the program interpreting the Wasm can't know whether the Wasm is stuck in an infinite loop, it can use heuristics.
For example, if the Wasm has been executing for 10 seconds while normally it should take around 2ms, then it is likely to be stuck.
Wasmi provides a fuel system, making it possible to interrupt the execution of the Wasm after a certain number of instructions have been executed.
This system is however not very flexible. It's all-or-nothing: when all the fuel has been consumed, the execution completely stops and can never be resumed.
Furthermore, it is difficult to map "fuel quantities" with real world time/CPU usage. A precise fuel number means that whether the execution will run out of fuel or not doesn't depend on the speed of the machine, which has the advantage to be deterministic. However, this is also a drawback: it is not possible to stop the execution after an approximate number of seconds. If you pick a certain fuel quantity, on some platforms this fuel will expire quickly and on some others it will expire very slowly. Fuel quantities are very arbitrary anyway, and I think that it is legitimate to want to stop after a certain number of real life seconds.
In order to solve that problem, I would suggest to make it possible to combine the call_resumable
feature with the fuel system.
When the fuel expires, call_resumable
should return a Ok(ResumableCall::Resumable)
(or something similar) instead of an Err
.
This would make it possible for the API user to choose what to do: either stop the execution, or add more fuel and resume the execution. This provides maximum flexibility to this feature.
The API user could do things such as print a warning after a certain time, ask the human whether to continue or not, de-prioritize the thread executing the Wasm after a certain time, etc.