Skip to content

Commit 078f425

Browse files
committed
Guard against re-entrant close() from within callbacks
If close() is called from an LlmCallback during generate() (same thread, re-entrant lock acquisition), detect via getHoldCount() > 1 and throw instead of freeing native resources mid-call. This commit was authored with the help of Claude.
1 parent 841b443 commit 078f425

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

  • extension/android/executorch_android/src/main/java/org/pytorch/executorch/extension/llm

‎extension/android/executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModule.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.io.Closeable;
1414
import java.nio.ByteBuffer;
1515
import java.util.List;
16-
import java.util.concurrent.locks.Lock;
1716
import java.util.concurrent.locks.ReentrantLock;
1817
import org.pytorch.executorch.ExecuTorchRuntime;
1918
import org.pytorch.executorch.ExecutorchRuntimeException;
@@ -33,7 +32,7 @@ public class LlmModule implements Closeable {
3332
public static final int MODEL_TYPE_MULTIMODAL = 2;
3433

3534
private final HybridData mHybridData;
36-
private final Lock mLock = new ReentrantLock();
35+
private final ReentrantLock mLock = new ReentrantLock();
3736
private boolean mDestroyed = false;
3837
private static final int DEFAULT_SEQ_LEN = 128;
3938
private static final boolean DEFAULT_ECHO = true;
@@ -204,6 +203,10 @@ private void checkNotDestroyed() {
204203
public void close() {
205204
if (mLock.tryLock()) {
206205
try {
206+
if (mLock.getHoldCount() > 1) {
207+
throw new IllegalStateException(
208+
"Cannot close module from within a callback during execution");
209+
}
207210
if (!mDestroyed) {
208211
mDestroyed = true;
209212
mHybridData.resetNative();

0 commit comments

Comments
 (0)