PowerShellEditorServices
PowerShellEditorServices copied to clipboard
Variable Expansion: ScriptProperty ToStrings are slow to perform

Per @SeeminglyScience it's probably because the ToString is trying to marshal to the pipeline thread and deadlocking.
@SeeminglyScience looks like it's not hard-hanging, just a performance issue. Could this have to do with the marshaling still?
https://user-images.githubusercontent.com/15258962/151478261-4f93b844-b432-41ee-ba6b-399335ff7db7.mp4
Narrowed it down to this loop for enumerables that is "slow", I'll dig deeper into VariableDetails later.
https://github.com/PowerShell/PowerShellEditorServices/blob/624fe30eda7b7dc918978e238a59b26b81a40dc7/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs#L338-L344
@SeeminglyScience looks like it's not hard-hanging, just a performance issue. Could this have to do with the marshaling still?
Yep!
In certain circumstances (like this one) it will timeout after 250ms and then just invoke it anyway from a different thread (while still targeting the same runspace) potentially causing state corruption. So I'd say that still perfectly explains it and potentially makes it a little bit more scary.
Here's the culprit: https://github.com/PowerShell/PowerShellEditorServices/blob/624fe30eda7b7dc918978e238a59b26b81a40dc7/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs#L198
If you set it to null instead of SafeToString(), everything is fast again.
More specifically: https://github.com/PowerShell/PowerShellEditorServices/blob/3274c230cc6675859d5c89d6c176219c2c9430f7/src/PowerShellEditorServices/Utility/Extensions.cs#L25
@SeeminglyScience how would I get this obj.ToString() method on the correct invocation? It does feel like about a 250ms delay on each invocation.
Adding @SeeminglyScience as I've hit my limits on figuring this out for now :)
And fixed, with your test too!