Skip to content

Commit 32504d0

Browse files
vkuttypCopilot
andcommitted
fix: replace lastBufStart with lastBufEnd in QueryStreamAsync
The old guard skipped DecodePartial whenever bufStart == lastBufStart (no decode progress). But after a split-token, bufStart stays the same while new packets keep arriving — so every subsequent packet was skipped until EOM, causing all rows to arrive at once instead of streaming. Fix: track lastBufEnd instead. Skip decode only when no new bytes have arrived since the last attempt (zero-payload packet), which is the correct and rare case. Every packet that adds bytes now triggers decode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f064f3b commit 32504d0

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/SqlDotnetty.MsSql/MsSqlConnection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public async IAsyncEnumerable<SqlRow> QueryStreamAsync(
301301
var buffer = new byte[8192];
302302
int bufEnd = 0; // exclusive end of valid data
303303
int bufStart = 0; // start of unconsumed data (decode cursor)
304-
int lastBufStart = -1; // detect no-progress: skip decode until more data arrives
304+
int lastBufEnd = 0; // detect no-progress: skip decode only if no new bytes arrived
305305

306306
var colMeta = new List<TdsColumnMeta>();
307307
bool eom = false;
@@ -340,19 +340,19 @@ public async IAsyncEnumerable<SqlRow> QueryStreamAsync(
340340
}
341341

342342
// ── Decode as many complete tokens as possible ───────────────
343-
// Skip decode if last call made no progress — we need more data first.
344-
// Always decode on EOM so we don't miss the final tokens.
345-
if (bufStart != lastBufStart || eom)
343+
// Skip decode only when no new bytes have arrived since the last attempt
344+
// (i.e. a zero-payload status packet). Always decode on EOM.
345+
if (bufEnd > lastBufEnd || eom)
346346
{
347-
lastBufStart = bufStart;
347+
lastBufEnd = bufEnd;
348348
var (tokens, newOffset, newColMeta) =
349349
TdsDecoder.DecodePartial(buffer, bufStart, bufEnd, colMeta);
350350

351351
bufStart = newOffset;
352352
colMeta = newColMeta;
353353

354354
// Compact when fully consumed to recycle buffer space.
355-
if (bufStart == bufEnd) { bufStart = bufEnd = 0; lastBufStart = -1; }
355+
if (bufStart == bufEnd) { bufStart = bufEnd = 0; lastBufEnd = 0; }
356356

357357
// ── Yield rows; raise info/error messages ────────────────────
358358
foreach (var token in tokens)

0 commit comments

Comments
 (0)