From 4599c6a9e7d0f21c8ff0ead2413fa64dab4856d2 Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Thu, 5 Nov 2026 18:04:30 +0700 Subject: [PATCH] fix(p2p): trim the simple priority queue on every push The simple priority queue trimmed its heap on push only when the signal channel was already pending. A push that refilled an empty signal skipped the trim, so with nobody draining, the heap ran past maxSize until the next 10ms tick. Instrumented under -race, a 2560-envelope flood at capacity 8 took the heap 7 to 18 envelopes past maxSize every time. TestLoadRouterSharedQueueOccupancyAndDrops checks that the queue retains no more than maxSize + cap(input) + cap(output) and failed when the drain caught that overshoot (85 and 87 retained against a ceiling of 84). The ceiling is right for the intended queue, so the queue now trims after every push and the ticker only re-signals. The test comment spells out why the envelope the run loop holds between input and heap cannot add to the count. Co-Authored-By: Claude Opus 5.5 --- internal/p2p/load_router_queue_test.go | 11 ++++++++--- internal/p2p/rqueue.go | 25 ++++++++++++------------- 2 files changed, 20 insertions(+), 16 deletions(-) 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{}{}: