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

Using wasmer from goroutines #84

Closed
maxim-ge opened this issue Nov 17, 2019 · 7 comments
Closed

Using wasmer from goroutines #84

maxim-ge opened this issue Nov 17, 2019 · 7 comments
Assignees
Labels
📦 component-runtime About the Wasm runtime ❓ question Further information is requested

Comments

@maxim-ge
Copy link

Summary

What is the safe way to use wasmer from multiple goroutines?

Additional details

If instance (result of wasm.NewInstance) is used by only one goroutine at any time - is it enough? The potential problem is that one goroutine might be executed by few threads.

@maxim-ge maxim-ge added the ❓ question Further information is requested label Nov 17, 2019
@Hywan
Copy link
Contributor

Hywan commented Nov 18, 2019

Since wasmerio/wasmer#807, I would say it is safe to use Instance in multiple threads yes, but I'm double-checking with some other members of the team.

@Hywan Hywan self-assigned this Nov 18, 2019
@Hywan Hywan added the 📦 component-runtime About the Wasm runtime label Nov 18, 2019
@Hywan
Copy link
Contributor

Hywan commented Nov 18, 2019

Some precisions:

  • It is safe to use an instance from multiple threads (since Instance implements Send in the runtime, written in Rust),
  • It isn't safe to re-enter the Instance from a Go function called as an imported function of the same instance (since Instance doesn't implement Sync in the runtime).

@Hywan
Copy link
Contributor

Hywan commented Nov 18, 2019

https://golang.org/pkg/runtime/#LockOSThread can be useful for reentrance.

@maxim-ge
Copy link
Author

maxim-ge commented Nov 18, 2019

Thanks a lot! Can I re-conclude a bit?

  1. It is safe to use Instance from goroutines provided goroutines are synced (only one goroutine uses Instances at a time, but Instance can be safely re-used by another goroutine)
  2. It is not safe to re-enter Instance from imported functions even provided (1) is satisfied

Correct?

@Hywan
Copy link
Contributor

Hywan commented Nov 18, 2019

Instance can be used by any goroutines at a time, but cannot be sent to another goroutine.

@maxim-ge
Copy link
Author

Ok, https://golang.org/pkg/runtime/#LockOSThread does the job, thank you very much!

@Hywan
Copy link
Contributor

Hywan commented Nov 18, 2019

#84 (comment) isn't true. I didn't know that goroutines can run on a same thread or can be moved to another thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 component-runtime About the Wasm runtime ❓ question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants