Repository navigation
PowerShell hangs when exiting having used a PowerShell runspace pool/runspace #5205
Description
Activity
sergei (@vors) and I were both able to repro this locally.
- addedWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtimeIssue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the product
on Oct 24, 2017 I think I have a simpler repro without using httplistener
PS /Users/steve> $rs = [runspacefactory]::CreateRunspacePool(1,5) PS /Users/steve> $rs.Open() PS /Users/steve> $ps = [powershell]::Create() PS /Users/steve> $ps.RunspacePool = $rs PS /Users/steve> $null = $ps.AddScript({1+1}) PS /Users/steve> $ps.Invoke() PS /Users/steve> exit #hangs
Reacted by TylerLeonhardt and sergeiPaulHigin commented
on Oct 25, 2017 ContributorMore actionsThis is hanging on the PipelineThread._workItemReady event wait. On exit this event should get set and release the thread wait via the finalizer:
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/hostifaces/LocalPipeline.cs#L1340But it looks like the finalizer is not being called.
Dongbo Wang (@daxian-dbw) This appears to be a bug in the .Net Core CLR since it works in Windows PowerShell.Reacted by Oisin GrehanIs this something we should open on .NET Core CLR?
@tylerl0706 need more data to provide to dotnetcore team before we hand it off to them
Reacted by TylerLeonhardtpowercode commented
on Nov 3, 2017 CollaboratorMore actionsI don't think we can depend on a finalizer being run. We should invoke dispose explicitly if we depend on it to run. There are quite a few cases where finalizers are not run - see https://blogs.msdn.microsoft.com/oldnewthing/20100809-00/?p=13203/ and
https://stackoverflow.com/questions/3458177/are-net-finalizers-always-executed.PaulHigin commented
on Nov 3, 2017 ContributorMore actionsSure, finalizers sometimes don't run due to various reasons, but in this case the finalizer never runs. Also this cleanup has relied on finalizers for years without any problems. So this appears to be a CLR regression.
We can (and do) do some cleanup during exit, such as closing remote runspaces and disconnect running remote jobs. But this cleanup is not guaranteed to run in all cases same as the finalizer thread.
PaulHigin commented
on Nov 3, 2017 ContributorMore actionsUnless you are suggesting that there is a bug in PowerShell that is causing the finalizer thread not to run (such as throwing an unhandled exception on the thread). That would be a regression on our part. But with my experimentation I don't see any unhandled exceptions. It looks like the finalizer is just not getting called.
powercode commented
on Nov 3, 2017 CollaboratorMore actionsI'm saying that it is brittle. Any type a user loads with a misbehaving finalizer may cause this to happen.
If we have control of these objects, (and thePipelineThreadseems like it should fall in this category) we should also be able to write infrastructure to dispose of them.PaulHigin commented
on Nov 3, 2017 ContributorMore actionsI understand. And we have that infrastructure on exit. My point is that anything we write is just as susceptible to catastrophic failures.
Paul Higinbotham (@PaulHigin) I don't know... Yes, finalizer never getting called might be a CLR regression, but it feels like we should be able to set _workItemReady (e.g. by calling LocalPipeline:Dispose()) in our code without relying on the mercy of the garbage collector.
PaulHigin commented
on Nov 3, 2017 ContributorMore actionsAndrew (@anmenaga), We really need to be able to depend on the CLR to work as advertised. We have other places in the code that relies on the finalizer to work. I'll investigate more and if this looks like a CLR regression I'll notify the team.
But for the short term I'll create a workaround to fix this particular hang problem for RC using our existing cleanup on exit code.
Reacted by sergei and Staffan GustafssonClose via #5356
PowerShell Core hangs when exiting and using a PowerShell runspace pool. On Windows PowerShell it exits successfully.
Steps to reproduce
here's the gist for my code: https://gist.github.com/tylerl0706/2d87a13c6147278bb4e8565585aa9722
Steps:
Expected behavior
PowerShell successfully exists
Actual behavior
PowerShell hangs and doesn't give the prompt back - this is causing my AppVeyor and Travis builds to fail due to timeout :(
Environment data