Skip to content

Non-blocking EventPipeEventSource Ctor - #1588

Merged
Brian Robbins (brianrob) merged 4 commits into
microsoft:mainfrom
josalem:dev/josalem/nonblocking-eventpipeeventsource
Mar 31, 2022
Merged

Brian Robbins (brianrob) merged 4 commits into
microsoft:mainfrom
josalem:dev/josalem/nonblocking-eventpipeeventsource

Conversation

@josalem

@josalem John Salem (josalem) commented Mar 14, 2022 •

Copy link
Copy Markdown
Contributor

This PR makes the constructor for EventPipeEventSource non-blocking. Previously, the constructor would block until data was sent on the stream. This brings EventPipeEventSource closer in behavior to other similar APIs in the TraceEvent library.

One issue that isn't solved by this PR (yet) is that we need to handle the case where users try to get metadata about the source before data arrives. Before this PR, users were guaranteed to have that information when the ctor returned. Now, this is only true for opening a completed file, hence the special casing for files. Currently, this PR makes no attempt to stop users from reading metadata properties. I am hoping for some feedback on whether this should throw an exception or now. Until this is decided, I'll leave this in draft mode. I'm going to leave this alone for now, and address it if users are hitting this behavior. The only use case for these Properties seems to be covered fine by differentiating streaming vs non-streaming.

fixes #1172

@noahfalk

Copy link
Copy Markdown
Member

Currently, this PR makes no attempt to stop users from reading metadata properties

I'd say throwing is desirable if the values aren't initialized yet, but if it was a significant hassle to implement I don't think it would be awful to return some default 0/null/empty string values instead. What do you think Brian Robbins (@brianrob)?

@josalem

Copy link
Copy Markdown
Contributor Author

I'd say throwing is desirable if the values aren't initialized yet

Agreed.

There are Debug.Asserts scattered around that check whether this metadata is set before certain operations. These are mostly buried in the TraceLog APIs from what I saw. These caused the file-based tests to fail until I special cased the file-based ctor.

Depending on Brian Robbins (@brianrob)'s thoughts, I'll look at making these properties throw if Process hasn't been called yet.

@brianrob

Copy link
Copy Markdown
Member

I would tend to agree that if there is metadata that can't be filled before returning from the constructor, then throwing is a good idea. If you look at how we handle realtime ETW sessions, the session is created and setup before you get the source from it, which means that the source can make calls to the session to get metadata. Would such a pattern help here? If not, I would say throwing is the next best thing.

@josalem

Copy link
Copy Markdown
Contributor Author

Would such a pattern help here?

I think yes, however, the API for configuring, starting, and stopping an EventPipe session is in the diagnostics client library and not in TraceEvent. That makes it a little more complicated to make this pattern happen.

Right now, the flow for "trace on startup" is this:

  1. [Diag Client Lib] Create configuration (list of providers, buffersize, etc.)
  2. [Diag Client Lib] Start EventPipe session (get Stream object)
  3. [TraceEvent lib] Create EventPipeEventSource using EventPipeEventSource(Stream stream) ctor
  4. [TraceEvent lib] Call EventPipeEventSource.Process()
  5. [Diag Client lib] Resume target
  6. [Diag Client lib] Stop EventPipe session

In the "trace on startup" scenario, we need to wait for the target process to resume before the session starts. Without this change, the ctor call in step 3 would block until we resume the target. After this change, the ctor would complete, but the metadata wouldn't be populated yet. Any usage of that EventPipeEventSource before resuming the target could cause errors in other parts of TraceEvent regardless of whether EventPipeEventSource.Process has been called. The metadata should only be present after step 5.

@brianrob

Copy link
Copy Markdown
Member

Gotcha. So this becomes a bit more complicated.

I think it is OK to simplify and just throw in APIs that don't have data yet. Just wanted to make sure that there wasn't another pattern that might help.

@josalem
John Salem (josalem) marked this pull request as ready for review March 30, 2022 22:54
@brianrob

Copy link
Copy Markdown
Member

John Salem (@josalem), I see that you just pushed another commit here. Please let me know when this is ready for another review, or is ready for merge. Thanks.

@josalem

Copy link
Copy Markdown
Contributor Author

I think this is ready for final review and merge at your discretion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EventPipeEventSource constructor blocks until the first event is read

3 participants