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

Cache process info where possible and dispose Process objects. #11274

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Erarndt
Copy link

@Erarndt Erarndt commented Jan 13, 2025

Fixes #

Context

There are multiple places where information about the current process is retrieved. There is a non-trivial cost to getting this information and the Process objects should be disposed to avoid additional GC cost due to the finalizer running. This change adds properties in EnvironmentUtilities that cache info such as the current process ID (this implementation is copied from .NET 6), and in some instances, updates some callers to dispose of the Process object for uncommon usages.

Changes Made

Testing

Notes

@JanKrivanek
Copy link
Member

Related to #11160

@Erarndt Erarndt marked this pull request as ready for review January 15, 2025 16:42
#endif

#if !NETCOREAPP
private static volatile int s_processId;
Copy link
Member

Choose a reason for hiding this comment

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

Are we fine with defaults or do we want to be explicit in the initial value?
e.g. private static volatile int s_processId = 0 or something.

@SimaTian SimaTian requested a review from a team as a code owner January 20, 2025 09:14
@SimaTian SimaTian force-pushed the dev/erarndt/disposeProcess branch from 25b098e to 8a74a3e Compare January 20, 2025 09:22
@SimaTian
Copy link
Member

I tried rebasing and/or merging with main, which made a mess.
In the end, I opted for checking out main, cherrypicking the commit & then fixing one issue with some old files being re-added by it.
Review will follow.
cc: @Erarndt

@@ -40,6 +40,7 @@
<Compile Include="..\Shared\BinaryWriterExtensions.cs">
<Link>Shared\BinaryWriterExtensions.cs</Link>
</Compile>
<Compile Include="..\Shared\EnvironmentUtilities.cs" Link="EnvironmentUtilities.cs" />
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, how long is this construct around please? (e.g. in other places we use an extra tag, while here we use an attribute)

using Process currentProcess = Process.GetCurrentProcess();
Interlocked.CompareExchange(ref s_processPath, currentProcess.MainModule.FileName ?? "", null);
processPath = s_processPath;
Debug.Assert(processPath != null);
Copy link
Member

Choose a reason for hiding this comment

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

Under which circumstance would this happen? (I noticed that there is a check that throws in src/MSBuild/MSBuildClientApp.cs and here we have an assert stating that we really don't want this to happen while debugging.)

@@ -859,6 +858,14 @@ private NodeEngineShutdownReason HandleShutdown()
#endif

return _shutdownReason;

static StreamWriter GetDebugWriter(bool debugCommunications)
Copy link
Member

Choose a reason for hiding this comment

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

Can we use something like

if (_debugCommunications) 
{
   using StreamWriter debugWriter = File.CreateText(string.Format(CultureInfo.CurrentCulture,  Path.Combine(FileUtilities.TempFileDirectory, @"MSBuild_NodeShutdown_{0}.txt"), Process.GetCurrentProcess().Id))
   debugWriter.WriteLine("Node shutting down with reason {0}.", _shutdownReason);
}

Or something else altogether if anyone has an idea for a cleaner answer.
I found it somewhat difficult to reason about the previous state or the proposed solution:

  • _debugCommunications is a local private bool
  • which we then feed into a static wrapper function
  • that either gives us nothing or a stream
  • and if that stream exists, we write into it

Copy link
Member

@SimaTian SimaTian left a comment

Choose a reason for hiding this comment

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

I've left some comments. Overall I like it.
Ping me when ready please and I'll approve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants