GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix crash when stopping pipeline with stop token by jborean93 · Pull Request #28300 · PowerShell/PowerShell

Conversation

@jborean93

@jborean93 Jordan Borean (jborean93) commented Nov 2, 2026

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

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge. If this PR is a work in progress, please open this as a Draft Pull Request and mark it as Ready to Review when it is ready to merge.
  • Breaking changes
    • None
    • OR
    • Experimental feature(s) needed
      • Experimental feature name(s):
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed
      • Issue filed:
  • Testing - New and feature
    • N/A or can only be tested interactively
    • OR
    • Make sure you've added a new test if existing tests do not effectively test the code changed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jborean93
Fix crash when stopping pipeline with stop token
e857ec9
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

azure-pipelines Bot commented Nov 2, 2026

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Copilot started reviewing on behalf of Jordan Borean (jborean93) November 2, 2026 20:01
View session
@jborean93 Jordan Borean (jborean93) mentioned this pull request Nov 2, 2026
pwsh 7.6 crashes with ObjectDisposedException in PipelineProcessor.Stop when Ctrl+C arrives as a native command ends #28299
Open
Copilot AI reviewed Nov 2, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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
  • Medium severity Test cancellation of a disposed token source · New
  • Medium severity Wait on the function token to exercise cancellation handling · New
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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/System.Management.Automation/engine/pipeline.cs
{
_pipelineStopTokenSource.Cancel();
}
catch (ObjectDisposedException)

@jborean93 Jordan Borean (jborean93) Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/powershell/Language/Scripting/PipelineStoppedToken.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jborean93
Use a cancellable task for test
5ec0eb1
@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
iSazonov
Ilya (iSazonov) approved these changes Nov 3, 2026
View reviewed changes
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Nov 10, 2026
@microsoft-github-policy-service

microsoft-github-policy-service Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@iSazonov Ilya (iSazonov) iSazonov approved these changes

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Milestone

No milestone

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

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@jborean93 @iSazonov
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.