Skip to content

Conversation

@elinor-fung
Copy link
Member

@elinor-fung elinor-fung commented Oct 25, 2022

Fix #75760

For tracing, we had a static for the mutex which has a destructor. If a thread calls exit, the static destructor was called on that thread with no synchronization with other threads, resulting in potential issues where they would try to use the destructed object.

This change switches to using a spin lock. In the majority of cases (running an application, tracing not enabled, no errors) we won't use it at all. When we do (tracing explicitly enabled, errors, commands for printing info), we don't expect much contention, since most hosting is on a single thread.

Related:

@ghost
Copy link

ghost commented Oct 25, 2022

Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

Issue Details

Fix #75760

For tracing, we had a static for the mutex which has a destructor. If a thread calls exit, the static destructor was called on that thread with no synchronization with other threads, resulting in potential issues where they would try to use the destructed object.

This change just stores the pointer instead. It does have the following effects:

  • Mutex is only created when needed - in the majority of cases (running an application, tracing not enabled, no errors) it is not needed
  • If it is created, it does not get deleted - it is left around until the process exists

Related:

Author: elinor-fung
Assignees: -
Labels:

area-Host

Milestone: -

@mgexo
Copy link

mgexo commented Oct 27, 2022

On a side, note, I really do not understand why you didnt check for g_trace_file != nullptr in trace::flush() before you even lock.
Thats what I also wrote in #75760 because in most (probably 99% of all environments), the trace feature is not even used and there is simply a useless lock at the end

@elinor-fung
Copy link
Member Author

Avoiding the lock at the end when it is not needed is definitely reasonable - and an improvement I will look at. However, the underlying root cause for the issue is the static object with a destructor, so that is what I wish to fix here.

@elinor-fung elinor-fung merged commit 55d3523 into dotnet:main Oct 28, 2022
@elinor-fung elinor-fung deleted the issue75760 branch October 28, 2022 03:58
@ghost ghost locked as resolved and limited conversation to collaborators Nov 27, 2022
@elinor-fung
Copy link
Member Author

/backport to release/6.0-staging

@github-actions github-actions bot unlocked this conversation Apr 18, 2024
@github-actions
Copy link
Contributor

Started backporting to release/6.0-staging: https://github.com/dotnet/runtime/actions/runs/8744005173

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

4 participants