tessi / wasmex

Execute WebAssembly from Elixir
MIT License
546 stars 31 forks source link

Provide pid in imports callback context #622

Closed ionull closed 1 month ago

ionull commented 1 month ago

Then we can call exported functions in imported functions.

ionull commented 1 month ago

Sorry, there must be some other way to do this: ** (EXIT) process attempted to call itself

ionull commented 1 month ago

When I call the exported function in the imports function callback, there is a timeout exception. Any idea?

tessi commented 1 month ago

hey @ionull - I believe I know where the timeout comes from. My suspicion is that this is a rust-side mutex the code it waiting on indefinitely that causes the timeout in elixir-land.

What happens when rust calls into an elixir function is that we have an unlocked mutex on the store_or_caller (the place we all wasm state is stored). That mutex stays locked during the call. Now when you, in elixir-land, call back into rust we need to access that same store_or_caller to mutate the wasm state, trying to unlock that same mutex. But it will never unlock, because we're still running the imported elixir-function.

ionull commented 1 month ago

Hi @tessi , the problem is that GenServer has not replied, and the status is locked on the GenServer side, where you can't call with pid, but we still can use the instance to call the wasm exported function like this: Wasmex.Instance.call_exported_function(store, instance, "__demo_export_0", [a,b],:from) receive do {:returned_function_call, {:ok, [result]}, :from} -> :ok result after 1000 -> raise "timeout on exported function call" end It's quite useful if we can get pid and instance from callback context.

tessi commented 1 month ago

uhh, interesting. so I was wrong. Nice to learn something :) I get why this is useful. Could you add a test for this scenario, please, so we can make sure not to break it in the future?

ionull commented 1 month ago

@tessi Hi, test case done.

tessi commented 1 month ago

@tessi Hi, test case done.

perfect! the case looks great to me. thanks a lot. let's see what CI says and merge when/if it's green :)

ionull commented 1 month ago

Is there anything else that I should do?

tessi commented 1 month ago

I wanted to say no, but the formatter is still upset. can you make sure mix format --check-formatted works? probably running mix format and a new commit is enough. Other than making formatters happy, I'm happy to merge :)

ionull commented 1 month ago

@tessi oh, I see. Have done the mix format, please check

tessi commented 1 month ago

@tessi oh, I see. Have done the mix format, please check

Thanks! And thanks again for your work 💚