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

API Ergonomics with StoreMut Handling #5371

Open
anlumo opened this issue Jan 31, 2025 · 0 comments
Open

API Ergonomics with StoreMut Handling #5371

anlumo opened this issue Jan 31, 2025 · 0 comments
Labels
❓ question I've a question!

Comments

@anlumo
Copy link

anlumo commented Jan 31, 2025

Summary

Calling wasm functions requires a StoreMut<'_>. This leads to me having to wrap my wasm module in a Mutex<_> in order to get a mutable reference from it. This kinda sounds reasonable, since a module can modify its memory and thus needs exclusive access (and I can't run two functions in parallel from the same store).

In host functions, I get an impl AsStoreMut as the first parameter. This allows me to call wasm functions from inside the host function. This works fine, however:

If my host function calls another function that calls another function, the latter one doesn't have any idea that it's running inside a host function any more. If it wants to call a wasm function, it tries to lock the store again, because that's how it gets the store when it's called by someone else and that works fine. In these specific circumstances however, this leads to a deadlock, because the same thread tries to lock the same Mutex<_> again.

Every step on the way sounds reasonable, but in the context of the whole application, this causes a lot of problems. What I'm doing right now is to litter my code with Option<&mut StoreMut<'_>> parameters. If it's Some(_), use that one for calling wasm functions. If it's None, lock the store to get the mutable reference that way.

I don't have an easy solution, but this is highly annoying. I didn't have that architectural problem with wasmtime at all.

Additional details

The project I'm working on is proprietary and thus I can't show the code, but it's using this crate I created that contains the RwLock<_> in Context in context.rs.

@anlumo anlumo added the ❓ question I've a question! label Jan 31, 2025
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

1 participant