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

Draft: Hooking Hermes GC to Rust (do not merge) #10

Closed
wants to merge 1 commit into from

Conversation

jhugman
Copy link
Owner

@jhugman jhugman commented May 29, 2024

This changes the implementation of bless to a HostObject.

Ultimately, this commit may not make it to main, as it doesn't yet work.

This changes the implementation of `bless` to a HostObject.

Ultimately, this commit may not make it to main, as it doesn't yet work.
@jhugman jhugman marked this pull request as draft May 29, 2024 18:24
@jhugman
Copy link
Owner Author

jhugman commented May 29, 2024

This is leftover work from #9.

It is not ready to land: it should not need to go through the JS, but should likely go straight to Rust from the C++ destructor.

@jhugman
Copy link
Owner Author

jhugman commented Jul 30, 2024

Superceded by #46 .

@jhugman jhugman closed this Jul 30, 2024
jhugman added a commit that referenced this pull request Jul 31, 2024
Fixes #10 .

According to [The Big O of Code
Reviews](https://www.egorand.dev/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](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization)
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](facebook/hermes#1440).
@jhugman jhugman deleted the jhugman/gc-objects-ii branch November 3, 2024 19:24
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.

1 participant