Skip to content

Commit 618ce6d

Browse files
committed
Don't wait forever whilst trying to load the Native Library
1 parent 8024ef1 commit 618ce6d

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

java/src/main/java/org/rocksdb/RocksDB.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,7 @@ public static void loadLibrary() {
8484
return;
8585
}
8686

87-
try {
88-
while (libraryLoaded.get() == LibraryState.LOADING) {
89-
Thread.sleep(10);
90-
}
91-
} catch (final InterruptedException e) {
92-
// restore interrupted status
93-
Thread.currentThread().interrupt();
94-
throw new RuntimeException("Interrupted whilst trying to load the RocksDB shared library", e);
95-
}
87+
waitForLibraryToBeLoaded();
9688
}
9789

9890
/**
@@ -148,9 +140,23 @@ public static void loadLibrary(final List<String> paths) {
148140
return;
149141
}
150142

143+
waitForLibraryToBeLoaded();
144+
}
145+
146+
private static void waitForLibraryToBeLoaded() {
147+
final long wait = 10; // Time to wait before re-checking if another thread loaded the library
148+
final long timeout =
149+
10 * 1000; // Maximum time to wait for another thread to load the library (10 seconds)
150+
long waited = 0;
151151
try {
152152
while (libraryLoaded.get() == LibraryState.LOADING) {
153-
Thread.sleep(10);
153+
Thread.sleep(wait);
154+
waited += wait;
155+
156+
if (waited >= timeout) {
157+
throw new RuntimeException(
158+
"Exceeded timeout whilst trying to load the RocksDB shared library");
159+
}
154160
}
155161
} catch (final InterruptedException e) {
156162
// restore interrupted status

0 commit comments

Comments
 (0)