diff --git a/internal/p2p/load_router_queue_test.go b/internal/p2p/load_router_queue_test.go index 4421bc07c..f9fd53544 100644 --- a/internal/p2p/load_router_queue_test.go +++ b/internal/p2p/load_router_queue_test.go @@ -33,9 +33,14 @@ func TestLoadRouterSharedQueueOccupancyAndDrops(t *testing.T) { // queue the router builds, at a size small enough to reach its ceiling. const capacity = 8 q := newSimplePriorityQueue(ctx, capacity) - // Account for the queue's heap and both channel buffers. Depending on - // scheduling, draining can make every one of these retained envelopes - // observable before the queue gets another chance to trim its heap. + // The queue holds envelopes in three places: waiting in the input buffer, + // in the heap, and popped into the output buffer. The heap is trimmed to + // maxSize on every push. The run loop holds one envelope in none of these + // between taking it from input and pushing it, which frees an input slot + // for the producer, but that push trims the heap back to maxSize, so the + // envelope replaces one already held instead of adding to the count. A + // flood that nobody drains fills all three, and draining can then hand + // over every one of them before a later push trims the heap again. ceiling := q.maxSize + cap(q.input) + cap(q.output) // Nothing drains while this runs, which is the case that matters: a diff --git a/internal/p2p/rqueue.go b/internal/p2p/rqueue.go index 669acfede..23b6a1b26 100644 --- a/internal/p2p/rqueue.go +++ b/internal/p2p/rqueue.go @@ -73,19 +73,11 @@ func (q *simpleQueue) run(ctx context.Context) { timestamp: time.Now().UTC(), }) - select { - case signal <- struct{}{}: - default: - if len(pq) > q.maxSize { - sort.Sort(pq) - for _, dropped := range pq[q.maxSize:] { - dropped.envelope.NotifyDelivery() - } - pq = pq[:q.maxSize] - } - } - - case <-ticker.C: + // Trim on every push, so the heap never holds more than + // maxSize. Trimming only when the signal was already pending + // let every push that refilled it skip the trim, so with + // nobody draining, the heap ran past maxSize until the next + // tick. if len(pq) > q.maxSize { sort.Sort(pq) for _, dropped := range pq[q.maxSize:] { @@ -93,6 +85,13 @@ func (q *simpleQueue) run(ctx context.Context) { } pq = pq[:q.maxSize] } + + select { + case signal <- struct{}{}: + default: + } + + case <-ticker.C: if len(pq) > 0 { select { case signal <- struct{}{}: