Skip to content

Build dotnet-monitor tool as ASP.NET Core 3.1 application. - #1420

Merged
jander-msft merged 10 commits into
dotnet:masterfrom
jander-msft:dev/jander/monitor3.1
Aug 18, 2020
Merged

jander-msft merged 10 commits into
dotnet:masterfrom
jander-msft:dev/jander/monitor3.1

Conversation

@jander-msft

@jander-msft jander-msft commented Aug 7, 2020 •

Copy link
Copy Markdown
Contributor

This change has dotnet-monitor built for ASP.NET Core 3.1.

One major benefit is that when it is published as a tool it doesn't have to package the various Microsoft.AspNetCore.* and Microsoft.Extensions.* assemblies from .NET 2.1, which minimizes the size of the tool (as netcoreapp2.1, it's size was 22.3 MB; as netcoreapp3.1, it's size is 7.3 MB).

Filtering of the dotnet-monitor process has been implemented in a new IEndpointInfoSource implementation that is specific to the dotnet-monitor tool project; class is named FilteredEndpointInfoSource. It also selects the IEndpointInfoSource implementation based on options configuration provided by dependency injection. An additional benefit this refactor provides is that the ServerEndpoingInfoSource is now properly disposed by the service container whereas before it was not disposed because it was created outside of the service container.

closes #1270

Comment thread src/Tools/dotnet-monitor/Startup.cs Resolved
Comment thread src/Tools/dotnet-monitor/Startup.cs Resolved
@jander-msft

Copy link
Copy Markdown
Contributor Author

@noahfalk @josalem, if there is no additional feedback on this, I'll check this in tomorrow (to allow for more feedback over the next day).

@josalem josalem left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this looks good to me 😃. The only scenario that might be worth testing would be configuring dotnet monitor with a diagnostic port that it creates itself. If there were dragons lurking that's where I imagine they'd be, but I can't think of anything immediately problematic. The runtime is capable of connecting to itself via the default listen port, so I would wager that the same should be true of a connect port.

@jander-msft

jander-msft commented Aug 14, 2020 •

Copy link
Copy Markdown
Contributor Author

I think this looks good to me 😃. The only scenario that might be worth testing would be configuring dotnet monitor with a diagnostic port that it creates itself. If there were dragons lurking that's where I imagine they'd be, but I can't think of anything immediately problematic. The runtime is capable of connecting to itself via the default listen port, so I would wager that the same should be true of a connect port.

@josalem, one thing that I do notice, which blocks this test from working, is that if a runtime instance starts up in connect mode and the port that it is trying to connect to doesn't exist (e.g. dotnet-monitor is not running yet), the runtime instance will forever ignore anything that establishes that port in the future. So if it pauses on start, then the runtime just hangs forever. If it doesn't pause on start, then it continues on running and ignores the port.

Without disabling the pause on start, having the dotnet-monitor server listen to itself will block forever (the server never comes up because managed code execution is blocked until the server sends the ResumeRuntime command... chicken-and-egg). With disabling pause on start, the dotnet-monitor server doesn't see itself because the runtime gives up on connecting to the port.

@josalem

josalem commented Aug 14, 2020 •

Copy link
Copy Markdown
Contributor

With disabling pause on start, the dotnet-monitor server doesn't see itself because the runtime gives up on connecting to the port.

How do you mean? If the port is configured to suspend it would make sense that it would hang (chicken-egg problem you mentioned), but if it isn’t configured to suspend, the runtime should continually try to connect to the port with an exponential falloff. The logic isn’t supposed to give up on connecting to the port, it will just retry after a timeout if it can’t connect. If I recall the max timeout is 250ms. Are you seeing it never connect to itself?

@noahfalk noahfalk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@jander-msft

Copy link
Copy Markdown
Contributor Author

With disabling pause on start, the dotnet-monitor server doesn't see itself because the runtime gives up on connecting to the port.

How do you mean? If the port is configured to suspend it would make sense that it would hang (chicken-egg problem you mentioned), but if it isn’t configured to suspend, the runtime should continually try to connect to the port with an exponential falloff. The logic isn’t supposed to give up on connecting to the port, it will just retry after a timeout if it can’t connect. If I recall the max timeout is 250ms. Are you seeing it never connect to itself?

@josalem, sorry, I miscalled what the actual problem was. The "have dotnet-monitor connect to itself" test should work if I try it again.

The problem that I was recalling is that if a runtime instance is configured to connect to a diagnostic port, a diagnostic tool then hosts its listening port at that location (which works fine), then the diagnostic tool shuts down, and finally a diagnostic tool hosts its listening port at the same location, then the runtime instance will not connect to the second instance of the tool or any subsequent tools that host their listening port at that location.

@josalem

josalem commented Aug 14, 2020

Copy link
Copy Markdown
Contributor

then the diagnostic tool shuts down, and finally a diagnostic tool hosts its listening port at the same location, then the runtime instance will not connect to the second instance of the tool or any subsequent tools that host their listening port at that location.

