Skip to content

EventPipeEventSource constructor blocks until the first event is read #1172

Closed

Description

Moving dotnet/diagnostics#1060 to here because this repo is a better place to keep track of this issue:

From David Mason (@davmason):

If you follow the sample code on how to use EventPipeEventSource but replace the provider with a provider that doesn't produce any events, it will block forever in the constructor to EventPipeEventSource.

    var providers = new List<EventPipeProvider>()
    {
        new EventPipeProvider("Microsoft-Windows-DotNETRuntime",
            EventLevel.Informational, (long)ClrTraceEventParser.Keywords.GC)
    };

    var client = new DiagnosticsClient(processId);
    using (var session = client.StartEventPipeSession(providers, false))
    {
        var source = new EventPipeEventSource(session.EventStream);

        source.Clr.All += (TraceEvent obj) => {
            Console.WriteLine(obj.EventName);
        }
        try
        {
            source.Process();
        }
        // NOTE: This exception does not currently exist. It is something that needs to be added to TraceEvent.
        catch (EventStreamException e)
        {
            Console.WriteLine("Error encountered while processing events");
            Console.WriteLine(e.ToString());
        }
    }

It will block on this line:

var entryObj = _deserializer.GetEntryObject(); // this call invokes FromStream and reads header data

This seems like a pitfall, and it's not clear what is happening. I've used that code pattern lots before and it's never failed, then all of a sudden it hangs my app and I have to do a lot of digging to understand why. I imagine customers will run in to it as well.

Activity

  1. raffaeler commented on Dec 10, 2020

    @raffaeler

    This is very problematic when we try to hook the very first events coming from a process.
    As the constructor is blocked, the events cannot be subscribed and therefore there is a high chance to lose the first events.

  2. brianrob commented on Jan 12, 2021

    @brianrob
    Member

    Sung Yoon Whang (@sywhang), are you planning to fix this? If I understand correctly, this sounds like an issue that is specific to live sessions.

  3. sywhang commented on Jan 12, 2021

    @sywhang
    ContributorAuthor

    Sure. I can take a look at this some time this week.

  4. brianrob commented on Jan 12, 2021

    @brianrob
    Member
  5. mwasplund-msft commented on Dec 7, 2021

    @mwasplund-msft

    It looks like Sung Yoon Whang (@sywhang) is no longer at Microsoft, Brian Robbins (@brianrob) can someone else take a look at this?

  6. brianrob commented on Jan 3, 2022

    @brianrob
    Member

    John Salem (@josalem) can you take a look at this now that Sung Yoon Whang (@sywhang) is no longer at Microsoft? Thinking about this one a bit, I think that it's reasonable to block when waiting for the next event after Process is called, but we shouldn't be blocking in the constructor. This would be consistent with how ETW real-time sessions work.

  7. josalem commented on Jan 3, 2022

    @josalem
    Contributor

    Can do. This is a recurring issue and is unexpected behavior I would argue. I think care may need to be taken on "fixing" this, however, as this has been the behavior for a long while and changing it without an opt-in has the potential to break existing tools if they update their TraceEvent version.

  8. brianrob commented on Jan 3, 2022

    @brianrob
    Member

    Thanks John. To be honest, I'm not concerned about putting this behind a flag unless it has other more far reaching implications. If you're concerned, one thing you could consider is submitting it to the feature/perfview-3.0 branch. I'm batching up some larger changes that I'm going to release with a 3.0 version, and so this could be part of that.

  9. davmason commented on Jan 4, 2022

    @davmason
    Contributor

    +1 to not requiring a flag. It is technically a behavior change, but it's hard to imagine anyone relying on it.

    I have helped multiple people figure out why their code was seemingly deadlocked, it is hard/impossible for someone unfamiliar with the internal of TraceEvent to figure out what is happening.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions