Repository navigation
Fix certificate manager process deadlocks in .NET 10 - #69051
Conversation
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
c175323 to
147f034
Compare
There was a problem hiding this comment.
🟡 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
CertificateManagerProcessRunnerto safely execute redirected processes and capture stdout/stderr. - Updated
UnixCertificateManagercall sites (NSS/OpenSSL/WSL trust paths) to use the runner instead ofProcess.Start(...).WaitForExit()patterns. - Updated
MacOSCertificateManagercall 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.
adamsitnik
left a comment
There was a problem hiding this comment.
@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(); |
There was a problem hiding this comment.
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);
}
}There was a problem hiding this comment.
Added a new method to discard the output completely and not allocate, using the event.
Co-authored-by: Youssef Fahmy <youssefvictor00@gmail.com>
There was a problem hiding this comment.
🟡 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 singleWaitForExit()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
|
One or more custom setup steps configured for this repository failed during this Copilot code review run: 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 |
There was a problem hiding this comment.
🔵 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
|
One or more custom setup steps configured for this repository failed during this Copilot code review run: 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 |
|
|
||
| namespace Microsoft.AspNetCore.Certificates.Generation; | ||
|
|
||
| internal static class CertificateManagerProcessRunner |
There was a problem hiding this comment.
subjective nit: this class could be just called ProcessRunner as its more or less general purpose utility
| internal static class CertificateManagerProcessRunner | |
| internal static class ProcessRunner |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
|
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. |
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