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

ReadKey null reference exception #3779

Closed
3 tasks done
veleek opened this issue Aug 16, 2023 · 2 comments · Fixed by #3781
Closed
3 tasks done

ReadKey null reference exception #3779

veleek opened this issue Aug 16, 2023 · 2 comments · Fixed by #3781
Labels
In-PR A PR is opened targeting the issue

Comments

@veleek
Copy link

veleek commented Aug 16, 2023

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

### Environment
PSReadLine: 2.2.6
PowerShell: 7.3.6
OS: Microsoft Windows 10.0.19045
BufferWidth: 200
BufferHeight: 60

Last 8 Keys:

 c l s Enter
 UpArrow UpArrow UpArrow Enter


### Exception

System.NullReferenceException: Object reference not set to an instance of an object.
   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: 7.3.6
PS HostName: ConsoleHost (Windows Terminal)
PSReadLine Version: 2.2.6
PSReadLine EditMode: Windows
OS: 10.0.19041.1 (WinBuild.160101.0800)
BufferWidth: 280
BufferHeight: 71

Steps to reproduce

I have a long running script that uses a FileSystemWatcher to monitor for changes to a file.
I had a bug where I didn't set the source identifier before registering for events, like so:

$watcher = [System.IO.FileSystemWatcher]::new($logPath, $logFilter)
Register-ObjectEvent $watcher Created -SourceIdentifier $createdEventIdentifier

# Later on in function
$createdEventIdentifier = "Blah"

So I accidentally created a registeration without an event handler, then later on in a try/finally I did:

Unregister-Event -SourceIdentifier $createdEventIdentifier -ErrorAction "SilentlyContinue"

So the unregisteration wouldn't do anything.

The end result was that when I kill my function (with Ctrl+C), I get into a NullReferenceException loop. I'm still able to input text, but it just repeatedly dumps the exceptions shown above.

If I run Get-EventSubscriber | Unregister-Event (yes I can still successfully run commands while it's in this error loop), then the erroring stops.

Expected behavior

Existing event subscriptions should not cause problems with ReadKey.

Actual behavior

ReadKey returns exceptions repeatedly until event registration is removed.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Aug 16, 2023
@veleek
Copy link
Author

veleek commented Aug 16, 2023

I was able to reproduce this with this minimial function:

function test() {
    $watcher = [System.IO.FileSystemWatcher]::new("C:\", "*.log")
    Register-ObjectEvent $watcher Created -SourceIdentifier $null
}

Run the test function and see the error loop. Passing any non $null value (including empty string "")for SourceIdentifier works properly.

@microsoft-github-policy-service microsoft-github-policy-service bot added In-PR A PR is opened targeting the issue and removed Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. labels Aug 17, 2023
@veleek
Copy link
Author

veleek commented Aug 17, 2023

@daxian-dbw - Amazing turnaround! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In-PR A PR is opened targeting the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant