Skip to content

Instantly share code, notes, and snippets.

@franklesniak
Created August 3, 2015 15:25
Show Gist options
  • Save franklesniak/6f208d9458be5f1f8c77 to your computer and use it in GitHub Desktop.
Save franklesniak/6f208d9458be5f1f8c77 to your computer and use it in GitHub Desktop.
This script reproduces a problem with PowerShell v4 when KB3000850 is installed, and with PowerShell v5. It causes the interpreter to freeze / deadlock / spinlock / something that causes it to be unresponsive with high CPU
# This script should be dot-sourced or copied and pasted into the interpreter.
# This script reproduces a problem with PowerShell v4 when KB3000850 is installed, and with PowerShell v5.
# It causes the interpreter to freeze / deadlock / spinlock / something that causes it to be unresponsive with high CPU
# See also: https://gist.github.com/anonymous/572f42ba0ea7eec4f721#file-psclass_repro_reduced11-ps1
function __PSClass-InvokeScript($class, $script, $object) {}
function __PSClass-InvokePropertyMethod($Class, $PropertyType, $PropertyName, $instance) {
# Interestingly, if you comment-out the following line, the script exhibits the high CPU behavior but does *eventually* complete!
# However, if you insert a breakpoint here in PowerShell ISE, invoking a command like $class will cause the ISE to freeze at "Stopping Intellisense"
__PSClass-InvokeScript $FoundClass $property.SetScript $instance $params # I would love to remove this pointless function call, but then I can't repro...
}
$class = New-Object Management.Automation.PSObject
$member = New-Object Management.Automation.PSNoteProperty "Properties", @{}
$class.PSObject.Members.Add($member)
[scriptblock]$script = `
{
Function AttachAndInit($instance, [array]$params)
{
$instance = __PSClass-AttachObject $this $instance
__PSClass-Initialize $this $instance $params
}
[array]$params = $Args[1]
AttachAndInit $Args[0] $params
}
$member = New-Object Management.Automation.PSScriptMethod "AttachTo", $script
$class.PSObject.Members.Add($member)
[scriptblock]$script = `
{
$instance = New-Object Management.Automation.PSObject
$this.AttachTo($instance, $Args)
}
$member = New-Object Management.Automation.PSScriptMethod "New", $script
$class.PSObject.Members.Add($member)
[scriptblock]$script = `
{
# This code is invoked whenever the properties of $instance are read (at the end of the script)
__PSClass-InvokePropertyMethod $this $Args[0] $Args[1] $Args[2] # PowerShell seems to struggle with this call
}
$member = New-Object Management.Automation.PSScriptMethod "InvokeProperty", $script
$class.PSObject.Members.Add($member)
$class.Properties["Legs"] = @{Name="Legs";GetScript={"None"};SetScript=$null;Private=$false;Override=$false}
$instance = New-Object Management.Automation.PSObject
$member = New-Object Management.Automation.PSNoteProperty "Class", $class
$instance.PSObject.Members.Add($member)
$getScript = {$instance.Class.InvokeProperty( "GET", "Legs", $instance )}
$scriptProperty = New-Object Management.Automation.PsScriptProperty "Legs", $getScript
$instance.PSObject.Properties.Add($scriptProperty)
Write-Host "To reproduce the problem, run the command: `$instance"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment