Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Safe handle has been closed' when stopping debugger while in a dynamicparam section in v2.2 #3751

Open
3 tasks done
fsackur opened this issue Dec 23, 2021 · 14 comments
Open
3 tasks done
Assignees
Labels

Comments

@fsackur
Copy link

fsackur commented Dec 23, 2021

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues, especially the pinned issues.

Exception report

Last 200 Keys:

<omitted irrelevant keystrokes>
 f o o Spacebar - b Tab

### Exception

System.ObjectDisposedException: Safe handle has been closed
   at System.Threading.WaitHandle.WaitMultiple(WaitHandle[] waitHandles, Int32 millisecondsTimeout, Boolean exitContext, Boolean WaitAll)
   at System.Threading.WaitHandle.WaitAny(WaitHandle[] waitHandles, Int32 millisecondsTimeout, Boolean exitContext)
   at Microsoft.PowerShell.PSConsoleReadLine.ReadKey()
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken, Nullable`1 lastRunStatus)

Screenshot

N/A

Environment data

PS Version: 5.1.19041.1320
PS HostName: Visual Studio Code Host
PSReadLine Version: 2.2.0-beta4
PSReadLine EditMode: Windows
OS: 10.0.19041.1 (WinBuild.160101.0800)
BufferWidth: 227
BufferHeight: 28

Steps to reproduce

  • Use VS Code with PowerShell Preview 2021.12.0 extension
  • Create a function with dynamic parameters, e.g.:
function foo
{
    [CmdletBinding()]
    param ()

    dynamicparam
    {
        $DynParams = [Management.Automation.RuntimeDefinedParameterDictionary]::new()

        $DynParam = [Management.Automation.RuntimeDefinedParameter]::new(
            'bar',
            [string],
            [Collections.ObjectModel.Collection[System.Attribute]]::new()
        )

        $DynParams.Add($DynParam.Name, $DynParam)

        return $DynParams
    }
}
  • Put a breakpoint within the dynamicparam block and start an interactive session
  • Invoke the dynamic parameter with foo - and hit <tab>
  • Debugger stops in the dynamicparam block, as expected
  • Stop the debugger with <Ctrl-F5>
  • Integrated console returns, but shows error message

Expected behavior

No exception

Actual behavior

Error shown

@daxian-dbw daxian-dbw transferred this issue from PowerShell/PSReadLine Jan 4, 2022
@ghost ghost added the Needs: Triage Maintainer attention needed! label Jan 4, 2022
@daxian-dbw
Copy link
Member

The exception indicates that the cancellationtoken passed to PSConsoleReadLine.ReadLine was disposed prematurely somehow.

@andyleejordan andyleejordan added Bug: Pre-release Bugs reproducing only in the pre-release extension. Issue-Bug A bug to squash. labels Jan 5, 2022
@andyleejordan andyleejordan added this to the Consider-vNext milestone Jan 5, 2022
@andyleejordan andyleejordan removed the Needs: Triage Maintainer attention needed! label Jan 5, 2022
@andyleejordan andyleejordan moved this to Todo in Sea Biscuit Jan 19, 2022
@andyleejordan andyleejordan moved this from Todo to In Progress in Sea Biscuit Jan 27, 2022
@andyleejordan
Copy link
Member

Well, I can reproduce this, which is great, but not sure why the token is getting prematurely disposed. Working on it 🤷

@andyleejordan
Copy link
Member

@daxian-dbw I think this recurs when ReadLine is invoked in a nested manner. In this bug report's case, the dynamic parameter was being expanded by ReadLine, then the debugger is invoked and a new nested debug prompt opens up, invoking ReadLine a second time. I was also able to reproduce accidentally when working on another bug, and it was again when ReadLine was running and I called it a second time. Is this a supported scenario for ReadLine, if not, what's the best way to handle it?

@daxian-dbw
Copy link
Member

It's not a supported scenario. PSConsoleReadLine.ReadLine is a static method, but the operations are all around a singleton of PSConsoleReadLine. Reentry of the PSReadLine means corrupting the text buffer of the singleton and more, so it won't work.

Is it possible to identify that we are re-entering the ReadLine method? If so, how about use the old legacy readline in that rare scenario?

@SeeminglyScience
Copy link
Collaborator

Yeah even debugging outside of VSCode seems to fall apart in this scenario. We may want to consider automatically resuming in this case, and suggest debugging these scenarios be done via attach to process.

@andyleejordan
Copy link
Member

Hey @fsackur, I can no longer reproduce this with the latest preview, and we fixed some issues around when to display the prompt in PowerShell/PowerShellEditorServices#1690. Can you please verify that this no longer reproduces with v2022.2.0-preview?

@andyleejordan andyleejordan added Needs: Fix Verification Please verify the fix for us! Resolution-Fixed Will close automatically. labels Feb 9, 2022
@andyleejordan
Copy link
Member

Marking as fixed, if it does still repro, please let me know, and re-open it!

@ghost ghost closed this as completed Feb 9, 2022
@ghost
Copy link

ghost commented Feb 9, 2022

This issue has been marked as fixed. It has been automatically closed for housekeeping purposes.

Repository owner moved this from In Progress to Done in Sea Biscuit Feb 9, 2022
@fsackur
Copy link
Author

fsackur commented Feb 12, 2022

I do still see the issue in VS Code 1.64.2 with the PS Preview extension in the PS Integrated Terminal using PSv7.2.1:

### Environment
PSReadLine: 2.2.0-beta5
PowerShell: 2022.2.0
OS: Microsoft Windows 10.0.22000
BufferWidth: 256
BufferHeight: 31

However, in PSRL 2.2.1-rc1 in a "normal" terminal, I do not see the issue.

@andyleejordan andyleejordan reopened this Feb 14, 2022
@ghost ghost closed this as completed Feb 14, 2022
@andyleejordan andyleejordan reopened this Feb 14, 2022
@andyleejordan andyleejordan removed the Resolution-Fixed Will close automatically. label Feb 14, 2022
@andyleejordan andyleejordan moved this from Done to In Progress in Sea Biscuit Feb 16, 2022
@andyleejordan
Copy link
Member

This looks pretty broken in the stable extension too 😢

@andyleejordan andyleejordan removed the status in Sea Biscuit Feb 23, 2022
@andyleejordan andyleejordan added Area-Debugging and removed Needs: Fix Verification Please verify the fix for us! Bug: Pre-release Bugs reproducing only in the pre-release extension. labels Feb 23, 2022
@andyleejordan andyleejordan moved this from Todo to Punted in Sea Biscuit Feb 23, 2022
@andyleejordan andyleejordan moved this to Todo in Sea Biscuit Feb 23, 2022
@andyleejordan andyleejordan moved this from Punted to Wishlist in Sea Biscuit Mar 15, 2022
@andyleejordan andyleejordan moved this from Wishlist to Punted in Sea Biscuit Mar 15, 2022
@andyleejordan andyleejordan moved this from Todo to In Progress in American Pharoah Apr 19, 2022
@andyleejordan
Copy link
Member

Unfortunately still repros with v2022.4.1-preview and with PowerShell/PowerShellEditorServices#1758, but next up is some serious work in PSReadLine.

@SeeminglyScience
Copy link
Collaborator

It'd be nice to have a better error message here, but this is still a case of needing re-entrance in PSReadLine. Now that attach to process works, I think that's the best way to debug argument completers, tab completion in general, and PSReadLine key handlers.

@andyleejordan
Copy link
Member

andyleejordan commented Apr 19, 2022

Agreed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Wishlist
Status: Punted
Development

No branches or pull requests

5 participants