Resuming a program where it left off when it reached a resource limit
As I understand it, when a Limit is reached, an assertion triggers an exception, the execution halts and if one wants the results, the called function will have to be restarted again, with higher limits.
Would there be a way to implement suspension and resumption when this happens, so the runtime state isn't discarded, much like Stackless Python can: https://stackless.readthedocs.io/en/latest/library/stackless/pickling.html ?
One use case would be something along my https://rlc-chess.com project:
A P2P wireless environment is simulated. Nodes can send messages to other nodes within some radius. Each has a unique id and gets a special packet with the id of another node. The objective is for as many nodes as possible to deliver the packets to the right node. Participants can submit .wasm programs that are instantiated for each node and that try to route the packets correctly.
https://www.youtube.com/watch?v=Y8MvSAZ-YMk
I could do the same as with the chess competition and limit the number of instructions each node can use to process each packet it receives. But it would also be interesting for each node to divide up the time itself. If the interpreter can continue the execution at the exact same instruction and state where it left off, this could be done.
This is something the Life VM also supports: https://github.com/perlin-network/life/issues/52 Not sure how hard this would be to implement.
It might look like this
class Node:
def __init__(self):
self.runtime = pywasm.load(...)
self.result = None
nodes = [Node() for i in range(20)]
while True:
for node in nodes:
node.runtime.machine.opts.cycle_limit = 1000
# I guess instead of raising an assertion, a "soft" return would be required
if node.result.state == pywasm.RESOURCE_EXCEPTION:
node.result = node.runtime.resume()
else:
node.result = node.runtime.exec("process", [], suspend_on_resource_exception=True)
This would go through each node and run it for 1000 steps each, forever. The WebAssembly code itself could contain arbitrarily complex code/loops, since the scheduling is handled this way on the outside.