Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock in TestUtilities.TestEventListener #101493

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/libraries/Common/tests/TestUtilities/TestEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,44 @@ public TestEventListener(ITestOutputHelper output, params string[] sourceNames)

public TestEventListener(Action<string> writeFunc, params string[] sourceNames)
{
List<EventSource> eventSources = _eventSources;

lock (this)
{
_writeFunc = writeFunc;
_sourceNames = new HashSet<string>(sourceNames);
foreach (EventSource eventSource in _eventSources)
_eventSources = null;
}

// eventSources were populated in the base ctor and are now owned by this thread, enable them now.
foreach (EventSource eventSource in eventSources)
{
if (_sourceNames.Contains(eventSource.Name))
{
OnEventSourceCreated(eventSource);
EnableEvents(eventSource, EventLevel.LogAlways);
}
_eventSources = null;
}
}

protected override void OnEventSourceCreated(EventSource eventSource)
{
lock (this)
// We're likely called from base ctor, if so, just save the event source for later initialization.
if (_sourceNames is null)
{
// We're called from base ctor, just save the event source for later initialization.
if (_sourceNames is null)
lock (this)
{
_eventSources.Add(eventSource);
return;
if (_sourceNames is null)
{
_eventSources.Add(eventSource);
return;
}
}
}

// Second pass called from our ctor, allow logging for specified source names.
if (_sourceNames.Contains(eventSource.Name))
{
EnableEvents(eventSource, EventLevel.LogAlways);
}
// Second pass called after our ctor, allow logging for specified source names.
Copy link
Member

@CarnaViire CarnaViire Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't do a second pass now, do we? You've removed the OnEventSourceCreated call...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see from the callstack above, scratch that

if (_sourceNames.Contains(eventSource.Name))
{
EnableEvents(eventSource, EventLevel.LogAlways);
}
}

Expand All @@ -102,7 +112,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
}
try
{
_writeFunc(sb.ToString());
_writeFunc?.Invoke(sb.ToString());
}
catch { }
}
Expand Down