Skip to content

Unsynchronized EndTimestamp writes in TransactionTracer.Finish race with IsFinished reads #5174

Closed

Description

@jamescrosswell

Caution

There are only really two a few scenarios where transactions get finished:

  1. In async local apps like ASP.NET Core (end of a request - no concurrency)
  2. 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
  3. 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

TransactionTracer.cs:449-487:

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 186

Race

  • EndTimestamp is Nullable<DateTimeOffset> — a multi-word struct, not atomically writable.
  • One thread executes Finish() and writes EndTimestamp after releasing _finishLock.
  • Another thread reads IsFinished (e.g., the MAUI binder on the UI thread checking CurrentUiTx is { IsFinished: false }).
  • The reader can observe a torn or stale value: IsFinished == false even 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

Activity

  1. added
    Bug
    .NET
    on Apr 30, 2026
  2. linear-code commented on Apr 30, 2026

    @linear-code
  3. jamescrosswell commented on Jul 1, 2026

    @jamescrosswell
    CollaboratorAuthor

    Addressed in 5248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETBugTraces

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions