Skip to content

InvalidRunspaceStateException: ... runspace is not in the Opened state #1965

Open
@rjmholt

Description

@rjmholt

System Details

System Details Output

### VSCode version: 1.30.2 61122f88f0bf01e2ac16bdb9e1bc4571755f5bd8 x64

### VSCode extensions:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]


### PSES version: 2.0.0.0

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.18898
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Description

When a nested expression is entered into a string in a PowerShell editor buffer, PSRL will crash due to a runspace exception. It's likely the root cause isn't with PSRL integration but with PowerShellExecutionContext.

Here's a repro:

Step 1

filter AssembleCLEntry
{
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [ChangelogItem]
        $CLItem,

        [Parameter()]
        [string]
        $Organization = 'PowerShell',

        [Parameter()]
        [string]
        $Repository = 'vscode-powershell'
    )

    return [ChangelogEntry]@{
        IssueLink = if ($CLItem.IssueNumber -ge 0) { "https://github.com/$Organization/$Repository/issues/$($CLItem.IssueNumber)" }
        PRLink = if ($CLItem.PRNumber -ge 0) { "https://github.com/$Organization/$Repository/$" # <-- Next char in the string will crash the extension }
    }
}

Step 2

filter AssembleCLEntry
{
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [ChangelogItem]
        $CLItem,

        [Parameter()]
        [string]
        $Organization = 'PowerShell',

        [Parameter()]
        [string]
        $Repository = 'vscode-powershell'
    )

    return [ChangelogEntry]@{
        IssueLink = if ($CLItem.IssueNumber -ge 0) { "https://github.com/$Organization/$Repository/issues/$($CLItem.IssueNumber)" }
        PRLink = if ($CLItem.PRNumber -ge 0) { "https://github.com/$Organization/$Repository/$(" # <-- BOOM! }
    }
}

Expected Behaviour

Life as normal

Actual Behaviour

Extension crashes

Attached Logs

Relevant Log entry:


