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

Add hooks into HostObject destructors #46

Merged
merged 1 commit into from
Jul 31, 2024
Merged

Conversation

jhugman
Copy link
Owner

@jhugman jhugman commented Jul 30, 2024

Fixes #10 .

According to The Big O of Code Reviews, this is a O(n) change.

This PR adds C++ destructor hooks from uniffi interface objects, to call the corresponding Rust drop methods.

The impedance mismatch between a GC'd language and an RAII language means that one is a leaky abstraction for the other.

From facebook/hermes#982 (comment):

Generally speaking, when dealing with native resources, it is
recommended to free the native resource explicitly when you are done
with it, because garbage collection is unpredictable and should not be
relied on for freeing native resources timely."

From the HostObject's destructor docs:

The C++ object's dtor will be called when the GC finalizes this
object. (This may be as late as when the Runtime is shut down.)
You have no control over which thread it is called on. This will
be called from inside the GC…

For this reason, the uniffiDestroy method is left in place for now: if you absolutely need to drop a Rust peer object, then you should call the uniffiDestroy method of the correspond JS object.

From the integration tests alone, it is difficult to show that this is working; they are too short-lived for the Runtime to be still running and garbage collection to have taken place.

However, the test_callbacks.ts did cause an ungraceful shutdown when the destructor called into Rust which then called back into JS– which had already been killed.

The situation will only really get better when React Native/Hermes supports a robust FinalizationRegistry implementation.

if (rsLambda == nullptr) {
// This only occurs when destructors are calling into Rust free/drop,
// which causes the JS callback to be dropped.
return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

clever

Copy link
Collaborator

@zzorba zzorba left a comment

Choose a reason for hiding this comment

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

LGTM

@jhugman jhugman merged commit 838ff68 into main Jul 31, 2024
@jhugman jhugman deleted the jhugman/iii-gc-objects branch July 31, 2024 13:59
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.

2 participants