⚡ Sangsakala Proxy 🏠 Home
Skip to content

Fix crash when stopping pipeline with stop token - #28300

Open
Jordan Borean (jborean93) wants to merge 2 commits into
PowerShell:masterfrom
jborean93:pipeline-stop-race
Open

Jordan Borean (jborean93) wants to merge 2 commits into
PowerShell:masterfrom
jborean93:pipeline-stop-race

Conversation

@jborean93

Copy link
Copy Markdown
Collaborator

PR Summary

There is a race condition where PipelineProcess.Stop() can be called after Dispose() has already disposed the token. This causes the Stop() call to raise ObjectDisposedException crashing the process. An explicitly registered action on
$PSCmdlet.PipelineStopToken.Register() can also throw an exception which is not handled in the stop call causing a similar crash.

The fix is to ignore AggregateException which contains any exceptions raised by the user defined delegates on the stop token and exit early if the token has been disposed. We exit early because Dispose() has already done the work and there is nothing left to stop.

PR Context

Fixes: #28299

It also fixes a related problem where if the delegate registered by a caller could throw which is raised as an AggregateException. In C# this wouldn't typically happen but if someone in PowerShell provided any ScriptBlock delegate the cancellation will most likely be triggered somewhere that doesn't have a runspace. For example:

function Test-Stop {
    [CmdletBinding()] param()
    $null = $PSCmdlet.PipelineStopToken.Register([Action]{ })
    Start-Sleep 30
}

Test-Stop  # press Ctrl+C
image

The fix is to ignore AggregateException so that this exception, any anything else a registered delegate a C# cmdlet would register, do not bring down the process.

This should be considered for backporting as well. While it is a race condition, the issue from #28299 shows that it can crash the whole process.

PR Checklist

There is a race condition where `PipelineProcess.Stop()` can be called
after `Dispose()` has already disposed the token. This causes the
`Stop()` call to raise `ObjectDisposedException` crashing the process.
An explicitly registered action on
`$PSCmdlet.PipelineStopToken.Register()` can also throw an exception
which is not handled in the stop call causing a similar crash.

The fix is to ignore `AggregateException` which contains any exceptions
raised by the user defined delegates on the stop token and exit early if
the token has been disposed. We exit early because `Dispose()` has
already done the work and there is nothing left to stop.
@jborean93
Jordan Borean (jborean93) requested a review from a team as a code owner November 2, 2026 20:00
Copilot AI balanced review requested due to automatic review settings November 2, 2026 20:00
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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 tests do not reliably exercise cancellation of a disposed token source or the AggregateException handler, leaving the original crash without dependable regression coverage.

2 open issues
What changed in this PR

This PR hardens pipeline cancellation to prevent shell crashes when stopping races with disposal or a registered stop-token callback throws, addressing #28299.

Changes:

  • Return safely when the cancellation source has already been disposed.
  • Catch callback exceptions so command stopping can continue.
  • Add a callback-failure test and adjust test readiness signals.
File Description
test/​powershell/​Language/​Scripting/​PipelineStoppedToken.Tests.ps1 Adds callback-failure coverage and moves readiness signals into test scripts.
src/​System.Management.Automation/​engine/​pipeline.cs Handles disposed cancellation sources and exceptions from stop-token callbacks.

Review effort: Balanced

{
_pipelineStopTokenSource.Cancel();
}
catch (ObjectDisposedException)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not sure how this would be possible without adding test only hooks that lock the state to a particular set of steps. This would be very complicated and means we start testing an implementation detail. Ultimately this is a race condition that I don't believe we can really test against.

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pwsh 7.6 crashes with ObjectDisposedException in PipelineProcessor.Stop when Ctrl+C arrives as a native command ends

3 participants