Skip to content

Fix certificate manager process deadlocks in .NET 10 - #69051

Merged
wtgodbe merged 10 commits into
release/10.0from
copilot/fix-certificate-managers-deadlocks-dotnet-10
Sep 9, 2026
Merged

wtgodbe merged 10 commits into
release/10.0from
copilot/fix-certificate-managers-deadlocks-dotnet-10

Conversation

Copilot AI commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Fix certificate manager process deadlocks in .NET 10

Fixes a deadlock that can happen intermittently when starting child processes in certificate managers (on macOS and Linux). These were fixed in .NET 11 using new Process APIs introduced in .NET 11 (#66727), but the fix can't be backported as-is, unfortunately. This fix is development for release/10.0 specifically.

Description

Starting a child process can deadlock if the child process decides to write a certain number of bytes that exceeds the pipe buffer size. This happens because we redirect the stdout and/or stderr without reading it. Once the buffer is full, the child process can't progress as it's waiting for a space in the buffer to write more data, and the parent process is never reading the data and is only waiting for the child process to exit, causing a deadlock.

Fixes #69049

Customer Impact

The main motivation is to unblock infra issues as we started observing this in release/10.0. However, users can also in theory face this, and there was a report about it in the past (see #65518)

Regression?

No

Risk

Low

Code change is very scoped, we know for sure the existing way of running a child process is wrong.

Verification

This kind of randomness is hard to validate properly E2E. But we will definitely observe it in our infra stability once this flows to SDK and flows back to us.

Packaging changes reviewed?

N/A

Copilot AI linked an issue Sep 4, 2026 that may be closed by this pull request
1 task done
Copilot AI changed the title [WIP] Fix certificate managers deadlocks in .NET 10 Fix certificate manager process deadlocks in .NET 10 Sep 4, 2026
Copilot AI requested a review from Youssef1313 September 4, 2026 11:32
Copilot AI and others added 2 commits September 7, 2026 09:14
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
@Youssef1313
Youssef1313 force-pushed the copilot/fix-certificate-managers-deadlocks-dotnet-10 branch from c175323 to 147f034 Compare September 7, 2026 07:14
@Youssef1313
Youssef1313 marked this pull request as ready for review September 7, 2026 14:42
Copilot AI lite review requested due to automatic review settings September 7, 2026 14:42

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.

🟡 Changes recommended

The PR description claims new focused automated tests were added, but no tests in the repo reference the new runner, so the backport lacks the stated verification.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR backports a deadlock-prevention fix for running external certificate-management commands on Unix/macOS in .NET 10 by centralizing redirected-process execution into a helper that drains stdout/stderr while waiting for exit.

Changes:

  • Added CertificateManagerProcessRunner to safely execute redirected processes and capture stdout/stderr.
  • Updated UnixCertificateManager call sites (NSS/OpenSSL/WSL trust paths) to use the runner instead of Process.Start(...).WaitForExit() patterns.
  • Updated MacOSCertificateManager call sites (trust verification, keychain add/remove) to use the runner and preserve captured output for error reporting.
File summaries
File Description
src/Shared/CertificateGeneration/UnixCertificateManager.cs Switches redirected process execution to the new runner to avoid stdout/stderr pipe deadlocks.
src/Shared/CertificateGeneration/MacOSCertificateManager.cs Switches keychain/trust command execution to the new runner and uses captured output in error paths.
src/Shared/CertificateGeneration/CertificateManagerProcessRunner.cs Introduces a shared runner that concurrently drains redirected stdout/stderr and returns exit code plus captured output.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Youssef1313 Youssef1313 added the Servicing-consider Shiproom approval is required for the issue label Sep 7, 2026

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

@Youssef1313 Please take a look at my comments. Thank you for fixing the bug!

process.WaitForExit();

var standardOutput = standardOutputTask.GetAwaiter().GetResult();
var standardError = standardErrorTask.GetAwaiter().GetResult();

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.

This code is valid and should not block, however I don't feel 100% confident in all possible scenarios with GetAwaiter().GetResult(). I think it would be better to use the new line events and the BeginRead* methods.

Run (discarding out and error):

// Send the output and error streams to empty handlers to avoid allocating large strings
process.OutputDataReceived += static (sender, e) => { };
process.ErrorDataReceived += static (sender, e) => { };

RunAndCaptureText :

process.OutputDataReceived += (sender, e) => outputBuilder.AppendLine(e.Data);
process.ErrorDataReceived += (sender, e) => errorBuilder.AppendLine(e.Data);

RunAndCaptureTextCombined:

StringBuilder combinedBuilder = new();

process.OutputDataReceived += Append;
process.ErrorDataReceived += Append;

process.BeginOutputReadLine();
process.BeginErrorReadLine();

process.WaitForExit();

process.OutputDataReceived -= Append;
process.ErrorDataReceived -= Append;

void Append(sender, e)
{
   lock(combinedBuilder) // both events can be triggered at the same time
   {
       combinedBuilder.AppendLine(e.Data);
   }
}

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.

Added a new method to discard the output completely and not allocate, using the event.

Co-authored-by: Youssef Fahmy <youssefvictor00@gmail.com>
@Youssef1313
Youssef1313 requested a lite review from Copilot September 9, 2026 07:14

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.

🟡 Changes recommended

The new process runner has output-draining/capture correctness issues (async read completion + callback races) that can truncate output and undermine the intended fix.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (3)

src/Shared/CertificateGeneration/CertificateManagerProcessRunner.cs:39

  • errBuilder ??= new() inside the ErrorDataReceived callback has the same race/thread-safety issue as stdout; allocate the builder up-front and append to the single instance.
            {
                if (e.Data is not null)
                {
                    (errBuilder ??= new()).AppendLine(e.Data);
                }

src/Shared/CertificateGeneration/CertificateManagerProcessRunner.cs:47

  • When using BeginOutputReadLine/BeginErrorReadLine, WaitForExit() only waits for the process to exit, not for async output reads to fully drain. This can truncate captured output and break consumers like the OpenSSL output parsing.
        process.WaitForExit();

        return new ProcessExecutionResult(process.ExitCode, outBuilder?.ToString() ?? string.Empty, errBuilder?.ToString() ?? string.Empty);

src/Shared/CertificateGeneration/CertificateManagerProcessRunner.cs:69

  • Same as Run(...): after starting async reads, a single WaitForExit() may return before the redirected streams are fully drained. Disposing immediately after can still drop output and (in edge cases) reintroduce hangs during teardown.
        process.WaitForExit();
        return process.ExitCode;
    }
  • Files reviewed: 3/3 changed files
  • Comments generated: 3
  • Review effort level: Lite

Copilot AI commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

One or more custom setup steps configured for this repository failed during this Copilot code review run:

Build solution

Setup steps run before each review. If the review above is missing context, or no review was posted at all, the failing step above may be the cause. See the workflow run for failure details, fix your setup steps configuration, and re-request a review.

Note

You can configure setup steps for Copilot code review separately from Copilot cloud agent with a copilot-code-review.yml file. Read the docs for details.

Co-authored-by: Youssef Fahmy <youssefvictor00@gmail.com>

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.

🔵 Needs a closer look

The new async stdout/stderr draining approach should ensure output pumps fully complete (post-exit) to avoid truncated output and potential races during disposal.

Review details

Suppressed comments (2)

Previously missed (1) — in code that hasn't changed since the last review.

src/Shared/CertificateGeneration/CertificateManagerProcessRunner.cs:46

  • When using BeginOutputReadLine/BeginErrorReadLine, WaitForExit() can return before the async output pump has fully drained stdout/stderr. That can lead to truncated StandardOutput/StandardError (breaking callers like OpenSSL parsing) and potentially races with disposing the Process. Consider waiting for stream completion after the process exits (e.g., a second WaitForExit() or explicit end-of-stream signaling) before building the ProcessExecutionResult.

This issue also appears on line 66 of the same file.

src/Shared/CertificateGeneration/CertificateManagerProcessRunner.cs:67

  • RunAndDiscardOutput also uses BeginOutputReadLine/BeginErrorReadLine; similar to Run(), it should ensure the async readers have finished draining before returning/disposal to avoid rare truncation/races (even if output is discarded, the pump still needs to complete cleanly).
        }

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

One or more custom setup steps configured for this repository failed during this Copilot code review run:

Build solution

Setup steps run before each review. If the review above is missing context, or no review was posted at all, the failing step above may be the cause. See the workflow run for failure details, fix your setup steps configuration, and re-request a review.

Note

You can configure setup steps for Copilot code review separately from Copilot cloud agent with a copilot-code-review.yml file. Read the docs for details.

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

LGTM!


namespace Microsoft.AspNetCore.Certificates.Generation;

internal static class CertificateManagerProcessRunner

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.

subjective nit: this class could be just called ProcessRunner as its more or less general purpose utility

Suggested change
internal static class CertificateManagerProcessRunner
internal static class ProcessRunner

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.

It makes sense, but as we are unlikely to repurpose this helper for anything else in release/10.0, I'll keep the existing name to get this in faster (but I admit that I agree ProcessRunner is a better name)


process.WaitForExit();

return new ProcessExecutionResult(process.ExitCode, outBuilder?.ToString() ?? string.Empty, errBuilder?.ToString() ?? string.Empty);

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.

nit: we could delay the allocation of both strings to the moment where the properties are being requested for the first time, so in case exit code == 0, they are not allocated.

Since this is just a backport and the previous version was already allocating this memory, it's just something to consider, not a must have

@Youssef1313 Youssef1313 added Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Sep 9, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @copilot. This PR was just approved to be included in the upcoming servicing release. Somebody from the @dotnet/aspnet-build team will get it merged when the branches are open. Until then, please make sure all the CI checks pass and the PR is reviewed.

@wtgodbe
wtgodbe merged commit 271dbe7 into release/10.0 Sep 9, 2026
26 of 30 checks passed
@wtgodbe
wtgodbe deleted the copilot/fix-certificate-managers-deadlocks-dotnet-10 branch September 9, 2026 16:50
@rbhanda rbhanda added this to the 10.0.13 milestone Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix certificate managers deadlocks in .NET 10

6 participants