Skip to content

[Utilities] Stop the array pool trim timer from capturing the execution context - #152

Merged
michaelstaib merged 1 commit into
mst/stream-v2from
mst/utilities-pool-timer-context
Nov 2, 2026
Merged

michaelstaib merged 1 commit into
mst/stream-v2from
mst/utilities-pool-timer-context

Conversation

@michaelstaib

Copy link
Copy Markdown
Owner

The trim timer of FixedSizeArrayPool buckets captured the execution context of the code that first created the pool. Any AsyncLocal value that was set at that point, for example a DataLoader scope, stayed reachable for as long as the timer lived.

The bucket now creates its timer with execution context flow suppressed, and restores the flow right afterwards. The timer callback and the trim policy are unchanged. The bucket timer is the only timer in Utilities.Buffers.

Tests:

  • FixedSizeArrayPoolTests.Constructor_Should_NotRetainAsyncLocalValue_When_PoolIsCreatedInScope checks that an AsyncLocal value set while the pool is created can be collected. It fails without the fix.
  • Utilities.Tests: 157/157 on net10.0 and net8.0.

No benchmark was run. The change runs once per bucket creation, not when arrays are rented or returned.

Copilot AI balanced review requested due to automatic review settings November 1, 2026 09:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the change to prevent pool timers from retaining ambient AsyncLocal values.

No open issues 🎉

What changed in this PR

Prevents FixedSizeArrayPool bucket timers from retaining ambient AsyncLocal values, avoiding unintended object retention for the timer’s lifetime.

Changes:

  • Suppresses execution context flow during timer creation and restores the prior flow state afterward, leaving callback and trimming behavior unchanged.
  • Adds a regression test verifying that an AsyncLocal value can be collected while the pool remains alive.
File Description
src/​HotChocolate/​Utilities/​test/​Utilities.Tests/​FixedSizeArrayPoolTests.cs Tests collection of an ambient AsyncLocal value after pool creation.
src/​HotChocolate/​Utilities/​src/​Utilities.Buffers/​FixedSizeArrayPool.cs Creates trim timers without capturing the execution context.

Review effort: Balanced

@michaelstaib
michaelstaib merged commit ea80b45 into mst/stream-v2 Nov 2, 2026
2 checks passed
@michaelstaib
michaelstaib deleted the mst/utilities-pool-timer-context branch November 2, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants