You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Trace-Command -Name MethodInvocation intermittently stops working #28447
classC
{
static [string] M([object] $arg1, [datetime] $arg2) { return'foo' }
}
# !! This call has the unexpected side effect of silencing the debug output from the# !! Trace-Command call below, as well as for repeat invocations with the same script block.# !! Commenting the call out makes the problem go away.# !! Making the call AFTER the Trace-Command call is OK, and repeat Trace-Command calls then work as # !! as expected.
[C]::M($false, [datetime]::now)
Trace-Command-PSHost -Name MethodInvocation { [C]::M($false, [datetime]::now) }
I don't fully understand the caching rules but the problem here stems from the fact that PowerShell caches a few different things in the binder. Based on my understanding there are two caches at play here:
Binder cache (PowerShell) - caches the binder object, keyed by member name, arg count, static/instance member
Rule cache (DLR) - caches the expression tree built by the binder, keyed by target and argument types
This is a very simplified example based on my understanding, there are edge cases and it doesn't cover the full details but hopefully it explains what is being cached at runtime here.
The relevant cache here is the rule cache and the expression tree that is generated and re-used when the same argument types are used. The tree for the tracer call is generated at
. The important thing to note is that this is only included in the expression tree if the tracer was enabled when the tree was created. It may look like it's run for each method call but with the caching it's only called per argument type (on the target type) and then re-used if a subsequent call uses those same types.
So extending your example you can see how changing the type will "re-enable" it.
classC
{
static [string] M([object] $arg1, [datetime] $arg2) { return'foo' }
}
# Caches expression tree for ([bool], [datetime])# This tree won't include the MethodInvocation write as it# is not enabled
[C]::M($false, [datetime]::now)
# Turning on the tracer won't do anything because the expression# tree that is cached by the binder for ([bool], [datetime]) won't# call the WriteLine MethodTrace-Command-PSHost -Name MethodInvocation { [C]::M($false, [datetime]::now) }
# Changing the runtime value also won't work because it is cached by type# and not valueTrace-Command-PSHost -Name MethodInvocation { [C]::M($true, [datetime]::now) }
# Changing the type of an arg will build a new expression tree and because# the tracer is enabled the tree includes the logic to write to the tracerTrace-Command-PSHost -Name MethodInvocation { [C]::M(1,[datetime]::now) }
# Even though the tree for ([int], [datetime]) include the expression to write# to the tracer, the tracer is now disabled so it becomes a no-op
[C]::M(1,[datetime]::now)
The fix would be to include a check in the expression tree to see if the tracer is enabled which is simple enough to do. IIRC I originally had this approach but when talking about it on the PowerShell Discord it was suggested to use the approach currently in the code for performance reasons. I could be misremembering things though. We do need to be careful as this is a hot path but a simple IL to check a property value sounds pretty cheap to me. Would be up to the WG to decide if they want to change this or just have it as part of a documented limitation for the tracer. I'll still open a PR and let them either close or continue with the PR if that's their choice.
Activity
jborean93 commentedon Nov 19, 2026
I don't fully understand the caching rules but the problem here stems from the fact that PowerShell caches a few different things in the binder. Based on my understanding there are two caches at play here:
This is a very simplified example based on my understanding, there are edge cases and it doesn't cover the full details but hopefully it explains what is being cached at runtime here.
The relevant cache here is the rule cache and the expression tree that is generated and re-used when the same argument types are used. The tree for the tracer call is generated at
PowerShell/src/System.Management.Automation/engine/runtime/Binding/Binders.cs
Lines 7037 to 7046 in cd071c9
So extending your example you can see how changing the type will "re-enable" it.
The fix would be to include a check in the expression tree to see if the tracer is enabled which is simple enough to do. IIRC I originally had this approach but when talking about it on the PowerShell Discord it was suggested to use the approach currently in the code for performance reasons. I could be misremembering things though. We do need to be careful as this is a hot path but a simple IL to check a property value sounds pretty cheap to me. Would be up to the WG to decide if they want to change this or just have it as part of a documented limitation for the tracer. I'll still open a PR and let them either close or continue with the PR if that's their choice.
jborean93 commentedon Nov 19, 2026
I've opened #28462.
mklement0 commentedon Nov 19, 2026
Thanks, Jordan Borean (@jborean93), I appreciate the deep dive and PR.