From 4b6387881577c3de8afa6dae61bfbd12ca107378 Mon Sep 17 00:00:00 2001 From: sebasbad Date: Wed, 29 Jul 2026 20:16:29 +0200 Subject: [PATCH] fix(engine): dispose model, context, and multimodal instance in worker shutdown to release C++ VRAM allocations --- lib/src/isolate/worker.dart | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/src/isolate/worker.dart b/lib/src/isolate/worker.dart index ee736ff6..07a2d5f2 100644 --- a/lib/src/isolate/worker.dart +++ b/lib/src/isolate/worker.dart @@ -860,11 +860,19 @@ void _shutdown( } catch (_) {/* ignore */} } state.sessions.clear(); - // We deliberately do NOT dispose the model or context here. With multiple - // engines in a process, freeing one worker's model/context can crash the - // other worker's outstanding operations because the backend is shared and - // some teardown paths touch process-global state. The OS reclaims memory - // on process exit; that is good enough for M3. + + if (state.multimodal != null) { + try { + state.multimodal!.dispose(); + } catch (_) {} + } + try { + state.context.dispose(); + } catch (_) {} + try { + state.model.dispose(); + } catch (_) {} + LlamaLibrary.dispose(); reply.send(EngineShutdownComplete(requestId)); commandRx.close();