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

[EventSource] Block EventCommandExecuted callback during initialization #105341

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,16 @@ public event EventHandler<EventCommandEventArgs>? EventCommandExecuted

m_eventCommandExecuted += value;

// If we have an EventHandler<EventCommandEventArgs> attached to the EventSource before the first command arrives
// It should get a chance to handle the deferred commands.
EventCommandEventArgs? deferredCommands = m_deferredCommands;
while (deferredCommands != null)
if (m_completelyInited)
{
value(this, deferredCommands);
deferredCommands = deferredCommands.nextCommand;
// If we have an EventHandler<EventCommandEventArgs> attached to the EventSource before the first command arrives
// It should get a chance to handle the deferred commands.
EventCommandEventArgs? deferredCommands = m_deferredCommands;
while (deferredCommands != null)
{
value(this, deferredCommands);
deferredCommands = deferredCommands.nextCommand;
}
}
}
remove
Expand Down Expand Up @@ -1638,8 +1641,9 @@ private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, str
m_eventPipeProvider = eventPipeProvider;
#endif
Debug.Assert(!m_eventSourceEnabled); // We can't be enabled until we are completely initted.
// We are logically completely initialized at this point.
m_completelyInited = true;
// We are logically completely initialized at this point, but set m_completelyInited after
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
// doing deferred commands under the EventListenersLock to avoid a race with SendCommand which
// can clear deferred commands before they are done.
}
catch (Exception e)
{
Expand All @@ -1660,6 +1664,11 @@ private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, str
DoCommand(deferredCommands); // This can never throw, it catches them and reports the errors.
deferredCommands = deferredCommands.nextCommand;
}

if (m_constructionException == null)
{
m_completelyInited = true;
}
}
}

Expand Down Expand Up @@ -2573,8 +2582,12 @@ internal void DoCommand(EventCommandEventArgs commandArgs)
}

// PRECONDITION: We should be holding the EventListener.EventListenersLock
Debug.Assert(Monitor.IsEntered(EventListener.EventListenersLock));
// We defer commands until we are completely inited. This allows error messages to be sent.
Debug.Assert(m_completelyInited);
// Technically, by this point initialization should be complete, but m_completelyInited
mdh1418 marked this conversation as resolved.
Show resolved Hide resolved
// is only set after processing deferred commands at the end of Initialize to
// avoid possibly double invoking the m_eventCommandExecuted callback should EventCommandExecuted
// be set during OnEventCommand.

if (m_etwProvider == null) // If we failed to construct
return;
Expand Down
Loading