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

What does instance.clone() actually do? #2796

Closed
RobDavenport opened this issue Feb 19, 2022 · 4 comments
Closed

What does instance.clone() actually do? #2796

RobDavenport opened this issue Feb 19, 2022 · 4 comments
Labels
❓ question I've a question!

Comments

@RobDavenport
Copy link

RobDavenport commented Feb 19, 2022

Summary

I'm attempting to implement a roll-back system for currently-running instances. I see that instance supports a .clone() function but I'm not too sure what exactly is happening inside of there. Does cloning an instance simply create a new pointer/reference to the already-existing instance? Or does it perform a copy of the stateful instance and generate a new one? If it is not the latter, how can this be accomplished?

Additional details

Ideally, my code functions in the following way: constantly calling a wasm defined update() function and saving that stateful instance in memory somewhere. Should a rollback be required, load that previously executing instance, then continue call update() again with new values, and re-write the old states. Is there a way I can clone an already-running instance (including it's full state), to be able to run or resume it again later? Is deep copy the correct term here?

Alternatively, would cloning Store potentially work in this case, or does it run into the same issue where it's still pointing to the same underlying instances? Would there be a way I could just memcpy (either a state or instance's) active memory and move/copy values in and out of the instance?

Could be related to - #480

@RobDavenport RobDavenport added the ❓ question I've a question! label Feb 19, 2022
@jcaesar
Copy link
Contributor

jcaesar commented Feb 22, 2022

Does cloning an instance simply create a new pointer/reference to the already-existing instance?

Yes

@RobDavenport
Copy link
Author

I was able to rollback my wasm instance state by storing the memories and globals prior to rollback and writing directly to them after rolling back (with a few cases to handle differing sizes).

@jcaesar
Copy link
Contributor

jcaesar commented Feb 23, 2022

I suspect that works in some cases. You might also have to consider tables. Other than that: How do you handle calls from (start …)? Install a middleware that removes them?

(I've thought about this at some point, but gave up, because you can't save state if the wasm stack isn't empty (and I suspect it's generally impossible if you have stuff like Host -> WASM -> Host -> WASM -> save-function))

@RobDavenport
Copy link
Author

The module is only ever created once. Rollbacks when occurring only happen when the WASM stack is empty, and saving/loading of these states is determined by the host when triggered due to network conditions. So I'm not really creating any new instances or modules, it's more like hot-swapping the memory for this already-running instance to rollback and continue with correct inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓ question I've a question!
Projects
None yet
Development

No branches or pull requests

2 participants