Skip to content

Commit 44ea786

Browse files
seesharperclaude
andcommitted
Use AsyncLocal instead of ThreadStatic for SqlStatement.Current
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>
1 parent 15ed9e8 commit 44ea786

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/DbReader/SqlStatement.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
namespace DbReader
22
{
3-
using System;
3+
using System.Threading;
44

55
/// <summary>
66
/// Represents the query currently executing.
77
/// </summary>
88
public class SqlStatement
99
{
10+
private static readonly AsyncLocal<string> _current = new AsyncLocal<string>();
11+
1012
/// <summary>
11-
/// The query currently executing.
13+
/// The query currently executing.
1214
/// </summary>
13-
[ThreadStatic]
14-
public static string Current;
15+
public static string Current
16+
{
17+
get => _current.Value;
18+
set => _current.Value = value;
19+
}
1520
}
1621
}

0 commit comments

Comments
 (0)