Skip to content
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

🐛 BUG: Uncaught ReferenceError: FinalizationRegistry is not defined #2258

Closed
kevi-sun opened this issue Nov 22, 2022 · 3 comments
Closed
Labels
bug Something that isn't working

Comments

@kevi-sun
Copy link

What version of Wrangler are you using?

2.4.2

What operating system are you using?

Linux and Mac

Describe the Bug

We have javascript call the wasm code, with WASM_BINDGEN_WEAKREF defined to generate code to register to javascript garbage collector, please see https://rustwasm.github.io/wasm-bindgen/reference/weak-references.html for detail.

here is an example of code generated by wasm-bindgen:

export class StreamWrapper {
    static __wrap(ptr) {
        const obj = Object.create(StreamWrapper.prototype);
        obj.ptr = ptr;
        StreamWrapperFinalization.register(obj, obj.ptr, obj);
        return obj;
    }

    __destroy_into_raw() {
        const ptr = this.ptr;
        this.ptr = 0;
        StreamWrapperFinalization.unregister(this);
        return ptr;
    }

    free() {
        const ptr = this.__destroy_into_raw();
        wasm.__wbg_streamwrapper_free(ptr);
    }
}

it works well with running "wrangler dev --local", but the following error was report when publish to CloudFlare with wrangler2

✘ [ERROR] A request to the Cloudflare API (/accounts/00000000000000000000000/workers/scripts/worker_name) failed.

  Uncaught ReferenceError: FinalizationRegistry is not defined
    at worker.js:446:35
   [code: 10021]

  
  If you think this is a bug, please open an issue at:
  https://github.com/cloudflare/wrangler2/issues/new/choose
@kevi-sun kevi-sun added the bug Something that isn't working label Nov 22, 2022
@lrapoport-cf lrapoport-cf moved this to Untriaged in workers-sdk Nov 22, 2022
@shyim
Copy link

shyim commented Jan 10, 2023

Does it make sense to add a warning here? The error message does say nothing

@kevi-sun
Copy link
Author

@shyim thanks, it would be nice to support FinalizationRegistry which makes use of javascript GC, but I don't know whether it is supported in Cloudflare env and how to fix that.
I tested in miniflare, looks all good with WASM_BINDGEN_WEAKREF defined.

@mrbbot
Copy link
Contributor

mrbbot commented Jan 27, 2023

Hey! 👋 Thanks for raising this. I brought this up internally, and it seems like there are some technical restrictions why we can't implement FinalizationRegistry in the Workers runtime. We could do a better job at documenting this limitation, but for now, you'll need to build your code without WASM_BINDGEN_WEAKREF when targeting Workers. Miniflare should be updated to remove support for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working
Projects
None yet
Development

No branches or pull requests

3 participants