Skip to content

Commit

Permalink
Java: extend JniWrapperCache checks to identify double dispose problem
Browse files Browse the repository at this point in the history
This change extends debug checks in JniWrapperCache class
to show the problem related to double dispose.

When we remove an entry from JniWrapperCache in 'get_cached_wrapper_impl()'
because the reference was invalidated we must anticipate, that the cleanup
will be scheduled for this non-existent object. It may cause problems, because
the cleanup of weak reference entry for the old object may remove the entry
for the new entry.

This commit extends checking and again makes 'stressTestJniWeakReferenceCache()'
test to fail.

In next commit the fix will be implemented to prevent such double-dispose.

Signed-off-by: Patryk Wrobel <[email protected]>
  • Loading branch information
pwrobeldev committed Jan 30, 2025
1 parent c6fd38d commit dd4599a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void onCreate() {
if (isFirstTime) {
isFirstTime = false; // Only load libraries once
loadNativeLibraries();
NativeBase.propagateCleanupException = true;
Runtime.getRuntime()
.addShutdownHook(
new Thread() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public abstract class NativeBase {
private static final Set<Reference<?>> REFERENCES =
Collections.newSetFromMap(new ConcurrentHashMap<Reference<?>, Boolean>());
public static boolean propagateCleanupException = false;
private static final ReferenceQueue<NativeBase> REFERENCE_QUEUE = new ReferenceQueue<>();
private final long nativeHandle;
Expand Down Expand Up @@ -126,6 +128,9 @@ public abstract class NativeBase {
((DisposableReference) reference).dispose();
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Error cleaning up after reference.", t);
if (propagateCleanupException) {
throw t;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ void JniWrapperCache::remove_cached_wrapper_impl(JNIEnv* jenv, const void* obj_p
std::lock_guard<std::mutex> lock(s_mutex);
auto iter = s_wrapper_cache.find(obj_ptr);
#ifdef INTERNAL_GLUECODIUM_JNI_WEAK_REF_CHECK
if (iter == s_wrapper_cache.end()) {
jenv->ThrowNew(jenv->FindClass("java/lang/Exception"), "Invalid removal of cache entry");
}
#endif

if (iter != s_wrapper_cache.end()) {
jenv->DeleteWeakGlobalRef(iter->second);
s_wrapper_cache.erase(iter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public abstract class NativeBase {
private static final Set<Reference<?>> REFERENCES =
Collections.newSetFromMap(new ConcurrentHashMap<Reference<?>, Boolean>());

public static boolean propagateCleanupException = false;

private static final ReferenceQueue<NativeBase> REFERENCE_QUEUE = new ReferenceQueue<>();
private final long nativeHandle;

Expand Down Expand Up @@ -106,6 +108,9 @@ private static void cleanUpQueue() {
((DisposableReference) reference).dispose();
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Error cleaning up after reference.", t);
if (propagateCleanupException) {
throw t;
}
}
}
}
Expand Down

0 comments on commit dd4599a

Please sign in to comment.