Repository navigation
Zero the vacated head slot in concurrent.Queue.Dequeue to release dead references - #598
Merged
Quim Muntal (qmuntal) merged 1 commit intoJul 23, 2026
Conversation
Copilot started reviewing on behalf of
PratikDhanave (PratikDhanave)
July 23, 2026 02:24
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes unintended memory retention in internal/concurrent.Queue by ensuring dequeued elements no longer remain GC-reachable via the slice’s backing array, and by releasing the backing array when the queue becomes empty.
Changes:
- Zero the vacated head slot in
Queue.Dequeue()before reslicing to avoid pinning dequeued references. - Set
q.items = nilwhen the queue drains to drop the backing array entirely. - Add deterministic tests that validate head-slot zeroing and backing-array release on empty.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/concurrent/queue.go | Clears the dequeued slot and nils the slice when empty to prevent retaining dead references. |
| internal/concurrent/queue_test.go | Adds tests verifying vacated-slot zeroing and backing-array release when the queue drains. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Dequeue advanced the slice with q.items = q.items[1:] without clearing the old head slot, so the backing array kept the dequeued reference alive until it reallocated. For long-lived reference-typed queues (message envelopes, workflow events, deferred funcs) that cycle steadily, dead values stayed GC-reachable far longer than necessary. Zero the head before re-slicing and drop the backing array once the queue empties, matching slices.Delete semantics so dead references are released promptly. Add deterministic tests asserting the vacated slot is cleared and the array is released when the queue drains.
PratikDhanave (PratikDhanave)
force-pushed
the
zero-vacated-queue-head-slot
branch
from
July 23, 2026 08:48
058bd1c to
89f37ec
Compare
Quim Muntal (qmuntal)
approved these changes
Jul 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Queue.Dequeueadvanced the internal slice withq.items = q.items[1:]but never cleared the old head slot. The backing array therefore kept a live pointer to the dequeued value at the vacated index until the array eventually reallocated.This change zeroes the head slot before re-slicing and drops the backing array once the queue empties:
Why
The queue is instantiated with reference element types that are long-lived and cycle steadily, e.g.
Queue[*execution.MessageEnvelope](workflow/inproc/runner.go),Queue[workflow.Event](workflow/internal/execution/eventstream.go), andQueue[func(context.Context) error](workflow/inproc/context.go). Because the head slot was never cleared, a dequeued value stayed GC-reachable through the backing array long after it was logically removed, retaining whatever object graph it pointed at until the slice happened to reallocate.Clearing the vacated element mirrors Go's own
slices.Deletesemantics, which zero removed slots precisely so dead references can be collected promptly.How it is tested
Two deterministic tests in
internal/concurrent/queue_test.go:TestQueue_DequeueZeroesVacatedSlotinspects the shared backing array through a full-capacity view and asserts the head slot isnilafterDequeue(while later elements keep the array alive).TestQueue_DequeueDropsEmptyBackingArrayassertsitemsis released tonilonce the queue drains.Both fail before the change and pass after.
go build ./...,go vet ./internal/concurrent/..., andgo test ./internal/concurrent/...all pass.