fix(site-runtime): reuse exited detached worker slots in script scheduler (review 2026-10-21 01/SR-M1)
This commit is contained in:
+40
@@ -118,4 +118,44 @@ public class ScriptExecutionSchedulerTests
|
||||
await Task.Delay(50);
|
||||
Assert.True(condition(), "condition not met within timeout");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Repeatedly wedging + detaching a worker must reuse the vacated slot rather than grow the
|
||||
/// slot table without bound (review 2026-10-21 01/SR-M1).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task DetachedWorkerSlot_IsReused_SlotTableStaysBounded()
|
||||
{
|
||||
using var scheduler = new ScriptExecutionScheduler(1);
|
||||
|
||||
for (var round = 0; round < 6; round++)
|
||||
{
|
||||
var gate = new ManualResetEventSlim(false);
|
||||
var started = new TaskCompletionSource<(int Slot, long Stamp)>();
|
||||
var wedged = Task.Factory.StartNew(() =>
|
||||
{
|
||||
var slot = ScriptExecutionScheduler.CurrentWorkerSlot!.Value;
|
||||
started.SetResult((slot, scheduler.CurrentRunStamp(slot)));
|
||||
gate.Wait();
|
||||
}, CancellationToken.None, TaskCreationOptions.None, scheduler);
|
||||
|
||||
var (s, stamp) = await started.Task;
|
||||
Assert.Equal(WorkerDetachOutcome.Detached, scheduler.TryDetachWorker(s, stamp));
|
||||
gate.Set();
|
||||
await wedged;
|
||||
|
||||
// Wait for the detached worker to exit and hand its slot back.
|
||||
var deadline = DateTime.UtcNow.AddSeconds(5);
|
||||
while (scheduler.DetachedThreadCount != 0 && DateTime.UtcNow < deadline)
|
||||
await Task.Delay(10);
|
||||
Assert.Equal(0, scheduler.DetachedThreadCount);
|
||||
await Task.Delay(50);
|
||||
}
|
||||
|
||||
// 1 live + at most 1 free/in-flight detached slot, never 1 + 6.
|
||||
Assert.True(scheduler.SlotCount <= 2, $"slot table grew to {scheduler.SlotCount}");
|
||||
// The pool still works afterwards.
|
||||
var ran = await Task.Factory.StartNew(() => 42, CancellationToken.None, TaskCreationOptions.None, scheduler);
|
||||
Assert.Equal(42, ran);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user