Skip to content

Closure gets called after it's dropped #3178

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

Closed
schrottkatze opened this issue Dec 6, 2022 · 1 comment
Closed

Closure gets called after it's dropped #3178

schrottkatze opened this issue Dec 6, 2022 · 1 comment
Labels

Comments

@schrottkatze
Copy link

Describe the Bug

A closure passed into a promise that got dropped on the rust side gets called anyway.

Steps to Reproduce

  1. Use this code as an ondrop handler.
    let drop_handler = { 
        move |e: DragEvent| {
            let c = Closure::new(|v| {
                debug!("handler called! v: {:#?}", v);
            });
            e.prevent_default();
            debug!("Drop ev: {:#?}", 
                e.data_transfer()
                    .unwrap()
                    .files()
                    .unwrap()
                    .item(0)
                    .unwrap()
                    .text()
                    .then(&c)
            );
        }
    };
  1. Drop a file there

Expected Behavior

The compiler doesn't allow this code to compile, as only a reference to c is passed to .then(), the value behind which being dropped immediately after. The closure gets called anyway.

Actual Behavior

The code compiles, leading to the error message below.

Uncaught (in promise) Error: closure invoked recursively or destroyed already

Additional Context

This is happening inside of a yew project, but I suspect this is an issue with web-sys.

@Liamolucko
Copy link
Collaborator

This is expected. It'd be impossible to enforce at compile time that JS never calls a closure after it's dropped, since there's nothing stopping JS from just holding onto it for the rest of the program. So, this is the best we can do, just throwing an error if the closure gets mistakenly called after being dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants