Repository navigation
Description
Caution
There are only really two a few scenarios where transactions get finished:
- In async local apps like ASP.NET Core (end of a request - no concurrency)
- In Global apps in something like a using block, when the transaction passes out of lexical scope or manually at the end of a block of code that's being instrumented
- As of recently, in Maui Apps when the idle timeout triggers...
I think 3 is the only case where we might have real concurrency issues. Need to explore the code in Auto-create traces for MAUI UI events #5116 and check what/when transactions get finished. I think it there's a timer, we always wait for the timeout and that finishes the transaction - so again, no concurrency issue... unless SDK users get the transaction from the scope and try to finish it manually for some reason at the same time that the timeout triggers
Summary
TransactionTracer.Finish() writes EndTimestamp outside _finishLock, while IsFinished (a public property) reads EndTimestamp from arbitrary threads. This is a pre-existing race that is not introduced by any specific PR — it has been present since _finishLock was added.
Code
public void Finish()
{
if (!TryFinishOnce()) // ← takes & releases _finishLock, sets _hasFinished
{
return;
}
...
if (_idleTimeout.HasValue)
{
...
EndTimestamp = latestSpanEnd ?? _stopwatch.CurrentDateTimeOffset; // ← outside lock
}
else
{
EndTimestamp ??= _stopwatch.CurrentDateTimeOffset; // ← outside lock
}
...
}public DateTimeOffset? EndTimestamp { get; internal set; } // line 73
public bool IsFinished => EndTimestamp is not null; // line 186Race
EndTimestampisNullable<DateTimeOffset>— a multi-word struct, not atomically writable.- One thread executes
Finish()and writesEndTimestampafter releasing_finishLock. - Another thread reads
IsFinished(e.g., the MAUI binder on the UI thread checkingCurrentUiTx is { IsFinished: false }). - The reader can observe a torn or stale value:
IsFinished == falseeven though the transaction has logically finished, or briefly inconsistent intermediate state of the nullable struct.
A narrower instance of this race in the OnIdleTimeout discard path was fixed in PR #5138 by moving the write inside the lock. This issue covers the broader pre-existing instance in Finish().
Suggested fix options
Option A (minimal): move both EndTimestamp writes inside _finishLock. Requires either restructuring Finish() to do its EndTimestamp computation inside the lock, or taking the lock a second time around the writes.
Option B (cleaner): change IsFinished to read from the existing _hasFinished field (a bool, atomic on .NET, already protected by _finishLock) instead of EndTimestamp. This is the property's intent, and _hasFinished is the authoritative flag the SDK already uses internally. Public consumers reading IsFinished would then see a properly synchronized value without needing any locking on the read side.
Option B is preferred — it fixes the race at the read boundary and makes IsFinished cheaper for callers.
Practical impact
Low-to-medium. Torn reads of DateTimeOffset? are rare on x64. Stale reads can cause callers (notably MauiEventsBinder) to briefly act on a finished transaction. No data corruption — just transient logical inconsistencies in a small race window.
References
- Originally flagged in scope of the discard path: feat: Automatic trace instrumentation for MAUI #5138 (comment)
- Discard-path fix (in PR feat: Automatic trace instrumentation for MAUI #5138): writes
EndTimestampinside_finishLock. - This issue tracks the broader pre-existing instance in
Finish().
Activity
linear-code commentedon Apr 30, 2026
NET-667
jamescrosswell commentedon Jul 1, 2026
Addressed in 5248