close method

  1. @override
Future<void> close()
override

Implementation

@override
Future<void> close() async {
  _isClosed = true;
  _isCancelled = true;
  // Abort any in-flight JS generation BEFORE the model tears the engine
  // down. Without this, the model's close() → engine.delete() can free the
  // WASM/WebGPU state while a pending iter.next() Promise is still resolving
  // against this conversation (use-after-free). stopGeneration() does the
  // same; close() must too.
  try {
    conversation.cancel();
  } catch (e) {
    if (kDebugMode) {
      debugPrint('[LiteRtLmWebSession] cancel during close threw: $e');
    }
  }
  _queryBuffer.clear();
  onClose();
}