Skip to content

Use Process.StartAndForget for fire-and-forget process launches - #54093

Merged
adamsitnik merged 12 commits into
mainfrom
copilot/update-process-start-usage
Jun 25, 2026
Merged

adamsitnik merged 12 commits into
mainfrom
copilot/update-process-start-usage

Conversation

Copilot AI commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Adopt Process.StartAndForget (dotnet/runtime#126078) at call sites where Process.Start was used in a fire-and-forget pattern, leaking the returned Process handle.

  • CleanFileBasedAppArtifactsCommand.cs — cleanup subprocess launched with discarded return value. The previous code redirected standard streams but never consumed them, which could lead to deadlocks; StartAndForget redirects to the null device by default.
  • ServerConnection.cs — Razor server subprocess launched, only .Id read from the handle. Uses #if NETFRAMEWORK guards to retain the original Process.Start + redirect behavior on net472 (where StartAndForget is unavailable) while using StartAndForget on .NET Core+.
- Process.Start(startInfo);
+ _ = Process.StartAndForget(startInfo);
+#if NETFRAMEWORK
+                    using Process process = Process.Start(startInfo);
+                    processId = process.Id;
+#else
+                    processId = Process.StartAndForget(startInfo);
+#endif

Note: Will not compile until MicrosoftNETCoreAppRefPackageVersion is updated to a build containing Process.StartAndForget.

@adamsitnik adamsitnik 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.

@copilot please address my feedback

Copilot AI requested a review from adamsitnik June 12, 2026 13:47
@adamsitnik
adamsitnik marked this pull request as ready for review June 12, 2026 14:31
@adamsitnik
adamsitnik requested review from a team and Copilot June 12, 2026 14:31

Copilot AI 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.

Pull request overview

This PR updates fire-and-forget subprocess launches to use Process.StartAndForget(...) so call sites don’t leak Process handles, and (where applicable) avoid redirected stdio pipes that can deadlock when not drained.

Changes:

  • Switch CleanFileBasedAppArtifactsCommand automatic cleanup launch from Process.Start to Process.StartAndForget, removing unused stdio redirection.
  • Update Razor server launch logic to use Process.StartAndForget on non-.NET Framework targets, while retaining a .NET Framework-specific path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/RazorSdk/Tool/ServerProtocol/ServerConnection.cs Uses Process.StartAndForget for fire-and-forget server startup (with NETFRAMEWORK fallback and redirect guards).
src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommand.cs Starts automatic cleanup via Process.StartAndForget and removes unused stdio redirection.

@Youssef1313 Youssef1313 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.

Overall LGTM

@jjonescz for another look for file-based apps clean.

@davidwengier maybe for the Razor part

@adamsitnik
adamsitnik merged commit 74dc3d3 into main Jun 25, 2026
27 checks passed
@adamsitnik
adamsitnik deleted the copilot/update-process-start-usage branch June 25, 2026 12:38
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 29, 2026
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.

5 participants