weBIGeo / webigeo

Rendering system for weBIGeo.
GNU General Public License v3.0
4 stars 0 forks source link

Screen freezes on mouse inputs #25

Closed pkomon-tgm closed 4 months ago

pkomon-tgm commented 4 months ago

Occasionally, mouse inputs causes the app to become unresponsive and freeze.

This issue is much easier to reproduce when the dev tools (Google Chrome) are open, however it seems to also occur without them sometimes.

To reproduce

  1. host WASM build (debug) locally
  2. perform mouse inputs like pressing down the left mouse button while tiles are still loading (easier in debug mode as it takes longer for tiles to load, even easier when console is open since its also slowing down the wasm build)
  3. app stutters or freezes entirely, console displays following error message
webgpu_app.js:468 Uncaught RuntimeError: Aborted(Assertion failed: Cannot have multiple async operations in flight at once)
    at abort (webgpu_app.js:466:41)
    at assert (webgpu_app.js:249:5)
    at Object.whenDone (webgpu_app.js:5801:5)
    at Object.mouse_button_event (eval at newFunc (webgpu_app.js:5569:23), <anonymous>:16:37)
    at webgpu_canvas.onmousedown (webgpu_app.html:163:18)
pkomon-tgm commented 4 months ago

This issue is a limitation of emscripten's Asyncify feature, which allows WASM code execution to be paused and resumed (and therefore behave like an async function in JS). Asyncify only allows for a single async call to be executed at a time, however.

Our issue arises when we send a request (using Qt's QNetworkRequest, which I am assuming, is using Asyncify too) while also a mouse event happens. We have custom mouse handlers in place in our custom emscripten shell here

https://github.com/weBIGeo/webigeo/blob/1133ef3a6f162824429fbb39c6ffb2c92ed1bdf7/webgpu_app/shell/webgpu_app.html#L159-L175

where we also call back into C++ code. This is also an async call, and finally triggers our error. This theory is supported by the fact that we don't use custom mouse event handlers on AlpineMaps, where this issue does not arise.

Emscripten has a section in its documentation detailing the issue: https://emscripten.org/docs/porting/asyncify.html#reentrancy

Still have to figure out a reasonable, non-hacky workaround.

pkomon-tgm commented 4 months ago

Using emscripten's ccall and async/await functions fixes this issue.

Fixed in f2431fcbd7e832e0cd920dceb1c77168ab2f848d (mouse, canvas resize) and 13cc6e9142e04af355edb08a54d7324606f745e0 (touch).