-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exception calling "BeginInvoke" with "0" argument(s): "Cannot perform the operation because the runspace pool is not in the 'Opened' state. The current state is 'Closed'." #111
Comments
I'm not quite sure what would be causing that other than the runspacepool cleanup might be disposing of the pool. PS C:\> $PoshRS_RunspacePoolCleanup
Name Value
---- -----
Runspace System.Management.Automation.Runspaces.LocalRunspace
Flag True
PowerShell System.Management.Automation.PowerShell
Host System.Management.Automation.Internal.Host.InternalHost
Timeout 3000000000
Handle System.Management.Automation.PowerShellAsyncResult
PS C:\> $PoshRS_RunspacePools
RunspacePool : System.Management.Automation.Runspaces.RunspacePool
State : Opened
AvailableJobs : 5
MaxJobs : 5
LastActivity : 10/10/2016 6:28:23 PM
RunspacePoolID : e23487d9-9a2d-4b49-924b-1eef918d2c5e
CanDispose : False Do you have any code that I could look at to reproduce this issue? |
I can't give you any code to reproduce it but I get it a lot when running interactively. I'm not sure what debugging information I can gather for you when it does happen? I gathered this during a long run of it... but it cleared up almost instantly afterwards... so I'm not sure. I will try to capture it again next time.
I think it has something to do when I've modified whatever module I'm working on and have done an Import-Module -Force on it. I'm not sure if that then force reloads your module (which is in the RequiredModules collection). I am using -Batch parameters though, so, I thought each batch has its own runspace, and so that this shouldn't happen. |
Still dealing with this a lot every day. Would really love to help finding a cause/solution.
|
if you can find anything that might be causing this, please let me know. I haven't been able to reproduce this yet in testing so troubleshooting this has been kind of hard. |
I can reproduce it some of the time with this script on a 4 core machine with nothing else going on.
The first time it failed at loop 12 (all later loops completed fine). On another run it failed at loop 22 (again with later loops completing fine). On three later runs they all completed with no errors. On a new PowerShell session it started failing at ~22 again. Have you noticed that by default a single runspace is open, and after a single Start-RsJob you now have 4 runspaces open; plus one for each additional batch (the last bit is expected, but they never seem to get re-used, or closed). It could be related. Because I use many |
One you import the module, there should be 2 additional runspaces opened up (1 to support the background job cleanup when it has been completed and another 1 to perform the runspacepool cleanup actions). I have noticed additional runspaces being created randomly and am starting to dive deeper into what those actually are and if they are related to the module. |
I think I solved the issue with multiple runspaces being created after using Start-RSJob. For some reason, if Remove-Variable throws an error about the variable doesn't exist then it will magically spawn a new runspace (that eventually goes away). Setting this to SilentlyContinue seems to solve this issue. |
Were you able to reproduce the overall issue? |
Yes, it was sporadic but I did see the issue pop up a few times. I didn't dive much into that as i wanted to knock out the issue with the excessive runspaces being created, but will now take a look at the overall issue and see what I can do for it. |
From the error stack: $error[-4]|Select-Object -Property *
PSMessageDetails :
Exception : System.Management.Automation.MethodInvocationException: Exception calling "BeginInvoke" with
"0" argument(s): "Cannot perform the operation because the runspace pool is not in the
'Opened' state. The current state is 'Closing'." --->
System.Management.Automation.Runspaces.InvalidRunspacePoolStateException: Cannot perform the
operation because the runspace pool is not in the 'Opened' state. The current state is
'Closing'.
at System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.AssertPoolIsOpen()
at
System.Management.Automation.PowerShell.CoreInvokeAsync[TInput,TOutput](PSDataCollection`1
input, PSDataCollection`1 output, PSInvocationSettings settings, AsyncCallback callback,
Object state, PSDataCollection`1 asyncResultOutput)
at System.Management.Automation.PowerShell.BeginInvoke[T](PSDataCollection`1 input,
PSInvocationSettings settings, AsyncCallback callback, Object state)
at CallSite.Target(Closure , CallSite , Object )
--- End of inner exception stack trace ---
at
System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception
exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
at CallSite.Target(Closure , CallSite , Object )
at Start-RSJob<End>(Closure , FunctionContext )
TargetObject :
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : InvalidRunspacePoolStateException
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at Start-RSJob<End>,
C:\Users\proxb\Documents\WindowsPowerShell\Modules\PoshRSJob\Public\Start-RSJob.ps1: line 504
at <ScriptBlock>, <No file>: line 4
at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {} $error[-4].Exception|Select-Object -Property *
ErrorRecord : Exception calling "BeginInvoke" with "0" argument(s): "Cannot perform the operation
because the runspace pool is not in the 'Opened' state. The current state is 'Closing'."
WasThrownFromThrowStatement : False
Message : Exception calling "BeginInvoke" with "0" argument(s): "Cannot perform the operation
because the runspace pool is not in the 'Opened' state. The current state is 'Closing'."
Data : {}
InnerException : System.Management.Automation.Runspaces.InvalidRunspacePoolStateException: Cannot perform
the operation because the runspace pool is not in the 'Opened' state. The current state
is 'Closing'.
at
System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.AssertPoolIsOpen()
at System.Management.Automation.PowerShell.CoreInvokeAsync[TInput,TOutput](PSDataColle
ction`1 input, PSDataCollection`1 output, PSInvocationSettings settings, AsyncCallback
callback, Object state, PSDataCollection`1 asyncResultOutput)
at System.Management.Automation.PowerShell.BeginInvoke[T](PSDataCollection`1 input,
PSInvocationSettings settings, AsyncCallback callback, Object state)
at CallSite.Target(Closure , CallSite , Object )
TargetSite : Void ConvertToMethodInvocationException(System.Exception, System.Type, System.String,
Int32, System.Reflection.MemberInfo)
StackTrace : at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationExceptio
n(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo
memberInfo)
at CallSite.Target(Closure , CallSite , Object )
at Start-RSJob<End>(Closure , FunctionContext )
HelpLink :
Source : System.Management.Automation
HResult : -2146233087 $error[-4].Exception.innerexception|Select-Object -Property *
CurrentState : Closing
ExpectedState : Opened
Message : Cannot perform the operation because the runspace pool is not in the 'Opened' state. The current
state is 'Closing'.
Data : {}
InnerException :
TargetSite : Void AssertPoolIsOpen()
StackTrace : at System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.AssertPoolIsOpen()
at System.Management.Automation.PowerShell.CoreInvokeAsync[TInput,TOutput](PSDataCollection`1
input, PSDataCollection`1 output, PSInvocationSettings settings, AsyncCallback callback, Object
state, PSDataCollection`1 asyncResultOutput)
at System.Management.Automation.PowerShell.BeginInvoke[T](PSDataCollection`1 input,
PSInvocationSettings settings, AsyncCallback callback, Object state)
at CallSite.Target(Closure , CallSite , Object )
HelpLink :
Source : System.Management.Automation
HResult : -2146233087 |
I've been banging away on the new version all day with no failures so I think you've solved it, thanks. |
The issue still exists :-( I am getting it all the time. I haven't been able to make the simple test script I provided before fail anymore though, so it seems the problem can be triggered by something a bit more complicated than that. |
Here's a test I've done (though you won't be able to reproduce it) dumping out details at each step. I'm calling a function, which generates a batch number (with a throttle of 4). In this case it's then running a single job on that batch. The function completes, and I repeat exactly as before with a new batch number and a single job. On the 5th run it fails. In a fresh session:
And the result.
And the detail from the error
|
I found one line that stands out to me where an initial runspacepool is being created. Unlike where I use an existing runspacepool, the LastActivity property is not updated to show a date of 5 minutes ahead and instead shows the default datestamp of 1/1/0001 12:00:00 AM which, to me, would pose an issue if the runspacepool cleanup just happens to hit the runspacepool on creation but before it has a chance to update the property until after a job is started. Being that it is hard to reproduce this, I will probably make the update and push out a commit and see if that has any effect. |
Sounds good to me, I will be able to verify it pretty quickly if it works or not :) |
Ok, the latest files are out there. I only updated Start-RSJob and haven't added this as a release yet, so you will have to download the Zip file from the main page. |
Looks really good to me so far. No speed regressions, and the error isn't occurring. |
Is the version on PowerShell gallery going to be updated soon with this fix? |
Yes. I was hoping to include an additional update as well, but implementing it has taken longer than expected. I will most likely update the gallery version by the end of the week, if not earlier. |
Happy New Year! Any signs of the new version in PowerShell Gallery? |
@loku02 I'm hoping to have it by this weekend. I've been sick and also trying to fit another big bug/feature issue into that release as well which has pushed that date back from when I initially wanted to do it. |
I hope you feel better now! Looking forward for the new version. |
@loku02 Update is now live |
@proxb Thank you! |
I'm coming across this issue frequently during development. I have a module I'm working on and often need to Import-Module Blah-Force to bring in the latest changes and test it (like in ISE). This module has a RequiredModules of PoshRSJob and functions which use Start-RSJob and others.
If I run steps manually one by one like so, it works:
I realised this is because there is a small delay when you run them like that. If I run them all at once with a delay it works:
But if they execute immediately after each other with no delay I get the above error. Every time!
Any ideas why this is happening?
The text was updated successfully, but these errors were encountered: