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

nodejs: Add a test for callback garbage collection #3864

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tronical
Copy link
Member

@tronical tronical commented Nov 6, 2023

Replaces #3772
Closes #3706

Copy link
Member

@ogoffart ogoffart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there was no leak at all after all?

@tronical
Copy link
Member Author

tronical commented Nov 6, 2023

Yes, at least I couldn't find it. But what's weird is that I don't quite understand how / why it works. But if the test looks sane to you, too, then that's the best thing we have for now to verify that we're not leaking.

@ogoffart
Copy link
Member

ogoffart commented Nov 6, 2023

Is there a test that doesn't do demo = null? Because that's not what i'd expect in real code. I'd use demo.hide() and let demo go out of scope.

demo = null;
callback_invoked = true;
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you add something like this:

Suggested change
function scope() {
let copy = demo;
copy.say_hello = () => {
console.log(copy.check);
};
}
scope();

i.e: add another callback that use a property from some other variable with a small scope.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That did the trick, good idea. Pushed, now the test fails.

Comment on lines 80 to 81
let demo = loadFile(path.join(__dirname, "resources/test-constructor.slint")) as any;
let callback_invoked = false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid bug here: The instance is never created! The my_callback assignments happen to work because well, it's just a JS object. And because we don't have type safety, the compiler didn't warn us. So the test never really tested what it should have: instantiate the Rust code that presumably keeps a strong ref.

@tronical
Copy link
Member Author

tronical commented Nov 6, 2023

Note to future self:

  1. Show
  2. GC should not collect
  3. Hide
  4. GC should collect

@tronical
Copy link
Member Author

Rebasing this first and then trying to find out why it passes in the CI (but fails, as expected locally).

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

Successfully merging this pull request may close these issues.

Fix memory leak with callback closures in napi
3 participants