Repository navigation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I have found this or similar behaviour when using using Start-ThreadJob or another Runspace and PS::Create. Class instances are not serialised and report their proper class name via GetType. The timing of calls to nested methods determines whether the call succeeds or causes a deadlock of some kind. The lockup causes the task/job to reaming in a "running" state forever, not faulted or able to exit. Is this caused by something like a race? I'm not expert enough in multi-threading to know what's going on.
This code reproduces the issue in PS 7.2.6. The calls that would be fatal are commented. The purpose was initially to test whether the GC would prematurely collect object instances passed to other threads after the instance went out of scope on the calling thread, but then I encountered (again) the issue I describe here.
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
class test_class : System.IDisposable
{
test_class([int] $Id)
{
$this._id = $Id
}
[int] $_id
[void] Dingaling()
{
"{0}:{1} is disposed? {2}" -f $this.GetType(), $this._id, $this._disposed | Out-Host
}
[int] GetId()
{
return $this._id
}
[void] NullFunc()
{
# does nothing at all!
}
[void] CallThisNullFunc()
{
"calling this.NullFunc" | Out-Host
"our type is $($this.GetType())" | Out-Host
$this | Out-String | Out-Host
#$this.NullFunc | Out-String | Out-Host # fatal
$this.NullFunc() # fatal
"returned from this.NullFunc" | Out-Host
}
[void] CallThisDispose()
{
"yo ho ho, callin' this.Dispo'" | Out-Host
$this.Dispose()
}
$_disposed = $false
[void] Dispose()
{
$this.Dispose($true)
#[System.GC]::SuppressFinalize($this)
}
[void] Dispose([bool] $Disposing)
{
"yarrr we be disposin' {0}!" -f $this._id | Out-Host
$this._disposed = $true
}
[void] Finalize()
{
"yarrr we be finalizin' {0}!" -f $this._id | Out-Host
$this.Dispose($false)
}
} # end class
function test_func2
{
$test2 = [test_class]::new(2)
$test2.Dingaling()
#$test2 = $null # should be collected by gc
}
test_func2
"call gc after test_func2"
[System.GC]::Collect()
$otherPs = [powershell]::Create()
$otherPs.RunspacePool = [runspacefactory]::CreateRunspacePool(1,2)
$otherPs.RunspacePool.Open()
function test_func
{
# use a function so test goes out of scope
$test = [test_class]::new(1)
$otherPs.AddScript({param($test)
#$test.Dispose() # no problems if called here
$test.CallThisNullFunc() # fine here
$test.Dingaling()
Start-Sleep -Seconds 10
$test.Dingaling()
$test.Dingaling()
#$test.Dispose() # fatal if called here
$test.Dingaling()
"our id is $($test.GetId())"
$test.NullFunc() # this is fine
$test.CallThisNullFunc() # fatal if called here, script will never end
#$test.CallThisDispose() # also fatal here
#$test.Dispose() # or here
})
$otherPs.AddArgument($test)
}
"call test_func"
test_func
"begin invoke"
$timer = [System.Diagnostics.Stopwatch]::new()
$timer.Start()
$runState = $otherPs.BeginInvoke()
#Start-Sleep -Seconds 15 # doesn't help
while (-not $runState.IsCompleted) {
"not done yet: {0}" -f ($runState | Out-String)
Start-Sleep -MilliSeconds 3000
[System.GC]::Collect()
if (20 -lt $timer.Elapsed.TotalSeconds) {
"terminate test after {0}s" -f $timer.Elapsed.TotalSeconds
break
}
}
$otherPs.InvocationStateInfo # "running"... forever
$otherPs.EndInvoke($runState) # fails to return
"runspace faulted? {0}" -f $otherPs.HadErrors
$otherPs.Streams.Error | Out-Host
$otherPs.RunspacePool.Close()
$otherPs.Dispose()
$otherPs = $null
1..10 | ForEach-Object {
Start-Sleep -MilliSeconds 1000
"do gc collection then quit"
[System.GC]::Collect()
}
Expected behavior
PS class methods that call other methods via $this.OtherMethod() should succeed.Actual behavior
Only PS class methods that do not call $this.OtherMethod succeed. Methods that call $this.OtherMethod succeed until the point at which they call OtherMethod. Then we encounter a deadlock of some kind.
**However** this depends on the timing of the call. As shown in the comments, calls made immediately after the worker thread begins appear to work; calls made after `Start-Sleep` will fail.Error details
No response
Environment data
$psversiontable
Name Value
---- -----
PSVersion 7.2.6
PSEdition Core
GitCommitId 7.2.6
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
No response
Activity
SeeminglyScience commentedon Sep 1, 2022
Duplicate of #12801
SeeminglyScience commentedon Sep 1, 2022
Not exactly duplicate in reported behavior but same cause. PowerShell classes are bound to the runspace they're created in and cannot safely be used between different runspaces.