rapier.js icon indicating copy to clipboard operation
rapier.js copied to clipboard

Crashes iOS Safari and iOS Chrome

Open canadaduane opened this issue 3 years ago • 10 comments

I notice that the rapier.rs 3d demo crashes iOS Safari (14.0.1) and iOS Chrome (87).

Unfortunately, it crashes the browser (no console logs, no crash traces), so it's difficult to say what's causing the problem.

The error message in Safari is "A problem repeatedly occurred on [url]". In Chrome the error message is "Can't open this page" with an "aww snap" crash icon ().

canadaduane avatar Feb 28 '21 16:02 canadaduane

Can confirm this bug.

I've written a small rapier.rs/webgl app, a dice-throwing simulator for a webrtc video chat. Anytime someone threw dice, a certain person got disconnected. Turns out they were using safari, and something crashed and reloaded the page. They've since switched to firefox where it works fine.

I wrote the WebGL renderer from scratch, which means that I'm probably triggering different render paths than the rapier 3d demo, and other webgl apps work fine for them. Maybe a WASM bug in safari? I didn't debug further as I don't own any apple devices.

  "dependencies": {
    "@dimforge/rapier3d-compat": "^0.3.1",
    "gl-matrix": "^3.3.0"
  },

cauthmann avatar Apr 05 '21 11:04 cauthmann

I started investigating the bug. The iOS crash appears to originate from a segfault on WebKit. Right now, we optimize the Rapier WASM files using the wasm-opt options: -O4 --enable-mutable-globals. Removing --enable-mutable-globals appears to fix the problem.

I'm actually not sure why we have --enable-mutable-globals in the first place. I will take a closer look tomorrow. If we don't need it, I'll remove that option and make a new release that should work on safari iOS.

sebcrozet avatar Apr 05 '21 16:04 sebcrozet

It looks like we needed --enabled-mutable-globals because of an issue in wasm-bindgen which has been fixed recently: https://github.com/rustwasm/wasm-bindgen/pull/2391 So we can safely remove this flag now and make a working iOS version.

sebcrozet avatar Apr 06 '21 07:04 sebcrozet

Interestingly, our WASM demos continue to crash on iOS when they are packaged by webpack. If I use webpack --debug then it works fine. I am not very experienced with webpack but I guess this problem isn't on our side? Does anyone already experienced a situation where the app works fine with webpack --debug but not webpack?

For the time being, I simply published our demos built using webpack --debug. I release Rapier 0.5.3 with the fix.

sebcrozet avatar Apr 06 '21 08:04 sebcrozet

Cross-posting my comment from Discord here:

I can see that the demo is working on iPad, but I can't seem to get my app to build in such a way that it works. Even with the webpack --debug flag, I still get the iOS crash. Are you using webpack v4 or v5? (I'm on v4 still).

Also, reflecting a bit, I'm a bit uncertain as to why webpack --debug would fix anything, as my understanding is that it simply adds more logging information to console while compiling.

Update: the --debug flag is also no longer valid in webpack 5.

canadaduane avatar Apr 07 '21 03:04 canadaduane

I updated to 0.5.3, using rapier3d-compat (thus there's no bundler touching the WASM), but my Safari using friend still gets kicked out the moment dice start rolling - but now they can see the dice for a split second before the tab crashes. Improvements I guess?

Sorry I cannot debug further.

cauthmann avatar Apr 15 '21 18:04 cauthmann

So in our case, an additional cause of trouble was our use of TerserPlugin to minify our javascript. It looks like Terser does something to the rapier3d glue code between our app and wasm--still uncertain what, exactly, it is doing to cause problems. But this narrows the problem sufficiently that we can at least get a production build working by excluding the dimforge/rapier3d chunk from Terser (it was always a chunk, anyway, because it must use a dynamic import). Here is how we configured webpack 4:

  // ... snip ...
  optimization: {
    /**
     * We need a separate chunk for @dimforge/rapier3d so that TerserPlugin can
     * exclude it from optimization, so that the physics engine wasm bundle will
     * work on iOS devices. See https://stackoverflow.com/a/58133835/159344.
     */
    splitChunks: {
      cacheGroups: {
        dimforge: {
          test: /[\\/]node_modules[\\/](@dimforge)[\\/]/,
          name: "dimforge",
          chunks: "all",
        },
      },
    },
    minimizer: [
      new TerserPlugin({
        // Exclude physics engine glue javascript (necessary for iOS devices)
        exclude: "dimforge",
      })
    ],
  }
// ...

canadaduane avatar Apr 29 '21 17:04 canadaduane

I can confirm. I use Rollup, instead of Webpack, though. I just tried to run my test project on iOS 14.2 / Safari, and it crashes if Terser is used. I do not use rapier-compat, though, so there is no Wasm injected as string, only the Wasm glue file and the application code.

LeXXik avatar Apr 29 '21 20:04 LeXXik

@LeXXik Are you able to share your working Rollup configuration for wasm, specifically? I filed an issue that has since been closed as "stale" regarding my inability to get rapier3d working with rollup. Very curious to know how you got it working.

canadaduane avatar Apr 30 '21 00:04 canadaduane

@canadaduane I will reach you over Discord, to keep the issue on topic.

LeXXik avatar Apr 30 '21 08:04 LeXXik