Skip to content

Race condition during TraceSource Initialization/Refresh #104457

Closed

Description

@h3xds1nz

Description

TraceSource is documented to be a thread-safe, however when you call System.Diagnostics.Trace.Refresh() during TraceSource initialization from a different thread, you have a high chance of receiving either InvalidOperationException or NullReferenceException from the OnInitializing method during the enumeration of Listeners.

There seem to be a lack of synchronization on the collection, this seem to have been introduced in #73087

Reproduction Steps

You can find a demo repository on Github - here.

Expected behavior

I can call System.Diagnostics.Trace.Refresh() safely from a different thread during underlying TraceSource initialization without the risk of crashing.

Actual behavior

A high chance of receiving either InvalidOperationException or NullReferenceException from the OnInitializing method during the enumeration of Listeners as the collection might have been modified or initialized from a different thread.

Regression?

Since #73087, previous .NET version (6) didn't suffer from this issue.

Known Workarounds

In case of WPF, wait some time before invoking a Refresh.

Configuration

.NET 8.0.6 x64 Win10

Activity

  1. ghost added
    needs-area-label
    on Jul 5, 2024
  2. added
    untriaged
    on Jul 5, 2024
  3. added and removed
    needs-area-label
    on Jul 5, 2024
  4. dotnet-policy-service commented on Jul 5, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti
    See info in area-owners.md if you want to be subscribed.

  5. tommcdon commented on Jul 6, 2024

    @tommcdon
    Member
  6. removed
    untriaged
    on Jul 17, 2024
  7. added this to the 9.0.0 milestone on Jul 17, 2024
  8. ericstj commented on Jul 17, 2024

    @ericstj
    Member

    @steveharter can you have a look at this?

  9. added
    in-pr
    on Aug 9, 2024
  10. locked and limited conversation to collaborators on Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Diagnostics.TraceSourcein-prregression-from-last-release

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions