Skip to content

Trace-Command -Name MethodInvocation intermittently stops working #28447

Open

Description

Prerequisites

Steps to reproduce

class C
{
  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) }

Expected behavior

foo
DEBUG: <timestamp> MethodInvocation Information: 0 : Invoking method: static string M(System.Object arg1, datetime arg2)
foo

Actual behavior

foo
foo

That is, the debug output is missing.

Error details

Environment data

PowerShell 7.7.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Nov 18, 2026
  2. jborean93 commented on Nov 19, 2026

    @jborean93
    Collaborator

    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:

    1. Binder cache (PowerShell) - caches the binder object, keyed by member name, arg count, static/instance member
    2. 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

    if (MethodInvocationTracer.IsEnabled)
    {
    expr = Expression.Block(
    Expression.Call(
    Expression.Constant(MethodInvocationTracer),
    CachedReflectionInfo.PSTraceSource_WriteLine,
    Expression.Constant("Invoking method: {0}"),
    Expression.Constant(result.methodDefinition)),
    expr);
    }
    . 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.

    class C
    {
      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 Method
    Trace-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 value
    Trace-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 tracer
    Trace-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.

  3. jborean93 commented on Nov 19, 2026

    @jborean93
    Collaborator

    I've opened #28462.

  4. mklement0 commented on Nov 19, 2026

    @mklement0
    ContributorAuthor

    Thanks, Jordan Borean (@jborean93), I appreciate the deep dive and PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions