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:
@@ -360,6 +360,52 @@ public sealed class ContainmentTests
|
||||
await Scripts.Eventually(() => sink.Rendered.Contains("hello 42"), TimeSpan.FromSeconds(5), "the forwarded log line");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #999 / review K-5: a script that logs megabytes inside one drain window no longer kills its worker. The worker's
|
||||
/// log drain put every buffered line (≤ 1,000 × ~24 KB) into ONE LogBatch over the 4 MiB frame cap; the writer
|
||||
/// faulted and the worker exited — every in-flight evaluation on the shard went BadResourceUnavailable. Now the
|
||||
/// drain is cut by bytes; the worker survives (same pid) and keeps evaluating.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task A_script_logging_megabytes_at_once_does_not_kill_its_worker()
|
||||
{
|
||||
var sink = new CapturingSink();
|
||||
await using var runner = Scripts.Runner(Scripts.Options());
|
||||
(await runner.RunAsync(Scripts.Vt("warm", "return 1;"), Ct)).Value.ShouldBe(1);
|
||||
var worker = runner.Shards[0].Current.ShouldNotBeNull();
|
||||
|
||||
var flood = await runner.RunAsync(Scripts.Vt("flood",
|
||||
"var s = new string('x', 8000); for (var i = 0; i < 2000; i++) ctx.Logger.Information(s); return 2;",
|
||||
timeout: TimeSpan.FromSeconds(20), logger: sink.CreateLogger()), Ct);
|
||||
flood.Status.ShouldBe(ScriptRunStatus.Ok, flood.Reason);
|
||||
flood.Value.ShouldBe(2);
|
||||
await Scripts.Eventually(() => sink.Events.Count >= 100 ? sink : null, TimeSpan.FromSeconds(10), "the forwarded lines");
|
||||
|
||||
// Positive control: the same worker answers the next evaluation.
|
||||
(await runner.RunAsync(Scripts.Vt("after", "return 3;"), Ct)).Value.ShouldBe(3);
|
||||
runner.Shards[0].Current.ShouldNotBeNull().Pid.ShouldBe(worker.Pid, "the worker was not restarted");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// K-5 host side: an evaluation whose inputs alone exceed the frame cap fails THAT evaluation (unsupported type),
|
||||
/// instead of faulting the pipe writer and killing the worker with every other evaluation in flight on it.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task An_evaluation_whose_inputs_exceed_the_frame_cap_fails_alone()
|
||||
{
|
||||
await using var runner = Scripts.Runner(Scripts.Options());
|
||||
(await runner.RunAsync(Scripts.Vt("warm", "return 1;"), Ct)).Value.ShouldBe(1);
|
||||
var worker = runner.Shards[0].Current.ShouldNotBeNull();
|
||||
|
||||
var big = await runner.RunAsync(Scripts.Vt("big", "return ((string)ctx.GetTag(\"t\").Value).Length;",
|
||||
inputs: Scripts.Input("t", new string('z', 5 * 1024 * 1024))), Ct);
|
||||
big.Status.ShouldBe(ScriptRunStatus.Failed);
|
||||
big.Reason!.ShouldContain("frame cap");
|
||||
|
||||
(await runner.RunAsync(Scripts.Vt("after", "return 3;"), Ct)).Value.ShouldBe(3);
|
||||
runner.Shards[0].Current.ShouldNotBeNull().Pid.ShouldBe(worker.Pid, "the worker was not restarted");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task An_alarm_predicate_runs_out_of_process()
|
||||
{
|
||||
|
||||
@@ -318,6 +318,103 @@ public sealed class ProtocolTests
|
||||
stream.Writes.ShouldBe(2, "one write per pass");
|
||||
}
|
||||
|
||||
private const int SmallCap = 64 * 1024;
|
||||
|
||||
private static List<Envelope> ReadAll(byte[] bytes, int cap)
|
||||
{
|
||||
var reader = new ScriptWorkerFrameReader(new MemoryStream(bytes), cap);
|
||||
var frames = new List<Envelope>();
|
||||
while (reader.Read() is { } env) frames.Add(env);
|
||||
return frames;
|
||||
}
|
||||
|
||||
private static Envelope BigEval(ulong id, int keyChars = 8000)
|
||||
=> new() { Eval = new EvalBatch { Items = { new EvalRequest { EvalId = id, Key = new string('k', keyChars) } } } };
|
||||
|
||||
private static EvalResult BigResult(ulong id, int chars = 8000)
|
||||
=> new() { EvalId = id, Value = new Variant { Type = Variant.Types.TypeTag.String, S = new string('r', chars) } };
|
||||
|
||||
/// <summary>
|
||||
/// #999 / review K-5: evaluations queued behind a write in progress are merged, but never past the frame cap — the
|
||||
/// merged batch is cut into several frames, each within the cap, in order. Before the fix the writer merged
|
||||
/// unboundedly and <c>Append</c> threw, which faults the writer and kills the worker connection.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Merged_evaluations_are_cut_at_the_frame_cap_not_faulted()
|
||||
{
|
||||
var stream = new GatedStream();
|
||||
Exception? fault = null;
|
||||
var writer = new ScriptWorkerFrameWriter(stream, ex => fault = ex, SmallCap, firstSeq: 1);
|
||||
|
||||
writer.TryWrite(Eval(1)).ShouldBeTrue(); // blocks on the gate
|
||||
for (ulong id = 2; id <= 25; id++) writer.TryWrite(BigEval(id)).ShouldBeTrue(); // ≈ 3 × the cap queued
|
||||
stream.Gate.SetResult();
|
||||
await Scripts.Eventually(() =>
|
||||
{
|
||||
try { return ReadAll(stream.ToArray(), SmallCap).SelectMany(f => f.Eval.Items).Count() == 25; }
|
||||
catch (ScriptWorkerProtocolException) { return false; }
|
||||
}, TimeSpan.FromSeconds(5), "every evaluation written");
|
||||
|
||||
fault.ShouldBeNull();
|
||||
var frames = ReadAll(stream.ToArray(), SmallCap);
|
||||
frames.Count.ShouldBeGreaterThanOrEqualTo(4, "the merged batch was cut, not sent whole");
|
||||
frames.ShouldAllBe(f => f.CalculateSize() <= SmallCap);
|
||||
frames.SelectMany(f => f.Eval.Items).Select(i => i.EvalId).ShouldBe(Enumerable.Range(1, 25).Select(i => (ulong)i));
|
||||
frames.Select(f => f.Seq).ShouldBe(Enumerable.Range(2, frames.Count).Select(i => (ulong)i));
|
||||
}
|
||||
|
||||
/// <summary>K-5: ONE envelope whose items together exceed the cap (an unbounded <c>EvalBatch</c>) is split by item.</summary>
|
||||
[Fact]
|
||||
public void One_oversized_batch_is_split_by_item()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
Exception? fault = null;
|
||||
var writer = new ScriptWorkerFrameWriter(stream, ex => fault = ex, SmallCap, firstSeq: 1, synchronous: true);
|
||||
var batch = new EvalBatch();
|
||||
for (ulong id = 1; id <= 30; id++) batch.Items.Add(new EvalRequest { EvalId = id, Key = new string('k', 8000) });
|
||||
|
||||
writer.TryWrite(new Envelope { Eval = batch }).ShouldBeTrue();
|
||||
|
||||
fault.ShouldBeNull();
|
||||
var frames = ReadAll(stream.ToArray(), SmallCap);
|
||||
frames.Count.ShouldBeGreaterThan(1);
|
||||
frames.ShouldAllBe(f => f.CalculateSize() <= SmallCap);
|
||||
frames.SelectMany(f => f.Eval.Items).Select(i => i.EvalId).ShouldBe(Enumerable.Range(1, 30).Select(i => (ulong)i));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// K-5: results are cut at the cap the same way, the started / deferred / soft-expired ids still arrive, and a
|
||||
/// single RESULT too large for any frame fails that one evaluation (unsupported type) instead of the connection.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Results_are_cut_at_the_cap_and_one_oversized_result_fails_alone()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
Exception? fault = null;
|
||||
var writer = new ScriptWorkerFrameWriter(stream, ex => fault = ex, SmallCap, firstSeq: 1, synchronous: true);
|
||||
var results = new EvalResults { StartedEvalIds = { 100, 101 }, DeferredEvalIds = { 102 }, SoftExpiredEvalIds = { 103 } };
|
||||
for (ulong id = 1; id <= 20; id++) results.Items.Add(BigResult(id));
|
||||
results.Items.Add(BigResult(21, chars: SmallCap * 2)); // alone over the cap
|
||||
results.Items.Add(BigResult(22));
|
||||
|
||||
writer.TryWrite(new Envelope { Results = results }).ShouldBeTrue();
|
||||
|
||||
fault.ShouldBeNull();
|
||||
var frames = ReadAll(stream.ToArray(), SmallCap);
|
||||
frames.Count.ShouldBeGreaterThan(1);
|
||||
frames.ShouldAllBe(f => f.CalculateSize() <= SmallCap);
|
||||
var items = frames.SelectMany(f => f.Results.Items).ToList();
|
||||
items.Select(i => i.EvalId).ShouldBe(Enumerable.Range(1, 22).Select(i => (ulong)i));
|
||||
var oversized = items.Single(i => i.EvalId == 21);
|
||||
oversized.OutcomeCase.ShouldBe(EvalResult.OutcomeOneofCase.Error);
|
||||
oversized.Error.Kind.ShouldBe(ScriptError.Types.Kind.UnsupportedType);
|
||||
oversized.Error.Message.ShouldContain("frame cap");
|
||||
items.Single(i => i.EvalId == 22).Value.S.Length.ShouldBe(8000, "the neighbours are untouched");
|
||||
frames.SelectMany(f => f.Results.StartedEvalIds).ShouldBe([100UL, 101UL]);
|
||||
frames.SelectMany(f => f.Results.DeferredEvalIds).ShouldBe([102UL]);
|
||||
frames.SelectMany(f => f.Results.SoftExpiredEvalIds).ShouldBe([103UL]);
|
||||
}
|
||||
|
||||
/// <summary>A failed write completes the writer, reports once, and later frames are refused rather than lost silently.</summary>
|
||||
[Fact]
|
||||
public void A_failed_write_completes_the_writer_and_reports_once()
|
||||
|
||||
Reference in New Issue
Block a user