brotli-wasm icon indicating copy to clipboard operation
brotli-wasm copied to clipboard

Fails when calling repeatedly

Open lowmelvin opened this issue 3 years ago • 1 comments
trafficstars

This currently gives me a Uncaught (in promise) RuntimeError: unreachable executed error when calling compress back-to-back after 100 or so iterations.

Uncaught (in promise) RuntimeError: unreachable executed
    compress brotli_wasm_bg.js:143
143: wasm.compress(retptr, ptr0, len0, addBorrowedObject(raw_options));

lowmelvin avatar Oct 22 '22 13:10 lowmelvin

Huh, weird! I can reproduce this myself too with:

const bw = require('brotli-wasm');
for (let i = 0; i < 1000; i++) { bw.compress(Buffer.from('test')); console.log(i); }

It fails after 123 compressions every time, seemingly in all node versions and browsers too.

I have no idea why that would happen I'm afraid, and I haven't seen it in real usage elsewhere, which makes this very surprising! The compress method here is just a tiny wrapper around the rust-brotli rust crate, and the error must be coming from inside there.

To get this fixed, we'll need to:

  • Work out how to get a full stack trace inside rust-brotli, to see what exactly is failing
  • Try to reproduce this with rust-brotli directly, without WASM etc, and see whether it's a result of the wrapper & build here somehow, or whether it's an upstream bug.

Any chance you've got time to look into either of those?

pimterry avatar Oct 24 '22 10:10 pimterry

Hey! Thanks for the reply. Unfortunately I don't have the bandwidth to take this on at the moment, and I also don't have experience with Rust or WASM. Hopefully someone more suitable will come along.

lowmelvin avatar Oct 25 '22 04:10 lowmelvin

Fair enough! I'm not super experienced in debugging issues like this either. I'll see if I can find time some day soon to look into it - in the meantime, if anybody else finds this issue and would like to get it fixed, help here is very welcome :smile:

pimterry avatar Oct 25 '22 10:10 pimterry

I'm also seeing this issue, and I'm seeing it occur when r0 goes over the signed 32bit limit (2_147_483_647) and wraps around to negative values in the decompress function.

RossWilliams avatar Oct 25 '22 20:10 RossWilliams

I dropped the wee allocator and was able to run 70 times more iterations than the previous failure point without issue. https://github.com/httptoolkit/brotli-wasm/pull/17

RossWilliams avatar Oct 25 '22 20:10 RossWilliams

I've tested that PR - it definitely resolves the repro above nicely, and I can now run that to at least 10,000 iterations with no problem (and it seems to run quite a bit quicker per iteration too, though I haven't properly measured it). Great fix from @RossWilliams, thanks so much.

I've just merged & shipped this, it's now published on npm as v1.3.1. Thanks for reporting this @rzqx! Do let me know if you're still having more issues in future.

pimterry avatar Oct 26 '22 11:10 pimterry