Repository navigation
Fix CA2213 disposal violations in cmdlets and ConsoleHost - #28604
bgoodman-ui wants to merge 1 commit into
Conversation
Dispose file/string writers, XML readers, web-response streams, cancellation tokens, retry timers, and the console writer deterministically instead of leaving them to finalization or process exit. Fixes or explicitly suppresses the CA2213 violations in the command and host assemblies tracked by PowerShell#15803. Cmdlet output and error behavior are unchanged.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
bgoodman-ui please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved cleanup defects can leak writers, pipelines, and cancellation sources, while a disposal failure can leave an originally read-only file writable.
4 open issues
What changed in this PR
Addresses CA2213 disposal violations in PowerShell cmdlets and ConsoleHost tracked by #15803.
Changes:
- Adds deterministic cleanup for writers, readers, response streams, cancellation sources, and retry timers.
- Adds targeted suppressions documenting resource ownership and existing cleanup paths.
| File | Description |
|---|---|
| src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs | Disposes console writer; documents pipeline ownership. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs | Disposes XML reader before its file stream. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs | Disposes wrapped response stream. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | Adds cancellation-source disposal. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | Documents borrowed stream ownership in suppression. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-string/Out-String.cs | Adds fallback writer cleanup. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs | Disposes writer before file stream. |
| src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs | Documents exit-handler cleanup in suppression. |
| src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs | Disposes retry timer during job cleanup. |
Review effort: Balanced
| } | ||
|
|
||
| #if UNIX | ||
| #pragma warning disable CA2213 // Disposable fields should be disposed - disposed by the exit handler in StreamClosing() |
| { | ||
| // StreamWriter.Dispose() flushes and disposes the underlying stream, | ||
| // so it must be disposed before the stream itself. | ||
| _sw.Dispose(); |
| @@ -1423,6 +1430,7 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM | |||
| _cancelToken = new CancellationTokenSource(); | |||
| private bool _displayDebuggerBanner; | ||
| private DebuggerStopEventArgs _debuggerStopEventArgs; | ||
| private bool _inPushedConfiguredSession; | ||
| #pragma warning disable CA2213 // Disposable fields should be disposed - owned by the remote runspace, not the console host |

Dispose file/string writers, XML readers, web-response streams, cancellation tokens, retry timers, and the console writer deterministically instead of leaving them to finalization or process exit.
Fixes or explicitly suppresses the CA2213 violations in the command and host assemblies tracked by #15803. Cmdlet output and error behavior are unchanged.
PR Summary
PR Context
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header