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 destructors to Kotlin and Swift #8

Closed
linabutler opened this issue Jun 24, 2020 · 6 comments · Fixed by #196
Closed

Add destructors to Kotlin and Swift #8

linabutler opened this issue Jun 24, 2020 · 6 comments · Fixed by #196
Assignees
Labels
blocks-nimbus Blocks Nimbus SDK on mobile get involved Good opportunity for deeper-dive onboarding

Comments

@linabutler
Copy link
Contributor

linabutler commented Jun 24, 2020

The Python bindings already define __del__ destructors for classes, but Kotlin and Swift don’t yet. We’ll want to make sure we wire these up and call them correctly, to avoid leaks and double-frees.

┆Issue is synchronized with this Jira Task

@linabutler linabutler added the get involved Good opportunity for deeper-dive onboarding label Jun 24, 2020
@rfk
Copy link
Collaborator

rfk commented Jun 24, 2020

Our hand-written Kotlin bindings don't have destructors either, and I'm told that's on purpose because they're generally considered bad form in Java (for good reasons that I don't think apply in our case). I believe there's some elaborate infrastructure around the Cleaner class and its helpers that we're supposed to use instead but I haven't investigate it in any detail.

Which isn't to say that we shouldn't do it, just to brain-dump some thoughts on how.

@linabutler
Copy link
Contributor Author

linabutler commented Jun 28, 2020

Ooooh, I think the Kotlin equivalent would be the AutoCloseable interface that’s mentioned in the Cleaner documentation! Swift’s deinit works more like a destructor than a GC finalizer, so there’s a closer match there in the semantics.

@thomcc
Copy link

thomcc commented Jun 28, 2020

Yeah, I agree. That's what all our existing code does. I think if the a-c team wants to do the cleaner infra. They should probably own that (or at least let us know), AFAIK the autocloseable version has been working fine for them.

@eoger eoger added the blocks-nimbus Blocks Nimbus SDK on mobile label Jul 29, 2020
@data-sync-user data-sync-user changed the title Add destructors to Kotlin and Swift SYNC-1565 ⁃ Add destructors to Kotlin and Swift Jul 30, 2020
@eoger eoger reopened this Jul 30, 2020
@jhugman jhugman self-assigned this Jul 30, 2020
@data-sync-user data-sync-user changed the title SYNC-1565 ⁃ Add destructors to Kotlin and Swift Add destructors to Kotlin and Swift Jul 30, 2020
@jhugman
Copy link
Contributor

jhugman commented Jul 31, 2020

In Java 8/Kotlin/Android, the java.lang.ref.Cleaner (the preferred way of doing this) isn't available.

The evil way is the Object.finalize. Effective Java would have us believe that there are only two reasons we should do this, and this is one of them.

But Glean's experience would show that this is a source of crashes (via Sentry), and they went and removed it. This bit from @badboy scared me most:

their implementation in old Android versions is buggy, which means it's very easy to run into "timeout exceptions" because Android is misusing time.

So much for the magical ways on Android.

For the non-magical ways, the explicit cleanup method. So now we come to the second hardest problem: naming; specifically— name collisions.

Android-components is using Closeable; this does has the nice property of working with Closeable.use, but I'm not sure how useful that is. Additionally, aesthetically, I don't like close().

@linabutler
Copy link
Contributor Author

Yeah, at first I was thinking we would implement {Auto}Closeable and rely on callers either using try-with-resources for short-lived objects, or manually calling close for long-lived ones like, say, PlacesConnection.

You're totally right, though! Aesthetically, close makes sense for types like connections—but connections are more likely to be statics, only closed when the process shuts down. For short-lived objects, like the sprite example, I can't imagine writing, say, sprite.close(), gross! Firefox Desktop calls cleanup methods like this finalize, but just by convention; they're still called explicitly...unlike Java where finalize is inherited and called by the GC. Maybe destroy is a good compromise for the name?

I don't think WebIDL has a concept like this, either—deleter in WebIDL means something totally different. We could maybe have a [Destructor=destroy] annotation on the interface itself, where destroy lets you set the destructor name (and in Swift and Python, we'd ignore those and generate deinit and __del__ methods). Or we just stick with calling it destroy in Kotlin, and add it to our list of banned names (#10).

Using a name like destroy gives up on try-with-resources, but a-c can always make a wrapper that implements AutoCloseable and calls destroy inside close if they really want.

@rfk
Copy link
Collaborator

rfk commented Aug 3, 2020

this does has the nice property of working with Closeable.use, but I'm not sure how useful that is

I'm not aware of any concrete uses of this in practice (but then again, there are many things of which I'm not aware). It may be modestly useful for test code that creates and destroys a bunch of instances.

We could maybe have a [Destructor=destroy] annotation on the interface itself, where destroy lets you set the destructor name
(and in Swift and Python, we'd ignore those and generate deinit and del methods). Or we just stick with calling it destroy in
Kotlin, and add it to our list of banned names (#10).

FWIW, I feel like at this stage we should be pretty strongly opinionated about the shape of the generated code, and just declare a name for the destructor rather than trying to let consumers configurate-all-the-things. Naming it destroy until we have a reason not to seems 👍 to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocks-nimbus Blocks Nimbus SDK on mobile get involved Good opportunity for deeper-dive onboarding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants