-
Notifications
You must be signed in to change notification settings - Fork 321
ReadKey null reference exception #3779
Description
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: 71Steps 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.