2019-05-15 10:38:21.327 [ERROR] tid:26 in 'OnListenTaskCompleted' C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs: line 391
    ProtocolEndpoint message loop terminated due to unhandled exception:
    
    System.AggregateException: One or more errors occurred. (Index and length must refer to a location within the string.
    Parameter name: length) ---> System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
    Parameter name: length
       at System.String.Substring(Int32 startIndex, Int32 length)
       at Microsoft.PowerShell.EditorServices.FindReferencesVisitor.VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Language\FindReferencesVisitor.cs:line 146
       at System.Management.Automation.Language.FunctionDefinitionAst.InternalVisit(AstVisitor visitor)
       at System.Management.Automation.Language.StatementBlockAst.InternalVisit(AstVisitor visitor, ReadOnlyCollection`1 traps, ReadOnlyCollection`1 statements, AstVisitAction action)
       at System.Management.Automation.Language.NamedBlockAst.InternalVisit(AstVisitor visitor)
       at System.Management.Automation.Language.ScriptBlockAst.InternalVisit(AstVisitor visitor)
       at System.Management.Automation.Language.Ast.Visit(AstVisitor astVisitor)
       at Microsoft.PowerShell.EditorServices.AstOperations.FindReferencesOfSymbol(Ast scriptAst, SymbolReference symbolReference, Dictionary`2 CmdletToAliasDictionary, Dictionary`2 AliasToCmdletDictionary) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Language\AstOperations.cs:line 203
       at Microsoft.PowerShell.EditorServices.LanguageService.FindReferencesOfSymbolAsync(SymbolReference foundSymbol, ScriptFile[] referencedFiles, Workspace workspace) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Language\LanguageService.cs:line 362
       at Microsoft.PowerShell.EditorServices.CodeLenses.ReferencesCodeLensProvider.ResolveCodeLensAsync(CodeLens codeLens, CancellationToken cancellationToken) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Host\CodeLens\ReferencesCodeLensProvider.cs:line 85
       at Microsoft.PowerShell.EditorServices.CodeLenses.CodeLensFeature.HandleCodeLensResolveRequestAsync(CodeLens codeLens, RequestContext`1 requestContext) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Host\CodeLens\CodeLensFeature.cs:line 176
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.DispatchMessageAsync(Message messageToDispatch, MessageWriter messageWriter) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\MessageDispatcher.cs:line 177
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.ListenForMessagesAsync(CancellationToken cancellationToken) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs:line 385
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContext.cs:line 50
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContextThread.<>c__DisplayClass4_0.<Run>b__0() in C:\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContextThread.cs:line 67
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       --- End of inner exception stack trace ---
    ---> (Inner Exception #0) System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
    Parameter name: length
       at System.String.Substring(Int32 startIndex, Int32 length)
       at Microsoft.PowerShell.EditorServices.FindReferencesVisitor.VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Language\FindReferencesVisitor.cs:line 146
       at System.Management.Automation.Language.FunctionDefinitionAst.InternalVisit(AstVisitor visitor)
       at System.Management.Automation.Language.StatementBlockAst.InternalVisit(AstVisitor visitor, ReadOnlyCollection`1 traps, ReadOnlyCollection`1 statements, AstVisitAction action)
       at System.Management.Automation.Language.NamedBlockAst.InternalVisit(AstVisitor visitor)
       at System.Management.Automation.Language.ScriptBlockAst.InternalVisit(AstVisitor visitor)
       at System.Management.Automation.Language.Ast.Visit(AstVisitor astVisitor)
       at Microsoft.PowerShell.EditorServices.AstOperations.FindReferencesOfSymbol(Ast scriptAst, SymbolReference symbolReference, Dictionary`2 CmdletToAliasDictionary, Dictionary`2 AliasToCmdletDictionary) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Language\AstOperations.cs:line 203
       at Microsoft.PowerShell.EditorServices.LanguageService.FindReferencesOfSymbolAsync(SymbolReference foundSymbol, ScriptFile[] referencedFiles, Workspace workspace) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Language\LanguageService.cs:line 362
       at Microsoft.PowerShell.EditorServices.CodeLenses.ReferencesCodeLensProvider.ResolveCodeLensAsync(CodeLens codeLens, CancellationToken cancellationToken) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Host\CodeLens\ReferencesCodeLensProvider.cs:line 85
       at Microsoft.PowerShell.EditorServices.CodeLenses.CodeLensFeature.HandleCodeLensResolveRequestAsync(CodeLens codeLens, RequestContext`1 requestContext) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Host\CodeLens\CodeLensFeature.cs:line 176
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.DispatchMessageAsync(Message messageToDispatch, MessageWriter messageWriter) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\MessageDispatcher.cs:line 177
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.ListenForMessagesAsync(CancellationToken cancellationToken) in C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs:line 385
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger) in C:\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContext.cs:line 50
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContextThread.<>c__DisplayClass4_0.<Run>b__0() in C:\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContextThread.cs:line 67
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)<---
    

2019-05-15 10:38:21.327 [ERROR] tid:26 in 'ProtocolEndpoint_UnhandledException' C:\PowerShellEditorServices\src\PowerShellEditorServices.Host\EditorServicesHost.cs: line 490
    PowerShell Editor Services is terminating due to an unhandled exception, see previous logs for details.

2019-05-15 10:38:21.458 [ERROR] tid:1 in 'PopRunspace' C:\PowerShellEditorServices\src\PowerShellEditorServices\Session\PowerShellContext.cs: line 2486
    Caller attempted to pop a runspace when no runspaces are on the stack.

Console exception:

System.Management.Automation.Runspaces.InvalidRunspaceStateException: Cannot invoke the pipeline because the runspace is not in the Opened state. Current state of the runspace is 'Closing'.
   at System.Management.Automation.Runspaces.RunspaceBase.AddToRunningPipelineList(PipelineBase pipeline)
   at System.Management.Automation.Runspaces.RunspaceBase.DoConcurrentCheckAndAddToRunningPipelines(PipelineBase pipeline, Boolean syncCall)
   at System.Management.Automation.Runspaces.PipelineBase.CoreInvoke(IEnumerable input, Boolean syncCall)
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.PSConsoleReadLine.ReadKey()
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken)

This might also be related to PowerShell/PowerShellEditorServices#907

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions