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

LeaksTest #2051

Merged
merged 2 commits into from
May 29, 2024
Merged

LeaksTest #2051

merged 2 commits into from
May 29, 2024

Conversation

swankjesse
Copy link
Collaborator

@swankjesse swankjesse commented May 23, 2024

I recently worked on explicitly breaking reference cycles to prevent objects from leaking when we mix garbage-collected Kotlin objects with reference-counted Swift objects.

But this work was likely to regress because we didn't have a mechanism to prevent these reference cycles from recurring.

This PR introduces a bunch of machinery to explicitly test for reference cycles. It works on the JVM because that's a capable platform for this kind of dynamic analysis, and because it's consistent with the Kotlin/Native platform that is actually where we need to defend against for leaks.

The first new class is JvmHeap, which lazily inspects an object for its outbound references. This correctly captures the Kotlin compiler-inserted indirect references. It also includes lots of special cases to avoid traversing into implemnetation details of kotlinx.serialization and coroutines.

The second new class is CycleFinder, which is a basic Dijkstra breadth-first search. It's one neat feature is that it can reconstruct the list of properties that participate in a cycle. The cycle in LeaksTest (before the widget is removed) is:

     * callHandler
       * endpoint
         * inboundServices[3]
           * value
             * service
               * viewOrNull
                 * mutableListChildren
                   * container[0]
                     * onChange
                       * receiver
                         * eventSink
                           * delegate

This PR adds a test for widgets leaking. In a follow-up PR I intend to add tests for other interfaces and extension points in Redwood that can be implemented in Swift. This includes event listeners, Zipline services, TreehouseContentSource, and CodeListener.

val album = Album(
name = "Lateralus",
artist = Artist("Tool", 1990),
releaseYear = 2001,
Copy link
Collaborator

Choose a reason for hiding this comment

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

🎸

redwood-treehouse-host/build.gradle Show resolved Hide resolved
Base automatically changed from jwilson.0521.treehouse_tester to trunk May 29, 2024 19:22
I recently worked on explicitly breaking reference cycles to
prevent objects from leaking when we mix garbage-collected
Kotlin objects with reference-counted Swift objects.

But this work was likely to regress because we didn't have a
mechanism to prevent these reference cycles from recurring.

This PR introduces a bunch of machinery to explicitly test
for reference cycles. It works on the JVM because that's a
capable platform for this kind of dynamic analysis, and
because it's consistent with the Kotlin/Native platform that
is actually where we need to defend against for leaks.

The first new class is JvmHeap, which lazily inspects an object
for its outbound references. This correctly captures the Kotlin
compiler-inserted indirect references. It also includes lots
of special cases to avoid traversing into implemnetation details
of kotlinx.serialization and coroutines.

The second new class is CycleFinder, which is a basic Dijkstra
breadth-first search. It's one neat feature is that it can
reconstruct the list of properties that participate in a cycle.
The cycle in LeaksTest (before the widget is removed) is:
     * callHandler
       * endpoint
         * inboundServices[3]
           * value
             * service
               * viewOrNull
                 * mutableListChildren
                   * container[0]
                     * onChange
                       * receiver
                         * eventSink
                           * delegate

This PR adds a test for widgets leaking. In a follow-up PR I intend
to add tests for other interfaces and extension points in Redwood that
can be implemented in Swift. This includes event listeners, Zipline services,
TreehouseContentSource, and CodeListener.
@swankjesse swankjesse force-pushed the jwilson.0523.leak_tester branch from bbf69ed to 7167675 Compare May 29, 2024 19:24
@swankjesse swankjesse merged commit 3988a9b into trunk May 29, 2024
9 checks passed
@swankjesse swankjesse deleted the jwilson.0523.leak_tester branch May 29, 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.

3 participants