-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thrown exceptions do not restore the stack pointer #1959
Comments
Thanks for the report! I'm unfortunately able to reproduce this though. Can you share some more information? For example:
|
@SebastienGllmt In your example functions, the string is never sent to JS, it exists purely in Rust, and follows normal Rust semantics, so it will be dropped at the end of the statement, and so it cannot leak. So if you are seeing a memory leak, then that's a bug in Rust itself (or possibly the Wasm engine), not wasm-bindgen. How much RAM does your computer have? How much RAM does your Wasm program use before it crashes?
|
I made a super simple repo. You can find it here: https://github.com/SebastienGllmt/wasm-memory-test Happens on both latest Firefox ( Running Computer has 32GBs of RAM on a 64bit OS so it's definitely not an OS-level problem |
@SebastienGllmt Thanks. So, this has nothing to do with strings, even if I do this I still get the error: pub fn foo() -> Result<(), JsValue> {
Err(JsValue::from(0))
} As far as I can tell, there is no memory leak. A memory leak would require the memory usage to increase over time, but that's not happening here. The error message for indexing out of bounds means just that: it's indexing out of bounds. It would give a different error message if it had run out of memory, like this:
What I think is happening is that there is some sort of internal corruption because you're catching the error and then ignoring it. Basically, wasm-bindgen expects that after it throws an error the program will finish, but that's not happening in this case. |
Ah you're right maybe the issue is actually with After some additional digging I found this as part of the wasm-bindgen documentation
It seems the way that
which throws an exception in JS-land so presumably this is the same issue causing the stack not to unwind. If that's the case, what's the best way to implement something like |
Sorry I still can't seem to reproduce this. I checked out that repo and then executed const { memory } = require('./pkg/wasm_repro_bg');
const { WasmObject } = require('./pkg/wasm_repro');
let last = 0;
for (let i = 0; i < 100000000; i++) {
try {
WasmObject.foo();
} catch (e) {
const size = memory.grow(0);
if (size > last) {
console.log(size);
last = size;
}
}
} I don't see any memory growth so I'm not sure what's causing you to think that there's a leak. Is this perhaps a different bug where, when reduced, isn't showing the original bug? Destructors are tricky with wasm right now, but the examples given here should not be leaking memory. If you're using |
Ah ok I believe I see what's happening now. The wasm module isn't running out of memory, but it's running out of stack. When the exception is thrown no more wasm code runs, and this includes wasm code to restore the stack pointer. This is a fundamental design flaw in returning |
Ok I'm going to close this in favor of #1963 which is a bit more targeted and is only about the stack growth issue. |
Some more info below
Describe the Bug
When a
String
type is used the memory is never dropped on the WASM side. This is problematic because in our codebase we have functions likeso whenever
try_parse
fails, theformat!
call allocates memory that just piles up during runtime until eventually you run out of memory.Steps to Reproduce
Define a function such as
or
then you can call it in Javascript in this way
Expected Behavior
Loop completes without any errors
Actual Behavior
On my computer this will crash after
65525
iterations. If you remove the string allocation though it does not crash.Additional Context
Generates using
wasm-pack 0.8.1
and happens on bothnodejs
andbrowser
builds.The text was updated successfully, but these errors were encountered: