-
Notifications
You must be signed in to change notification settings - Fork 578
[Java.Interop] Fix JNI global-reference ownership leaks #12762
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
Closed
Closed
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
9c4067a
[Java.Interop] Release method-cache redirect references
simonrozsival 588c8fc
[Java.Interop] Dispose subclass caches without initializing the owner
simonrozsival cf8a268
Fix transferred JNI reference cleanup during activation
simonrozsival e8c1596
[runtime] Release temporary startup class reference
simonrozsival beff091
Dispose unpublished subclass constructor caches
simonrozsival c17a279
[Java.Interop] Preserve standalone native registration ownership
simonrozsival 94a8991
[Mono.Android] Avoid retaining canceled Action callbacks
simonrozsival 7e68e0e
[tests] Keep standalone registration fixture off Android
simonrozsival bea36b7
[tests] Make gref regressions reliable across test runtimes
simonrozsival 9cd00ba
[tests] Tolerate JDK-specific module constant pools
simonrozsival 32f9b47
[tests] Clean up bytecode test formatting
simonrozsival 37f9bbf
[Java.Interop] Keep empty native registration a no-op
simonrozsival addc53d
[Java.Interop] Preserve empty-registration formatting
simonrozsival efb7394
[Java.Interop] Explain method cache publication
simonrozsival 8af77b4
[Mono.Android] Avoid callback removal closure
simonrozsival c77de9a
[tests] Keep native registration coverage focused
simonrozsival b35b5cb
[Java.Interop] Explain native registration roots
simonrozsival 30ced49
[Java.Interop] Clarify registration retention naming
simonrozsival b43909a
[Java.Interop] Encapsulate method cache ownership
simonrozsival 8d5d846
[Java.Interop] Configure method cache at call sites
simonrozsival 0fad335
[Java.Interop] Size custom method caches explicitly
simonrozsival 6c49220
[Java.Interop] Dispose unused cache candidates
simonrozsival f2e6a46
[Java.Interop] Use Lock for native registrations
simonrozsival caf9a29
[Java.Interop] Reject repeated native registration
simonrozsival 4051811
[Java.Interop] Test redirect cleanup through public API
simonrozsival a5c8e7a
[Java.Interop] Generalize resource-owning cache
simonrozsival a90470a
[Java.Interop] Atomically claim native registration
simonrozsival 5fcfc48
[Java.Interop] Clarify repeated registration error
simonrozsival 341b06b
[Java.Interop] Keep registration ownership in JniType
simonrozsival 0ace082
[Java.Interop] Explain registration retention order
simonrozsival 2bc7c2a
[Java.Interop] Encapsulate runtime ownership cleanup
simonrozsival 13088c8
[Mono.Android] Avoid callback removal closures
simonrozsival 8c55d90
[Java.Interop] Delay registration ownership transfer
simonrozsival 2424be1
[Java.Interop] Pass native registration owner directly
simonrozsival 70c79a8
[Java.Interop] Track native registration before marshaling
simonrozsival 92eeb00
[Java.Interop] Fix cache ownership CI failures
simonrozsival ad5c018
[Java.Interop] Preserve repeated native registration
simonrozsival c498384
[Mono.Android] Keep runnable removal references valid
simonrozsival 379c1af
Merge origin/main into simonrozsival-gref-leak-audit
simonrozsival b7b008a
[Java.Interop] Synchronize JNI ownership teardown
simonrozsival 42194cf
[Java.Interop] Document member cache teardown contract
simonrozsival dc7dc71
[Java.Interop] Retain private native registration batches
simonrozsival da5b253
[Mono.Android] Preserve callback binding semantics
simonrozsival 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
114 changes: 114 additions & 0 deletions
114
external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniValueCache.cs
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,114 @@ | ||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Threading; | ||
|
|
||
| namespace Java.Interop { | ||
|
|
||
| partial class JniPeerMembers { | ||
|
|
||
| internal sealed class JniValueCache<TKey, TValue> : IDisposable, IReadOnlyDictionary<TKey, TValue> | ||
| where TKey : notnull | ||
| where TValue : class | ||
| { | ||
|
|
||
| readonly ConcurrentDictionary<TKey, TValue> values; | ||
| readonly Action<TValue> dispose; | ||
| // JniPeerMembers disposal requires external synchronization with users of returned | ||
| // values. This state only prevents in-flight factories from publishing into a | ||
| // detached cache after teardown. | ||
| int disposed; | ||
|
|
||
| public JniValueCache (int concurrencyLevel, int capacity, Action<TValue> dispose) | ||
| { | ||
| values = new ConcurrentDictionary<TKey, TValue> (concurrencyLevel, capacity); | ||
| this.dispose = dispose; | ||
| } | ||
|
|
||
| public int Count => values.Count; | ||
| public IEnumerable<TKey> Keys => values.Keys; | ||
| public IEnumerable<TValue> Values => values.Values; | ||
| public TValue this [TKey key] => values [key]; | ||
|
|
||
| public bool ContainsKey (TKey key) => values.ContainsKey (key); | ||
| public bool TryGetValue (TKey key, [MaybeNullWhen (false)] out TValue value) => values.TryGetValue (key, out value); | ||
|
|
||
| public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator () => values.GetEnumerator (); | ||
| IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
|
|
||
| internal static JniValueCache<TKey, TValue> GetOrCreate (ref JniValueCache<TKey, TValue>? cache, int concurrencyLevel, int capacity, Action<TValue> dispose) | ||
| { | ||
| var value = Volatile.Read (ref cache); | ||
| if (value != null) | ||
| return value; | ||
|
|
||
| var candidate = new JniValueCache<TKey, TValue> (concurrencyLevel, capacity, dispose); | ||
| var existing = Interlocked.CompareExchange (ref cache, candidate, null); | ||
| if (existing == null) | ||
| return candidate; | ||
|
|
||
| candidate.Dispose (); | ||
| return existing; | ||
| } | ||
|
|
||
| internal static void Dispose (ref JniValueCache<TKey, TValue>? cache) | ||
| { | ||
| Interlocked.Exchange (ref cache, null)?.Dispose (); | ||
| } | ||
|
|
||
| public TValue GetOrAdd (TKey key, Func<TKey, TValue> factory) | ||
| { | ||
| return GetOrAdd (key, static (key, factory) => factory (key), factory); | ||
| } | ||
|
|
||
| public TValue GetOrAdd<TArg> (TKey key, Func<TKey, TArg, TValue> factory, TArg argument) | ||
| { | ||
| AssertNotDisposed (); | ||
|
|
||
| if (values.TryGetValue (key, out var value)) | ||
| return value; | ||
|
|
||
| // ConcurrentDictionary may invoke a GetOrAdd factory multiple times and discard | ||
| // losing values. Construct explicitly so an unpublished owner can be disposed. | ||
| // JNI lookup can also reenter this cache, so do not lock construction. | ||
| TValue? candidate = factory (key, argument); | ||
| try { | ||
| AssertNotDisposed (); | ||
| value = values.GetOrAdd (key, candidate); | ||
| if (ReferenceEquals (value, candidate)) | ||
| candidate = null; | ||
| if (Volatile.Read (ref disposed) != 0) { | ||
| if (values.TryRemove (key, out var removed)) | ||
| dispose (removed); | ||
| throw new ObjectDisposedException (nameof (JniValueCache<TKey, TValue>)); | ||
| } | ||
| return value; | ||
| } finally { | ||
| if (candidate != null) | ||
| dispose (candidate); | ||
| } | ||
| } | ||
|
|
||
| void AssertNotDisposed () | ||
| { | ||
| if (Volatile.Read (ref disposed) != 0) | ||
| throw new ObjectDisposedException (nameof (JniValueCache<TKey, TValue>)); | ||
| } | ||
|
|
||
| public void Dispose () | ||
| { | ||
| if (Interlocked.Exchange (ref disposed, 1) != 0) | ||
| return; | ||
|
|
||
| foreach (var key in values.Keys) { | ||
| if (values.TryRemove (key, out var value)) | ||
| dispose (value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.