fix(script-runner): #999 K-5 script-worker frames are cut at the frame cap; one oversized evaluation fails alone instead of killing the worker

This commit is contained in:
Joseph Doherty
2026-12-03 10:56:40 -05:00
parent c42eeb3c69
commit 8e91d6503d
6 changed files with 314 additions and 19 deletions
@@ -574,7 +574,9 @@ internal sealed class WorkerHost
using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(100));
while (await timer.WaitForNextTickAsync())
{
if (_logSink.Drain() is { } batch) Send(new Envelope { Logs = batch });
// #999 / review K-5: cut by BYTES as well as lines — one batch of every buffered line (≤ 1,000 × ~24 KB)
// used to exceed the frame cap, fault the writer and exit the worker.
foreach (var batch in _logSink.Drain(Math.Max(1, _maxFrameBytes - 1024))) Send(new Envelope { Logs = batch });
}
}
@@ -816,17 +818,39 @@ internal sealed class WorkerHost
});
}
public LogBatch? Drain()
/// <summary>
/// Drains every buffered line into batches of at most <paramref name="maxBatchBytes"/> encoded bytes each
/// (#999 / review K-5). A line too large for any batch on its own is counted as dropped.
/// </summary>
public List<LogBatch> Drain(int maxBatchBytes)
{
var batches = new List<LogBatch>();
var dropped = Interlocked.Exchange(ref _dropped, 0);
if (_lines.IsEmpty && dropped == 0) return null;
var batch = new LogBatch { Dropped = (uint)dropped };
if (_lines.IsEmpty && dropped == 0) return batches;
var batch = new LogBatch();
var bytes = 0;
while (_lines.TryDequeue(out var line))
{
Interlocked.Decrement(ref _count);
var size = line.CalculateSize();
var lineBytes = 1 + Google.Protobuf.CodedOutputStream.ComputeLengthSize(size) + size;
if (lineBytes > maxBatchBytes - 16)
{
dropped++;
continue;
}
if (bytes + lineBytes > maxBatchBytes - 16)
{
batches.Add(batch);
batch = new LogBatch();
bytes = 0;
}
batch.Lines.Add(line);
bytes += lineBytes;
}
return batch;
batch.Dropped = (uint)dropped;
batches.Add(batch);
return batches;
}
private static string Truncate(string value, int max) => value.Length <= max ? value : value[..max];