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

Fix GC for references and hold parameter references over async await

Open pando-fredrik opened this issue 2 years ago • 2 comments

The PR ( https://github.com/rustwasm/wasm-bindgen/pull/3562 ) removed the wrapper generation which was also responsible for registering to the JS FinalizationRegistry to garbage collect to run the clean up code when a GC occurred.

This PR reenables the registration to allow reference types to properly garbage collect, however there was also an issue where objects was garbage collected (wasm-bindgen 0.2.87 and below) where an WASM object if passed as a parameter to an asynchronous method was garbage collected while it was awaited, 0.2.88/0.2.89 does not exhibit this problem as the garbage collection was broken (and thus leaks memory instead), this PR introduces a small runtime wrapper for asynchronous calls that makes sure the variables stay in scope during the function call.

The code generation is hard to follow so I'm not confident this is the preferred solution and are open to changes and pointers to improve the patch.

pando-fredrik avatar Dec 13 '23 11:12 pando-fredrik

Unfortunately I'm not even able to start the test suite that fails as it crashes on my computers (even on the main branch). Pointers would be welcome.

pando-fredrik avatar Dec 14 '23 06:12 pando-fredrik

Update, I eventually found I missed to set WASM_BINDGEN_SPLIT_LINKED_MODULES before starting the tests.

And I also noticed that mut refs break with this patch (which the tests correctly identified, although with a rather vague error).

The reason is identified (but not yet solved), the issue seems to be that in some circumstances (I believe when mut refs are used an accessor method is injected to return the actual underlying object which breaks with this patch in its current shape or form)

I still hope someone can chime in on a way better solution to this (perhaps the PR should be split too as it tries to solve two problems, the finalization registration alone seems to work fine; but leaves our original problem, which was the sole reason I started looking into this and noticed the broken GC- as we were using wasm-bindgen 0.2.87 and after upgrade to 0.2.89 the GC problem was gone, which we found was because objects weren't simply wasnt GCed)

pando-fredrik avatar Dec 14 '23 14:12 pando-fredrik

Superseded by #3940.

Liamolucko avatar Jun 01 '24 23:06 Liamolucko