Skip to content

How is waiting meant to work when using Runspace.OpenAsync? #7034

Closed

Description

@alx9r

It seems like it should be possible to subscribe to Runspace.AvailabilityChanged and Runspace.StateChanged to wait for a runspace to be opened when using Runspace.OpenAsync(). However, I have not found a way to use those events in a manner that reliably waits until the opening process is complete.

Specifically, I am surprised that the runspace transitions to Available,Opened then Busy,Opened before it completes the import of modules specified by InitialSessionState. It seems like neither the Opened nor Available states should be reached until the module is imported.

Steps to reproduce

$moduleContent = {
    function fibonacci {
        param([int]$n)
        [bigint]$a=0
        [bigint]$b=1
        foreach ($x in 0..$n)
        {
            $a,$b = $b,($a+$b)
        }
        $b
    }
    0..0 | % { fibonacci 5000 }
}

$modulePath = "$([System.IO.Path]::GetTempPath())slowLoading.psm1"
$moduleContent | Set-Content $modulePath

$initialSessionState = [initialsessionstate]::CreateDefault()
$initialSessionState.ImportPSModule($modulePath)

$runspace = [runspacefactory]::CreateRunspace($initialSessionState)
$runspace.OpenAsync()

1..500 | 
    % {
        [pscustomobject]@{
            i         = $_
            Available = $runspace.RunspaceAvailability
            State     = $runspace.RunspaceStateInfo.State
        }
        sleep 0.5
    }

Expected behavior

 i Available State
 - ---------   -----
  1      None Opening
...
  8      None Opening
  9      Busy Opening
...
285      Busy Opening
286 Available  Opened

Actual behavior

  i Available State
  - ---------   -----
  1      None Opening
...
  8      None Opening
  9 Available  Opened
 10 Available  Opened
 11      Busy  Opened
...
285      Busy  Opened
286 Available  Opened

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0-preview.691
PSEdition                      Core
GitCommitId                    v6.1.0-preview.691
OS                             Microsoft Windows 6.3.9600 
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Question
    WG-Engine
    on Jun 9, 2018
  2. alx9r commented on Jun 15, 2018

    @alx9r
    Author

    FWIW it seems like pattern matching on a merged stream of RunspaceAvailabilityChanged and StateChanged events can be used to infer when a runspace is ready for use. There seems to be two characteristic streams depending on whether modules are specified in InitialSessionState.Modules.

    The characteristic event streams seem to be as shown below. The last event mentioned in the respective stream seems to indicate that the runspace is ready for use.

    No Modules Specified
    
    RunspaceAvailability RunspaceStateInfo
    -------------------- -----------------
                         Opening          
    Available                             
                         Opened           
    
    Modules Specified
    
    RunspaceAvailability RunspaceStateInfo 
    -------------------- ----------------- 
                         Opening           
    Available                              
                         Opened            
    Busy                                   
    Available                              
    

    I don't know how robust this method is, but it's the best method I have found for getting a signal when a runspace is ready for use. I've also found that having such a signal is critical for achieving speedup by utilizing multiple processors when executing CPU-bound PowerShell.

  3. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. added
    Resolution-No Activity
    on Nov 16, 2023
  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions