Skip to content

Commit 2e92fe4

Browse files
MaxHeimbrockclaude
andcommitted
Fix chunk drop in incremental reader by buffering pending chunks
ReadIncrementalInstructionBase<T> previously stored each incoming chunk in a single _latestChunk field. When several ChunkReceived FFI events were dispatched on the same sync-context flush (routine between Unity frames), OnChunk overwrote the field before the consumer coroutine could resume from its yield, silently dropping earlier chunks. Queue chunks that arrive while a prior read is still pending. Reset() advances _latestChunk to the next queued chunk and keeps IsCurrentReadDone true so the next yield completes immediately, draining the buffer before waiting for more FFI events. Flips DataStreamIncrementalReadTests from failing to passing. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 77f7fd5 commit 2e92fe4

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

Runtime/Scripts/DataStream.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ internal StreamError(Proto.StreamError proto) : base(proto.Description) { }
7777
public abstract class ReadIncrementalInstructionBase<TContent> : StreamYieldInstruction
7878
{
7979
private readonly ulong _handleValue;
80+
private readonly Queue<TContent> _pendingChunks = new();
8081
private TContent _latestChunk;
8182

8283
/// <summary>
@@ -107,8 +108,26 @@ protected ReadIncrementalInstructionBase(FfiHandle readerHandle)
107108

108109
protected void OnChunk(TContent content)
109110
{
110-
_latestChunk = content;
111-
IsCurrentReadDone = true;
111+
if (IsCurrentReadDone)
112+
{
113+
// Consumer hasn't yielded since the last chunk; buffer until Reset().
114+
_pendingChunks.Enqueue(content);
115+
}
116+
else
117+
{
118+
_latestChunk = content;
119+
IsCurrentReadDone = true;
120+
}
121+
}
122+
123+
public override void Reset()
124+
{
125+
base.Reset();
126+
if (_pendingChunks.Count > 0)
127+
{
128+
_latestChunk = _pendingChunks.Dequeue();
129+
IsCurrentReadDone = true;
130+
}
112131
}
113132

114133
protected void OnEos(Proto.StreamError protoError)

Tests/EditMode/DataStreamIncrementalReadTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3-
using LiveKit;
43
using LiveKit.Internal;
54
using NUnit.Framework;
65

76
namespace LiveKit.EditModeTests
87
{
9-
public class DataStreamIncrementalReadTests
8+
public class DataStreamTests
109
{
1110
private sealed class TestIncrementalReader : ReadIncrementalInstructionBase<string>
1211
{

0 commit comments

Comments
 (0)