@jander-msft, that's troubling. I have tests in the runtime repo that specifically check for the scenario of a tool closing its port and creating it again. While the test is pathological and does this in a tight loop, I would expect a more normal scenario like you described to work cleanly.

https://github.com/dotnet/runtime/blob/master/src/tests/tracing/eventpipe/reverseouter/reverseouter.cs

Specifically, this snippet here: https://github.com/dotnet/runtime/blob/1e6e8d9c8faa9a0dd51c2de67fcf1f5e0a4a1ed5/src/tests/tracing/eventpipe/reverseouter/reverseouter.cs#L71-L75

CreateServerAndReceiveAdvertisement does as its name implies and creates the server, accepts the connection, parses the advertisement, then shuts down the server completely. That involves stopping any listening and unlinking the socket/namedpipe. I would expect a tool shutting down to go through the same behavior as this test. Is it possible that the reverse server as implemented in this repo isn't cleaning up the socket entirely?

For reference, this is how the server is cleaned up in those tests:
https://github.com/dotnet/runtime/blob/ef4bc4d503b71f9de1a8988da6db38bcfcfb0cc4/src/tests/tracing/eventpipe/common/Reverse.cs#L152-L180

@jander-msft

Copy link
Copy Markdown
Contributor Author

then the diagnostic tool shuts down, and finally a diagnostic tool hosts its listening port at the same location, then the runtime instance will not connect to the second instance of the tool or any subsequent tools that host their listening port at that location.

@jander-msft, that's troubling. I have tests in the runtime repo that specifically check for the scenario of a tool closing its port and creating it again. While the test is pathological and does this in a tight loop, I would expect a more normal scenario like you described to work cleanly.

https://github.com/dotnet/runtime/blob/master/src/tests/tracing/eventpipe/reverseouter/reverseouter.cs

Specifically, this snippet here: https://github.com/dotnet/runtime/blob/1e6e8d9c8faa9a0dd51c2de67fcf1f5e0a4a1ed5/src/tests/tracing/eventpipe/reverseouter/reverseouter.cs#L71-L75

CreateServerAndReceiveAdvertisement does as its name implies and creates the server, accepts the connection, parses the advertisement, then shuts down the server completely. That involves stopping any listening and unlinking the socket/namedpipe. I would expect a tool shutting down to go through the same behavior as this test. Is it possible that the reverse server as implemented in this repo isn't cleaning up the socket entirely?

For reference, this is how the server is cleaned up in those tests:
https://github.com/dotnet/runtime/blob/ef4bc4d503b71f9de1a8988da6db38bcfcfb0cc4/src/tests/tracing/eventpipe/common/Reverse.cs#L152-L180

I'm observing this behavior on my local Windows machine, which is using named pipes, so there are no remnant files that should be interfering with this.

Refactor to computer filters only once for lifetime of DiagnosticServices instance.
@josalem

josalem commented Aug 14, 2020

Copy link
Copy Markdown
Contributor

I'm observing this behavior on my local Windows machine

Those tests run and pass on Windows too, so I'd be equally troubled if that were happening there as well.

@jander-msft

Copy link
Copy Markdown
Contributor Author

Those tests run and pass on Windows too, so I'd be equally troubled if that were happening there as well.

What I've found is that if I have dotnet-monitor call stream.Disconnect before disposing it, the the runtime instance will reconnect when the server comes back up later. That tells me that the runtime instance is expecting the diagnostic pipe/socket to be explicitly disconnected/closed before it tries to reconnect again. I think this would need to be fixed in the runtime because either something is going to forget to gracefully disconnect the stream (e.g. dotnet-monitor), something malicious could just not do it, or the diagnostic tool process could crash and not have a chance to send that disconnect.

@josalem

josalem commented Aug 15, 2020

Copy link
Copy Markdown
Contributor

That tells me that the runtime instance is expecting the diagnostic pipe/socket to be explicitly disconnected/closed before it tries to reconnect again.

I'll look at this. I would expect the kernel to clean up any open NamedPipe handles if a server process were to crash/close without gracefully shutting down a pipe. Based on the docs, I would expect a call to ReadFile including an overlapped IO call, would error and return ERROR_BROKEN_PIPE, much like the EPIPE/SIGPIPE for the equivalent scenario on Linux. If that isn't happening, I'm not sure there is anything we could do to identify that scenario without adding additional polling. One thing that could be happening is that the ReadFile might be failing, but the overlapped IO event handle hasn't been set... I'll take a look early next week.

@noahfalk noahfalk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM (with the caveat I haven't been paying too close attention to the higher in the stack components)

@jander-msft
jander-msft merged commit e8f0ea9 into dotnet:master Aug 18, 2020
@jander-msft
jander-msft deleted the dev/jander/monitor3.1 branch August 18, 2020 23:40
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dotnet-monitor should not detect itself as a target process

5 participants