Java - Fixed a reference counting bug in the OrtEnvironment close method. Added a unit test for the bug.

This commit is contained in:
Adam Pocock 2020-01-29 16:31:06 -05:00 committed by Changming Sun
parent 7bb5c357a8
commit 4cc0031177
2 changed files with 13 additions and 1 deletions

View file

@ -248,7 +248,6 @@ public class OrtEnvironment implements AutoCloseable {
*/
@Override
public synchronized void close() throws OrtException {
closed = true;
synchronized (refCount) {
int curCount = refCount.get();
if (curCount != 0) {
@ -256,6 +255,7 @@ public class OrtEnvironment implements AutoCloseable {
}
if (curCount == 1) {
close(OnnxRuntime.ortApiHandle, nativeHandle);
closed = true;
INSTANCE = null;
}
}

View file

@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@ -70,6 +71,17 @@ public class InferenceTest {
}
}
@Test
public void repeatedCloseTest() throws OrtException {
OrtEnvironment env = OrtEnvironment.getEnvironment("repeatedCloseTest");
try (OrtEnvironment otherEnv = OrtEnvironment.getEnvironment()) {
assertFalse(otherEnv.isClosed());
}
assertFalse(env.isClosed());
env.close();
assertTrue(env.isClosed());
}
@Test
public void createSessionFromPath() throws OrtException {
String modelPath = resourcePath.resolve("squeezenet.onnx").toString();