Commit 54b0148
authored
Android: LlmModule Closeable lifecycle and ModelRunner crash fix (#19012)
## Summary
- **LlmModule**: Implement `Closeable` so modules can be used with
try-with-resources. Add `close()` with explicit
caller contract. Deprecate `resetNative()` in favor of `close()`. Add
`ReentrantLock` to serialize access to
non-thread-safe native state, matching `Module.java`'s pattern.
- **ModelRunner**: Add early return after `loadMethod()` failure to
prevent cascading crash in warmup/inference
loops. Fix native memory leak by wrapping benchmark body in try/finally
for `module.destroy()`.
### LlmModule lifecycle design
- `ReentrantLock` serializes all operations — prevents accidental
concurrent access (e.g. double-tap, rotation
during generation) from causing native crashes
- `generate()`/`prefill*()`/`load()`/`resetContext()` acquire the lock
- `close()` uses `tryLock()` — fails fast with `IllegalStateException`
instead of blocking (no ANR risk)
- `stop()` remains a bare native method — lock-free, uses C++ atomic
flag for cross-thread interrupt
- Caller contract: call `stop()`, wait for `generate()` to return, then
`close()`
### Tests added
- `testUseAfterCloseThrows`: `generate()` after `close()` throws
`IllegalStateException`
- `testCloseIsIdempotent`: double `close()` is safe
## Test plan
- [ ] Existing `LlmModuleInstrumentationTest` tests pass
- [ ] New lifecycle tests pass
- [ ] ModelRunner benchmark handles load failures gracefully
This PR was authored with the help of Claude.1 parent 239f7cc commit 54b0148
3 files changed
Lines changed: 277 additions & 159 deletions
File tree
- extension
- android/executorch_android/src
- androidTest/java/org/pytorch/executorch
- main/java/org/pytorch/executorch/extension/llm
- benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
274 | 290 | | |
275 | 291 | | |
276 | 292 | | |
| |||
0 commit comments