Skip to content

Commit acc26bf

Browse files
greenrobotgreenrobot-team
authored andcommitted
BoxStore: make handle volatile
Also, set handle to 0 before calling nativeDelete() to mitigate races
1 parent c7ac7ed commit acc26bf

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static boolean isSyncServerAvailable() {
233233
private final File directory;
234234
private final String canonicalPath;
235235
/** Reference to the native store. Should probably get through {@link #getNativeStore()} instead. */
236-
private long handle;
236+
volatile private long handle;
237237
private final Map<Class<?>, String> dbNameByClass = new HashMap<>();
238238
private final Map<Class<?>, Integer> entityTypeIdByClass = new HashMap<>();
239239
private final Map<Class<?>, EntityInfo<?>> propertiesByClass = new HashMap<>();
@@ -690,11 +690,11 @@ public void close() {
690690
t.close();
691691
}
692692

693-
if (handle != 0) { // failed before native handle was created?
694-
nativeDelete(handle);
695-
// The Java API has open checks, but just in case re-set the handle so any native methods will
696-
// not crash due to an invalid pointer.
697-
handle = 0;
693+
long handleToDelete = handle;
694+
// Make isNativeStoreClosed() return true before actually closing to avoid Transaction.close() crash
695+
handle = 0;
696+
if (handleToDelete != 0) { // failed before native handle was created?
697+
nativeDelete(handleToDelete);
698698
}
699699

700700
// When running the full unit test suite, we had 100+ threads before, hope this helps:

0 commit comments

Comments
 (0)