releaseVirtualConversation method
Tear down the live virtual conversation if it belongs to
conversationToken. Called when a virtual session closes so its native
conversation doesn't linger. If a different session is active, this is a
no-op — that session's conversation must stay live.
If a turn is in flight, the teardown is DEFERRED to the turn's cleanup — deleting the pointer now would be a use-after-free. We also cancel the in-flight generation so the turn finishes promptly and the deferred teardown runs.
Implementation
void releaseVirtualConversation(Object conversationToken) {
if (_virtualActiveToken != conversationToken) return;
if (_virtualTurnInFlight) {
_pendingReleaseToken = conversationToken;
final conv = _virtualConv;
if (conv != null) _cancelOn(conv);
return;
}
final conv = _virtualConv;
if (conv != null) {
_deleteConversation(conv);
_virtualConv = null;
_virtualActiveToken = null;
}
}