Skip to content

Use AsyncLocal instead of ThreadStatic for SqlStatement.Current#79

Merged
seesharper merged 1 commit intomasterfrom
fix-sqlstatement-current
Feb 24, 2026
Merged

Use AsyncLocal instead of ThreadStatic for SqlStatement.Current#79
seesharper merged 1 commit intomasterfrom
fix-sqlstatement-current

Conversation

@seesharper
Copy link
Owner

Summary

  • SqlStatement.Current was annotated with [ThreadStatic], which stores values per OS thread
  • With async/await, continuations can resume on a different thread pool thread (especially after .ConfigureAwait(false)), causing SqlStatement.Current to be null or stale in the async read path
  • Replaced with AsyncLocal<string>, which flows through ExecutionContext and correctly tracks the value across await boundaries
  • Public API is unchanged — callers read/write SqlStatement.Current the same way

Test plan

  • All 225 existing tests pass
  • Verify async reads (ReadAsync, ReadWithoutNavigationPropertiesAsync) correctly use the cache key across thread switches

🤖 Generated with Claude Code

ThreadStatic breaks in async contexts where continuations resume on a
different thread pool thread. AsyncLocal flows through ExecutionContext
and correctly tracks the current SQL statement across await boundaries.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@seesharper seesharper merged commit 3774723 into master Feb 24, 2026
2 checks passed
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