You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PowerShell classes leak to other runspaces on macOS #9313
class MyClass {
# Property: Holds name
[String] $Name# Constructor: Creates a new MyClass object, with the specified name
MyClass([String] $NewName) {
# Set name for MyClass$this.Name=$NewName
}
# Method: Method that changes $Name to the default name
[void] ChangeNameToDefault() {
$this.Name="DefaultName"
}
}
[MyClass].GetType()
[PowerShell]::Create().AddScript('[MyClass]::new("ffff")').Invoke()
Expected behavior
Class types should not leak to other runspaces.
Actual behavior
Type was able to be created in other runspace.
Environment data
Name Value
---- -----
PSVersion 6.2.0
PSEdition Core
GitCommitId 6.2.0
OS Darwin 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Notes
This doesn't repro without using the class in some way (i.e. calling [MyClass].GetType()). That part is important.
cc Dongbo Wang (@daxian-dbw) who wasn't able to repro this (I assume on Windows? Using PowerShell Core 6.2?)
TylerLeonhardt been a while since I played with it, but don't new [powershell] instances execute in the current runspace unless you manually assign them a new one?
I'm pretty sure they're in a new runspace because Get-Runspace returns another runspace. Just to be sure, I tried manually creating a new runspace and the issue still repros:
class MyClass {
# Property: Holds name
[String] $Name# Constructor: Creates a new MyClass object, with the specified name
MyClass([String] $NewName) {
# Set name for MyClass$this.Name=$NewName
}
# Method: Method that changes $Name to the default name
[void] ChangeNameToDefault() {
$this.Name="DefaultName"
}
}
$rs= [runspacefactory]::CreateRunspace()
$rs.Open()
[MyClass].GetType()
[PowerShell]::Create($rs).AddScript('[MyClass]::new("ffff")').Invoke()
Reacted by Rain Sallow (/u/ta11ow) and Patrick Meinecke
Windows PowerShell 5.1 behaves the same way. A loaded type definition is available process wide unless maybe the process is using AppDomains for isolation but 5.1 doesn't appear to use AppDomains. And there is no support for AppDomains in .NET Core anyway.
Keith Hill (@rkeithhill) I'd expect the type to still be loaded, but I wouldn't expect PowerShell to resolve it since resolution is scope based:
& {
classMyClass {}
}
# Returns the above class
[AppDomain]::CurrentDomain.GetAssemblies().
Where{
[Reflection.CustomAttributeExtensions]::IsDefined(
$_,
[Management.Automation.DynamicClassImplementationAssemblyAttribute])
}.ForEach{
# The types sometimes have a distinguishing namespace$_.Modules[0].FindTypes([Reflection.Module]::FilterTypeName,'MyClass')
}
# Throws type not found
[MyClass]
Edit: Side note, I can't repro on 5.1 nor 6.2 on Windows. I get an Unable to find type [MyClass]. error in Streams.Errors
PSReadLine plays a role in the repo. To get consistent repro on all platforms (including powershell.exe), do the following:
start pwsh/powershell.exe with -noprofile
copy the following to pwsh/powershell.exe:
class MyClass {
# Property: Holds name
[String] $Name
# Constructor: Creates a new MyClass object, with the specified name
MyClass([String] $NewName) {
# Set name for MyClass
$this.Name = $NewName
}
# Method: Method that changes $Name to the default name
[void] ChangeNameToDefault() {
$this.Name = "DefaultName"
}
}
The moment you remove PSReadLine, this won't repro. This is because PSReadLine always parse the input before it's sent to engine to execute. The parsing involves resolving [MyClass], and put it in the type cache with the wrong TypeResolutionState.
Activity
vexx32 commentedon Apr 7, 2019
TylerLeonhardt been a while since I played with it, but don't new
[powershell]instances execute in the current runspace unless you manually assign them a new one?TylerLeonhardt commentedon Apr 7, 2019
I'm pretty sure they're in a new runspace because
Get-Runspacereturns another runspace. Just to be sure, I tried manually creating a new runspace and the issue still repros:class MyClass { # Property: Holds name [String] $Name # Constructor: Creates a new MyClass object, with the specified name MyClass([String] $NewName) { # Set name for MyClass $this.Name = $NewName } # Method: Method that changes $Name to the default name [void] ChangeNameToDefault() { $this.Name = "DefaultName" } } $rs = [runspacefactory]::CreateRunspace() $rs.Open() [MyClass].GetType() [PowerShell]::Create($rs).AddScript('[MyClass]::new("ffff")').Invoke()rkeithhill commentedon Apr 7, 2019
Windows PowerShell 5.1 behaves the same way. A loaded type definition is available process wide unless maybe the process is using AppDomains for isolation but 5.1 doesn't appear to use AppDomains. And there is no support for AppDomains in .NET Core anyway.
SeeminglyScience commentedon Apr 8, 2019
Keith Hill (@rkeithhill) I'd expect the type to still be loaded, but I wouldn't expect PowerShell to resolve it since resolution is scope based:
Edit: Side note, I can't repro on 5.1 nor 6.2 on Windows. I get an
Unable to find type [MyClass].error inStreams.ErrorsAlso Rain Sallow (/u/ta11ow) (@vexx32)
Only when you pass
[System.Management.Automation.RunspaceMode]::CurrentRunspacedaxian-dbw commentedon Dec 5, 2019
PSReadLine plays a role in the repo. To get consistent repro on all platforms (including powershell.exe), do the following:
-noprofileThen you will see the
Name: ffffprinted out.The moment you remove PSReadLine, this won't repro. This is because PSReadLine always parse the input before it's sent to engine to execute. The parsing involves resolving
[MyClass], and put it in the type cache with the wrongTypeResolutionState.