Skip to content

PowerShell classes leak to other runspaces on macOS #9313

Closed

Description

@TylerLeonhardt

Steps to reproduce

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.
image

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?)

Activity

  1. added
    Issue-Question
    Issue-Bug
    and removed
    Issue-Question
    on Apr 7, 2019
  2. vexx32 commented on Apr 7, 2019

    @vexx32
    Collaborator

    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?

  3. TylerLeonhardt commented on Apr 7, 2019

    @TylerLeonhardt
    MemberAuthor

    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()
  4. rkeithhill commented on Apr 7, 2019

    @rkeithhill
    Collaborator

    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.

  5. SeeminglyScience commented on Apr 8, 2019

    @SeeminglyScience
    Contributor

    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:

    & {
        class MyClass {}
    }
    
    # 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

    Also Rain Sallow (/u/ta11ow) (@vexx32)

    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?

    Only when you pass [System.Management.Automation.RunspaceMode]::CurrentRunspace

  6. daxian-dbw commented on Dec 5, 2019

    @daxian-dbw
    Member

    PSReadLine plays a role in the repo. To get consistent repro on all platforms (including powershell.exe), do the following:

    1. start pwsh/powershell.exe with -noprofile
    2. 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"
        }
      }
    
    1. copy the following to pwsh/powershell:
    [MyClass].GetType()
    [PowerShell]::Create().AddScript('[MyClass]::new("ffff")').Invoke()
    

    Then you will see the Name: ffff printed 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 wrong TypeResolutionState.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugResolution-Fixed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions