René's URL Explorer Experiment


Title: Engine event processing bypasses "ShouldQueueAndProcessInExecutionThread" causing state corruption and crash due to Runspace affinity · Issue #4003 · PowerShell/PowerShell · GitHub

Open Graph Title: Engine event processing bypasses "ShouldQueueAndProcessInExecutionThread" causing state corruption and crash due to Runspace affinity · Issue #4003 · PowerShell/PowerShell

X Title: Engine event processing bypasses "ShouldQueueAndProcessInExecutionThread" causing state corruption and crash due to Runspace affinity · Issue #4003 · PowerShell/PowerShell

Description: This was found while investigating a PowerShell class static/instance method concurrency bug. Background When invoking a script block using InvokeWithPipe, if the script block is bound to a different Runspace (e.g. created in a different...

Open Graph Description: This was found while investigating a PowerShell class static/instance method concurrency bug. Background When invoking a script block using InvokeWithPipe, if the script block is bound to a differe...

X Description: This was found while investigating a PowerShell class static/instance method concurrency bug. Background When invoking a script block using InvokeWithPipe, if the script block is bound to a differe...

Opengraph URL: https://github.com/PowerShell/PowerShell/issues/4003

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Engine event processing bypasses \"ShouldQueueAndProcessInExecutionThread\" causing state corruption and crash due to Runspace affinity","articleBody":"This was found while investigating a PowerShell class static/instance method concurrency bug.\r\n\r\n## Background\r\n\r\nWhen invoking a script block using [`InvokeWithPipe`](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/lang/scriptblock.cs#L910), if the script block is bound to a different Runspace (e.g. created in a different Runspace), then the script block will be marshaled to that Runspace using the `EventManager` and is supposed to be executed by the main pipeline thread of that Runspace.\r\n\r\nThis is how it's done:\r\n1. The thread that is calling `InvokeWithPipe` finds that the script block is bound to another Runsapce and needs to run in that Runspace. (code [here](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/lang/scriptblock.cs#L961))\r\n2. The thread queues an event on the `EventManager` of the target Runspace, wishing the main pipeline thread of the target Runspace would pick up the event and run the script block. (code [here](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/lang/scriptblock.cs#L978))\r\n3. The thread waits for the main pipeline thread of the target Runspace to finish running the script block. (code [here](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/EventManager.cs#L984))\r\n\r\n## Issues\r\n\r\nThe problem is that, to avoid a possible hang, the thread, which waits for the main pipeline thread of the target Runspace, will pick up the event and execute it (not the required main pipeline thread) after waiting for 250 mSec (See the code [here](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/EventManager.cs#L998)). This will result in 2 threads running in the same Runspace and changing its state simultaneously.  This would cause:\r\n\r\n1. Deadlock.\r\n2. Runspace state corruption and process crash.\r\n\r\n## Repro Steps\r\n\r\n### Deadlock\r\n\r\nBoth arguments for '-sb' and '-arg' are script blocks that are created in the powershell console session. So when `bar` runs `$sb.InvokeReturnAsIs($arg)` in a new Runspace, it needs to marshal it back to the powershell console session. This is what happens:\r\n\r\n1. The new Runspace thread (**aka. requesting thread**) cannot execute the script block because it has to run in the powershell console session (**aka. target Runspace**) which is bound with the script block, so it queues an event for the pipeline thread of the target Runspace to run the script block;\r\n2. However, the target Runspace is completely unresponsive because it’s blocked on `$ps.Invoke()`;\r\n3. So, after 250ms, the requesting thread go ahead to process the event itself to run the script block. Note that – an event is now executing;\r\n4. Again, the requesting thread finds it cannot run `$arg` and goes back to step (1). However, this time when it comes to step (3), an event is already executing. So [`this.ProcessPendingActions()`](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/EventManager.cs#L1000) will immediately return, and the requesting thread will be stuck in the while loop.\r\n\r\n```powershell\r\n## The deadlock happens on PowerShell Core RC builds\r\n$ps = [powershell]::Create()\r\n$ps.AddScript('function bar { param([scriptblock]$sb, [scriptblock]$arg) $sb.InvokeReturnAsIs($arg) }').Invoke()\r\n$ps.Commands.Clear()\r\n$ps.AddCommand(\"bar\").AddParameter('sb', {param([scriptblock] $s) $s.InvokeReturnAsIs()}).AddParameter('arg', {[Console]::WriteLine(\"blah\")}) \u003e $null\r\n$ps.Invoke()\r\n```\r\n\r\n### Runspace state corruption and process crash\r\n\r\n\u003e **Note: this doesn't repro on latest PowerShell Core anymore because we have fixed the PowerShell class static method to not route method execution to other Runspaces incorrectly. But the underlying problem in `EventManager` is still there. You can run this repro in Windows PowerShell v5.1 to see the results.**\r\n\r\nThis repro creates a script `DoInvokeInParallel.ps1` that dot-sources an `Invoker.ps1` file which defines a class with a static method.  Run `DoInvokeInParallel.ps1` in multiple `Runspaces` via a `RunspacePool` to use the class static method concurrently.\r\n\r\n`Invoker.ps1` file\r\n```powershell\r\nclass Invoker\r\n{\r\n    static [object[]] Invoke(\r\n        [scriptblock] $scriptToInvoke,\r\n        [object[]] $args)\r\n    {\r\n        return $scriptToInvoke.Invoke($args)\r\n    }\r\n}\r\n```\r\n\r\n`DoInvokeInParallel.ps1` file\r\n``` powershell\r\n$invokerPath = Join-Path $PSScriptRoot Invoker.ps1\r\n. $invokerPath\r\n\r\n$rsp = [runspacefactory]::CreateRunspacePool(1, 10, $host)\r\n$rsp.Open()\r\n\r\n$scriptTemplate = @'\r\n    . {0}\r\n    1..100 | foreach {{\r\n        $results = [Invoker]::Invoke({{ \"RS_{1} Loop $_ `r`n\" }}, $null)\r\n        Write-Output $results\r\n    }}\r\n'@\r\n\r\nclass Task\r\n{\r\n    [powershell] $powershell\r\n    [System.IAsyncResult] $Async\r\n}\r\n\r\n$tasks = @()\r\n\r\n1..10 | foreach {\r\n    $task = [Task]::new()\r\n    $script = $scriptTemplate -f $invokerPath,$_\r\n    $task.powershell = [powershell]::Create()\r\n    $null = $task.powershell.AddScript($script)\r\n    $task.powershell.RunspacePool = $rsp\r\n    $task.Async = $task.powershell.BeginInvoke()\r\n    $tasks += $task\r\n}\r\n\r\nforeach ($task in $tasks)\r\n{\r\n    $results = $task.powershell.EndInvoke($task.Async)\r\n    Write-Host $results\r\n    Write-Host $task.powershell.Streams.Error\r\n    $task.powershell.Dispose()\r\n}\r\n\r\n$rsp.Dispose()\r\n```\r\n**Run `DoInvokeInParallel.ps1`. The result is multiple \"invalid session state\" asserts if you are using a debug flavor Windows PowerShell. Eventually, the process will crash.**\r\n","author":{"url":"https://github.com/PaulHigin","@type":"Person","name":"PaulHigin"},"datePublished":"2017-06-13T21:14:27.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/4003/PowerShell/issues/4003"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:0679e77b-8e6b-ff39-707d-9e761a60b061
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9EC0:15298C:19864C2:2384D91:6A5F36D0
html-safe-nonce64e809a6f4cb852e0c54d3fa83970e42338c48f6791b2856258d500568eb2581
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RUMwOjE1Mjk4QzoxOTg2NEMyOjIzODREOTE6NkE1RjM2RDAiLCJ2aXNpdG9yX2lkIjoiMzYyOTgzMDE4NTM3Njc1NzQ1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac93d9853809eebf2d0ab7b445ef3b849a8245aa309d3c9c6589ff534a36870750
hovercard-subject-tagissue:235695018
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/4003/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c4e3cfd315a866613b7ac2a2da00278508243517e77047c29e227f8f253399be/PowerShell/PowerShell/issues/4003
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c4e3cfd315a866613b7ac2a2da00278508243517e77047c29e227f8f253399be/PowerShell/PowerShell/issues/4003
og:image:altThis was found while investigating a PowerShell class static/instance method concurrency bug. Background When invoking a script block using InvokeWithPipe, if the script block is bound to a differe...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamePaulHigin
hostnamegithub.com
expected-hostnamegithub.com
Nonec80a2cb0221395c7c3987d7891fcde45641e27ea0bd3917b247d8fe154cf710f
turbo-cache-controlno-preview
go-importgithub.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git
octolytics-dimension-user_id11524380
octolytics-dimension-user_loginPowerShell
octolytics-dimension-repository_id49609581
octolytics-dimension-repository_nwoPowerShell/PowerShell
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id49609581
octolytics-dimension-repository_network_root_nwoPowerShell/PowerShell
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release4cf24abec96dc3a7b39f364f737aca1e50991904
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/PowerShell/PowerShell/issues/4003#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F4003
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F4003
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=PowerShell%2FPowerShell
Reloadhttps://github.com/PowerShell/PowerShell/issues/4003
Reloadhttps://github.com/PowerShell/PowerShell/issues/4003
Reloadhttps://github.com/PowerShell/PowerShell/issues/4003
Please reload this pagehttps://github.com/PowerShell/PowerShell/issues/4003
PowerShell https://github.com/PowerShell
PowerShellhttps://github.com/PowerShell/PowerShell
Notifications https://github.com/login?return_to=%2FPowerShell%2FPowerShell
Fork 8.4k https://github.com/login?return_to=%2FPowerShell%2FPowerShell
Star 54.5k https://github.com/login?return_to=%2FPowerShell%2FPowerShell
Code https://github.com/PowerShell/PowerShell
Issues 1.3k https://github.com/PowerShell/PowerShell/issues
Pull requests 286 https://github.com/PowerShell/PowerShell/pulls
Discussions https://github.com/PowerShell/PowerShell/discussions
Actions https://github.com/PowerShell/PowerShell/actions
Projects https://github.com/PowerShell/PowerShell/projects
Security and quality 3 https://github.com/PowerShell/PowerShell/security
Insights https://github.com/PowerShell/PowerShell/pulse
Code https://github.com/PowerShell/PowerShell
Issues https://github.com/PowerShell/PowerShell/issues
Pull requests https://github.com/PowerShell/PowerShell/pulls
Discussions https://github.com/PowerShell/PowerShell/discussions
Actions https://github.com/PowerShell/PowerShell/actions
Projects https://github.com/PowerShell/PowerShell/projects
Security and quality https://github.com/PowerShell/PowerShell/security
Insights https://github.com/PowerShell/PowerShell/pulse
Engine event processing bypasses "ShouldQueueAndProcessInExecutionThread" causing state corruption and crash due to Runspace affinityhttps://github.com/PowerShell/PowerShell/issues/4003#top
Issue-BugIssue has been identified as a bug in the producthttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Bug%22
Resolution-No ActivityIssue has had no activity for 6 months or morehttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Resolution-No%20Activity%22
Size-Weekhttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Size-Week%22
WG-Enginecore PowerShell engine, interpreter, and runtimehttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Engine%22
https://github.com/PaulHigin
PaulHiginhttps://github.com/PaulHigin
on Jun 13, 2017https://github.com/PowerShell/PowerShell/issues/4003#issue-235695018
InvokeWithPipehttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/lang/scriptblock.cs#L910
herehttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/lang/scriptblock.cs#L961
herehttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/lang/scriptblock.cs#L978
herehttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/EventManager.cs#L984
herehttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/EventManager.cs#L998
this.ProcessPendingActions()https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/EventManager.cs#L1000
Issue-BugIssue has been identified as a bug in the producthttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Bug%22
Resolution-No ActivityIssue has had no activity for 6 months or morehttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Resolution-No%20Activity%22
Size-Weekhttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Size-Week%22
WG-Enginecore PowerShell engine, interpreter, and runtimehttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Engine%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.