You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(WIP) Make Android Module thread-safe and prevent destruction during inference (#9833)
Summary:
While the Android Module interface was originally not designed to be thread safe, we've seen a sizable number of issues pop up due to users not fully meeting the thread safety requirements that we impose on the caller. Empirically, this is not always obvious when writing app code and can sneak in in subtle ways. Common issues are calling forward from a different thread while one inference is already in progress and not synchronizing module cleanup with inference. Both have caused crashes that are sometimes difficult for users to debug.
This PR attempts to mitigate these issues by adding explicit synchronization in the Java Module class. Both method load and execution are behind a lock, and destroy will warn and avoid immediate destruction if an inference is in progress. I'm hesitant to directly acquire the lock in destroy, since it can get called in certain cleanup paths. Instead, I'm just warning and setting the native peer to null so it should get GC'd once out of use.
Differential Revision: D72273052
Copy file name to clipboardExpand all lines: extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/ModuleInstrumentationTest.java
+62
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,8 @@
25
25
importjava.io.InputStream;
26
26
importjava.net.URI;
27
27
importjava.net.URISyntaxException;
28
+
importjava.util.concurrent.CountDownLatch;
29
+
importjava.util.concurrent.atomic.AtomicInteger;
28
30
importjava.io.IOException;
29
31
importjava.io.File;
30
32
importjava.io.FileOutputStream;
@@ -42,6 +44,7 @@ public class ModuleInstrumentationTest {
42
44
privatestaticStringFORWARD_METHOD = "forward";
43
45
privatestaticStringNONE_METHOD = "none";
44
46
privatestaticintOK = 0x00;
47
+
privatestaticintINVALID_STATE = 0x2;
45
48
privatestaticintINVALID_ARGUMENT = 0x12;
46
49
privatestaticintACCESS_FAILED = 0x22;
47
50
@@ -124,4 +127,63 @@ public void testNonPteFile() throws IOException{
0 commit comments