bootsharp
bootsharp copied to clipboard
Research .NET runtime WASM module lifecycle
It is not clear whether the .NET runtime WASM module have to be terminated and/or if it can be re-initialized.
There appears to be emscripten_force_exit
function in the module, but it throws an error:
https://github.com/Elringus/DotNetJS/blob/97647de97a9ffe0aafdc1941a947f79f9816b9d8/JavaScript/dotnet-runtime/src/wasm.ts#L25
Is someone have any related info, please let us know here.
WASM that is not in use should be destroyed. If not, the Mono VM will always be running in the background. EmscriptenModule cannot be reused, so a MONO VM cannot be reinitialized after it is created. However, you can create a new EmscriptenModule.
In my project: https://github.com/Aloento/FFmpeg4JS/blob/02832f0c5765d78b3d0125e7698516bbd75b433b/src/ffmpeg.js#L5230 https://github.com/Aloento/FFmpeg4JS/blob/02832f0c5765d78b3d0125e7698516bbd75b433b/src/ffmpeg.js#L390
I usually let WASM run in the worker, it's simpler, you just need worker.terminate();
WASM that is not in use should be destroyed.
Yeah, I felt that way as well, but as mentioned it throws an error on exit. Maybe something on the mono side.
You can try to call function exitRuntime()
in your env like:
https://github.com/Aloento/FFmpeg4JS/blob/02832f0c5765d78b3d0125e7698516bbd75b433b/src/ffmpeg.js#L1047
Or could you share the detail for this error?
I've tried it again, but looks like emscripten_force_exit
has been removed since I've last tested this. Not sure what's the correct way to shutdown/dispose the mono runtime now.
I don't think you need to close MONO, but to exit EmscriptenModule. Just exit the EmscriptenModule then MONO will dispose too.
Given emscripten_force_exit
is not exported, mono is probably built without the exit flag. Not sure if it's possible to terminate by other means. https://emscripten.org/docs/api_reference/emscripten.h.html#c.emscripten_force_exit
It's easy to export like: https://github.com/Aloento/FFmpeg4JS/blob/02832f0c5765d78b3d0125e7698516bbd75b433b/src/ffmpeg.js#L5123
eg:
Module["exitRuntime"] = exitRuntime;
Right, so it's actually built with no exit flag: https://github.com/Elringus/DotNetUMD/blob/6807d5ceeb52f9421802985d3737479406c95b08/src/mono/wasm/wasm.proj#L67
Guess that means it can't be directly disposed. We could override the flag in our custom branch, but due to #20 I want to refrain from further patches. Maybe they'll change it in .NET 7.
Blazor certainly doesn't exit, as it runs through the entire page lifecycle and will not change in future versions I think. So you're better off building a version yourself.
It's .NET runtime for wasm, which is not just for blazor, so there's still a chance. They've already made a lot of progress to modularize it in .NET 6-7. And again, I'm planning to opt-out of custom runtime build by .NET 7 release, so patching it further is not desired.