Skip to content

Compact recently-closed heap to prevent stale entry accumulation#134

Open
Isusami wants to merge 1 commit intomasterking32:mainfrom
Isusami:feature/compact-recently-closed-heap
Open

Compact recently-closed heap to prevent stale entry accumulation#134
Isusami wants to merge 1 commit intomasterking32:mainfrom
Isusami:feature/compact-recently-closed-heap

Conversation

@Isusami
Copy link
Copy Markdown

@Isusami Isusami commented Apr 13, 2026

Summary

Add heap compaction to pruneRecentlyClosedLocked to prevent unbounded growth of the recentlyClosedHeap when stream IDs are rapidly recycled.

Changes

  • Add compactRecentlyClosedHeapLocked -- rebuilds the heap from the authoritative map when the heap exceeds 1.5x the map size (with a minimum floor of mapLen + 4)
  • Call it at the end of every pruneRecentlyClosedLocked cycle
  • Add TestRecentlyClosedHeapStaleEntryGrowth regression test that simulates 10 cycles of 50 recycled stream IDs

Why

When a stream ID is re-remembered with a new expiration, a new entry is pushed onto the heap but the old (stale) entry remains. The lazy-deletion in pruneRecentlyClosedLocked only cleans up entries at the front of the heap. Under pathological stream ID recycling, the heap grew to ~1.9x the map size. With compaction, peak ratio is capped at ~1.5x.

Test plan

  • go test -race ./internal/client/... -- all tests pass, race-clean
  • TestRecentlyClosedHeapStaleEntryGrowth confirms heap stays within 1.5x bound

When stream IDs are rapidly recycled, re-remembering pushes new heap
entries while stale ones remain, causing the heap to grow up to ~1.9x
the map size. Add compactRecentlyClosedHeapLocked to rebuild the heap
from the authoritative map when it exceeds 1.5x, bounding peak memory.
Includes regression test TestRecentlyClosedHeapStaleEntryGrowth.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant