Skip to content

Possible issue with Equals implementation in ActivityTraceId  #85198

Open

Description

Description

I suspect that there is an issue with the way equality is implemented in ActivityTraceId. If the activity.TraceId has a default value and we use either the == operator or Equals method to compare it with the default ActivityTraceId, it always seems to returns false. This behavior is unexpected and can lead to bugs in applications that rely on this comparison.

Using F12 on Visual Studio to see the definition of ActivityTraceId, I noticed that it internally uses _hexString to calculate HashCode and for Equals and == implementation. When we use default, the _hexString is uninitialized which causes this comparison to fail.

Please call out if I am doing or expecting something unreasonable. Thanks.

Reproduction Steps

  1. Create an Activity object with a default ActivityTraceId.
  2. Compare the activity.TraceId with the default ActivityTraceId using the == operator or Equals method.
  3. Observe that the comparison always returns false, even though the TraceId property of the activity is set to the default value.

Here is a simple xUnit Test to demonstrate the same. (My library targets .NET Standard 2.1 and uses .NET 6.)

[Fact]
public void CompareTraceIdWithDefault()
{
    Activity.TraceIdGenerator = () => default;
    using var activity = new Activity("my-activity").Start();
       
    // All of these fail.
    Assert.Equal(default, activity.TraceId);
    Assert.True(default(ActivityTraceId) == activity.TraceId);
    Assert.True(default(ActivityTraceId).Equals(activity.TraceId));

    // This works.
    Assert.Equal(default(ActivityTraceId).ToString(), activity.TraceId.ToString());
}

Expected behavior

When comparing the activity.TraceId with the default ActivityTraceId using the == operator or Equals method, the comparison should return true if the activity.TraceId has the default value.`

Actual behavior

When comparing the activity.TraceId with the default ActivityTraceId using the == operator or Equals method, the comparison always returns false, even if the activity.TraceId has the default value.

Regression?

No response

Known Workarounds

Use ToString() and then compare.

Configuration

  • Which version of .NET is the code running on?
    .NET 6 (6.0.408)
  • What OS and version, and what distro if applicable?
    Windows 10.
  • What is the architecture (x64, x86, ARM, ARM64)?
    x64
  • Do you know whether it is specific to that configuration?
    Don't think so.

Other information

No response

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions