Skip to content

Fix MemoryLimitConstraint skipping check when async resumes on a different thread#2480

Merged
lahma merged 1 commit into
sebastienros:mainfrom
brunodeuner:fix/memory-limit-async-thread
May 17, 2026
Merged

Fix MemoryLimitConstraint skipping check when async resumes on a different thread#2480
lahma merged 1 commit into
sebastienros:mainfrom
brunodeuner:fix/memory-limit-async-thread

Conversation

@brunodeuner

@brunodeuner brunodeuner commented May 17, 2026

Copy link
Copy Markdown
Contributor

GC.GetAllocatedBytesForCurrentThread() is per-thread. When an async continuation resumes on a different thread pool thread, Reset() and Check() read counters from different threads, which can silently bypass the limit or produce false positives.

Fix: store the ManagedThreadId in Reset() and skip the check in Check() when the current thread differs. Safe degradation: the constraint is not enforced across thread hops, but no wrong results are produced.

…erent thread

GC.GetAllocatedBytesForCurrentThread() is per-thread, so comparing the
counter captured in Reset() with the one read in Check() is meaningless
when an async continuation resumes on a different thread pool thread.
This can silently bypass the limit or produce false positives.

Store the thread ID in Reset() and skip the check in Check() when the
current thread differs from the one that called Reset().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@lahma
lahma force-pushed the fix/memory-limit-async-thread branch from 35fbbbc to 8023bcd Compare May 17, 2026 16:45

@lahma lahma left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you!

@lahma
lahma enabled auto-merge (squash) May 17, 2026 16:46
@lahma
lahma disabled auto-merge May 17, 2026 16:48
@lahma
lahma enabled auto-merge (squash) May 17, 2026 16:48
@lahma
lahma merged commit ec66147 into sebastienros:main May 17, 2026
4 checks passed
@brunodeuner
brunodeuner deleted the fix/memory-limit-async-thread branch May 18, 2026 01:31
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.

2 participants