Repository navigation
feat(runtime): find the JS/Java cycles canary mode keeps alive (experimental) #2130
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
Merged
+1,513
−4
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
866148d
feat(runtime): find the JS/Java cycles canary mode keeps alive (exper…
edusperoni 3fadd8b
fix(runtime): leak probe review fixes
edusperoni 5114144
build(runtime): build the leak probe into debug runtimes only
edusperoni b5eae24
fix(runtime): report leak probe results as own properties
edusperoni 38f6c2e
fix(runtime): leak probe review fixes
edusperoni d660d8e
fix(runtime): leak probe reads a canary record's shell
edusperoni f2984ad
fix(runtime): leak probe reads a canary record's handles and its shell
edusperoni 865945e
fix(runtime): leak probe reads a canary record's weak-collection key
edusperoni 122279c
docs(runtime): leak probe reports cycles through plain wrappers under…
edusperoni c645dc5
build(runtime): ship the leak probe's Java side whenever its native s…
edusperoni aba8de6
test(runtime): check the leak probe's rebuilt-canary fixture was rebuilt
edusperoni abb763d
test(runtime): fail the leak probe's settling spec on any unsettled w…
edusperoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| describe("Wrapper lifetime leak probe", function () { | ||
| var runtime = require("ns:runtime"); | ||
| var mode = runtime.getConfig("wrapperLifetimeMode"); | ||
| var canary = mode === "canary"; | ||
| var Holder = com.tns.tests.WrapperHolder; | ||
| // WeakRefs, so the spec itself keeps nothing it planted alive. | ||
| var planted = []; | ||
| // Alive for the whole suite: what leaks is the entry's value. | ||
| var probeMap = new WeakMap(); | ||
| var report; | ||
| var canaryRebuilds; | ||
|
|
||
| function cyclesThrough(edge) { | ||
| return report.cycles.filter(function (cycle) { | ||
| return cycle.path.indexOf("--" + edge + "-->") !== -1; | ||
| }); | ||
| } | ||
|
|
||
| // Each probe dumps the Java heap, so every shape is planted once and probed once, on a | ||
| // later task: a WeakRef keeps its target alive until the job that made it ends. | ||
| beforeAll(function (done) { | ||
| // A cycle through Java whose object's canary was rebuilt since: the record also | ||
| // keeps a copy of the rebuilt canary, which must not read as a root. | ||
| (function () { | ||
| var rebuilt = new java.lang.Runnable({ run: function () {} }); | ||
| var list = new java.util.ArrayList(); | ||
| list.add(rebuilt); | ||
| rebuilt.probeRebuiltList = list; | ||
| planted.push(new WeakRef(rebuilt)); | ||
| Holder.held = list; | ||
| })(); | ||
| // The canary dies on a later job: a WeakRef keeps its target until the job ends. | ||
| setTimeout(function () { | ||
| gc(); | ||
| java.lang.System.nanoTime(); | ||
| java.lang.Runtime.getRuntime().gc(); | ||
| gc(); | ||
| java.lang.System.nanoTime(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| var rebuildsBefore = runtime.getWrapperLifetimeStats().canaryRebuilds; | ||
| (function () { | ||
| // A read: the rebuilt canary's copy stays current. | ||
| Holder.held.get(0).probeRebuiltList.size(); | ||
| })(); | ||
| canaryRebuilds = runtime.getWrapperLifetimeStats().canaryRebuilds - rebuildsBefore; | ||
| Holder.held = null; | ||
| (function () { | ||
| // A JS property referencing its own object. | ||
| var selfReferencing = new java.lang.Runnable({ run: function () {} }); | ||
| selfReferencing.probeSelf = selfReferencing; | ||
| planted.push(new WeakRef(selfReferencing)); | ||
|
|
||
| var twin = new java.lang.Runnable({ run: function () {} }); | ||
| twin.probeSelf = twin; | ||
| planted.push(new WeakRef(twin)); | ||
|
|
||
| // A cycle through Java: the object's state holds a list that holds the object. | ||
| var listed = new java.lang.Runnable({ run: function () {} }); | ||
| var list = new java.util.ArrayList(); | ||
| list.add(listed); | ||
| listed.probeList = list; | ||
| planted.push(new WeakRef(listed)); | ||
|
|
||
| // Held by a Java static: alive for a reason of its own, not a leak. | ||
| var held = new java.lang.Runnable({ run: function () {} }); | ||
| held.probeHeld = held; | ||
| Holder.held = held; | ||
| planted.push(new WeakRef(held)); | ||
|
|
||
| // A WeakMap value referencing its own key: in canary mode the key is the | ||
| // object's lifetime key, which its record holds. | ||
| var mapped = new java.lang.Runnable({ run: function () {} }); | ||
| probeMap.set(mapped, { probeMapped: mapped }); | ||
| planted.push(new WeakRef(mapped)); | ||
|
|
||
| // A back reference through a WeakRef keeps nothing. | ||
| var weak = new java.lang.Runnable({ run: function () {} }); | ||
| weak.probeWeak = new WeakRef(weak); | ||
| })(); | ||
| (function () { | ||
| // A method closing over its own object: the cycle runs through the object's | ||
| // prototype, its implementation object. In a function of its own, so no other | ||
| // planted closure shares the context that holds it. | ||
| var probeClosed = new java.lang.Runnable({ | ||
| run: function () { | ||
| return probeClosed; | ||
| } | ||
| }); | ||
| planted.push(new WeakRef(probeClosed)); | ||
| })(); | ||
| setTimeout(function () { | ||
| report = runtime.findLeakCycles(); | ||
| console.log("leak probe: " + JSON.stringify({ | ||
| leaked: report.leaked, | ||
| leakedOutsideCycles: report.leakedOutsideCycles, | ||
| probed: report.probed, | ||
| cycles: report.cycles.length, | ||
| durationMs: report.durationMs, | ||
| dumpBytes: report.dumpBytes | ||
| })); | ||
| report.cycles.forEach(function (cycle) { | ||
| console.log("leak probe cycle: " + cycle.signature); | ||
| }); | ||
| done(); | ||
| }, 0); | ||
| }, 0); | ||
| }, 20000); | ||
|
|
||
| afterAll(function () { | ||
| Holder.held = null; | ||
| planted.forEach(function (ref) { | ||
| var object = ref.deref(); | ||
| if (object !== undefined) { | ||
| __releaseNativeCounterpart(object); | ||
| } | ||
| }); | ||
| planted = []; | ||
| report = null; | ||
| }); | ||
|
|
||
| it("reports the heaps it joined (" + mode + ")", function () { | ||
| expect(report.mode).toBe(mode); | ||
| expect(Array.isArray(report.cycles)).toBe(true); | ||
| if (!canary) { | ||
| // Nothing to look for: no dump is taken. | ||
| expect(report.leaked).toBe(0); | ||
| expect(report.javaObjects).toBeUndefined(); | ||
| return; | ||
| } | ||
| expect(report.javaObjects).toBeGreaterThan(0); | ||
| expect(report.jsNodes).toBeGreaterThan(0); | ||
| expect(report.probed).toBeGreaterThan(0); | ||
| expect(report.truncated).toBe(false); | ||
| expect(report.durationMs).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("finds an object whose JS state references it", function () { | ||
| var cycles = cyclesThrough("probeSelf"); | ||
| if (!canary) { | ||
| // Resurrection breaks it: nothing keeps the object once JS lets go. | ||
| expect(cycles.length).toBe(0); | ||
| return; | ||
| } | ||
| expect(cycles.length).toBe(2); | ||
| expect(cycles[0].path).toContain("--ns:kept-->"); | ||
| expect(cycles[0].path).toContain("--ns:wrapper-->"); | ||
| expect(cycles[0].members.length).toBe(1); | ||
| }); | ||
|
|
||
| it("gives the same shape the same signature, free of ids", function () { | ||
| if (!canary) { | ||
| return; | ||
| } | ||
| var cycles = cyclesThrough("probeSelf"); | ||
| expect(cycles[0].signature).toBe(cycles[1].signature); | ||
| }); | ||
|
|
||
| it("finds a cycle through a Java collection", function () { | ||
| var cycles = cyclesThrough("probeList"); | ||
| if (!canary) { | ||
| expect(cycles.length).toBe(0); | ||
| return; | ||
| } | ||
| expect(cycles.length).toBe(1); | ||
| expect(cycles[0].path).toContain("--java.util.ArrayList.elementData-->"); | ||
| }); | ||
|
|
||
| it("finds a cycle through a WeakMap value that references its key", function () { | ||
| var cycles = cyclesThrough("probeMapped"); | ||
| if (!canary) { | ||
| expect(cycles.length).toBe(0); | ||
| return; | ||
| } | ||
| expect(cycles.length).toBe(1); | ||
| expect(cycles[0].path).toContain("--WeakMap value-->"); | ||
| }); | ||
|
|
||
| it("finds a cycle through a method that closes over its object", function () { | ||
| var cycles = cyclesThrough("probeClosed"); | ||
| if (!canary) { | ||
| expect(cycles.length).toBe(0); | ||
| return; | ||
| } | ||
| expect(cycles.length).toBe(1); | ||
| expect(cycles[0].path).toContain("--ns:kept-->"); | ||
| expect(cycles[0].path).toContain("--ns:wrapper-->"); | ||
| }); | ||
|
|
||
| it("finds a cycle through Java after the object's canary was rebuilt", function () { | ||
| var cycles = cyclesThrough("probeRebuiltList"); | ||
| if (!canary) { | ||
| expect(cycles.length).toBe(0); | ||
| return; | ||
| } | ||
| expect(canaryRebuilds).toBeGreaterThan(0); | ||
| expect(cycles.length).toBe(1); | ||
| }); | ||
|
|
||
| it("does not report an object Java holds for a reason of its own", function () { | ||
| expect(cyclesThrough("probeHeld").length).toBe(0); | ||
| }); | ||
|
|
||
| it("does not report a back reference through a WeakRef", function () { | ||
| expect(cyclesThrough("probeWeak").length).toBe(0); | ||
| }); | ||
|
|
||
| it("settles the wrappers that died since the last collection first", function () { | ||
| if (!canary) { | ||
| return; | ||
| } | ||
| (function () { | ||
| for (var i = 0; i < 50; i++) { | ||
| new java.lang.StringBuilder("probeGarbage"); | ||
| } | ||
| })(); | ||
| // Read as leaked if unsettled: their Java objects are still held for them. | ||
| expect(runtime.findLeakCycles().leaked).toBeLessThanOrEqual(report.leaked); | ||
| }, 20000); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
test-app/runtime/src/debugTools/java/com/tns/LeakProbe.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package com.tns; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| /** | ||
| * ns:runtime findLeakCycles()'s Java side, in debug runtimes only. During the heap dump, | ||
| * {@link #pinned} holds the Java objects the analysis maps to JS wrappers and states; the | ||
| * analysis finds that array through this static field by name and does not count it as a | ||
| * reference. | ||
| */ | ||
| final class LeakProbe { | ||
| static final String DUMP_PREFIX = "ns-leak-probe"; | ||
|
|
||
| // Older than any dump a probe of this process or another one may still be parsing. | ||
| private static final long STALE_DUMP_AGE_MS = 10 * 60 * 1000; | ||
|
|
||
| static Object[] pinned; | ||
|
|
||
| private LeakProbe() { | ||
| } | ||
|
|
||
| // Before the probe takes strong references: objects only weak entries hold would | ||
| // otherwise read as leaked until Java collects them. | ||
| static void collectGarbage() { | ||
| for (int i = 0; i < 2; i++) { | ||
| java.lang.Runtime.getRuntime().gc(); | ||
| } | ||
| } | ||
|
|
||
| // Writes an HPROF dump with objects pinned in order; returns its path, or null if it | ||
| // could not be written. The caller deletes the dump once parsed. | ||
| static String dump(Object[] objects) { | ||
| // pinned is shared by every runtime of the process. | ||
| synchronized (LeakProbe.class) { | ||
| deleteStaleDumps(); | ||
| File file = null; | ||
| try { | ||
| file = File.createTempFile(DUMP_PREFIX, ".hprof"); | ||
| pinned = objects; | ||
| android.os.Debug.dumpHprofData(file.getAbsolutePath()); | ||
| return file.getAbsolutePath(); | ||
| } catch (Throwable t) { | ||
| android.util.Log.w("JS", "findLeakCycles could not dump the Java heap", t); | ||
| if (file != null) { | ||
| file.delete(); | ||
| } | ||
| return null; | ||
| } finally { | ||
| pinned = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Dumps a killed process left behind. | ||
| private static void deleteStaleDumps() { | ||
| String tmp = System.getProperty("java.io.tmpdir"); | ||
| File[] files = tmp != null ? new File(tmp).listFiles() : null; | ||
| if (files == null) { | ||
| return; | ||
| } | ||
| long staleBefore = System.currentTimeMillis() - STALE_DUMP_AGE_MS; | ||
| for (File file : files) { | ||
| String name = file.getName(); | ||
| if (name.startsWith(DUMP_PREFIX) && name.endsWith(".hprof") | ||
| && file.lastModified() < staleBefore) { | ||
| file.delete(); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.