Skip to content

feat(tracing): add async context propagation helpers for JoinableTaskFactory #7

@CalvinAllen

Description

@CalvinAllen

Summary

Activity.Current can be lost when switching threads in Visual Studio's async model, particularly when using JoinableTaskFactory. Add helpers to properly flow tracing context across async boundaries.

Problem

using (VsixTelemetry.StartActivity("ParentOperation"))
{
    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
    // Activity.Current may be null here!
    
    using (VsixTelemetry.StartActivity("ChildOperation"))
    {
        // This span won't be linked to parent
    }
}

Proposed Solution

// Option 1: Extension method for JoinableTaskFactory
await ThreadHelper.JoinableTaskFactory
    .WithActivityContext()
    .SwitchToMainThreadAsync();

// Option 2: Wrapper that captures and restores context
using (VsixTelemetry.PreserveContext())
{
    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
    // Activity.Current is preserved
}

// Option 3: Run with explicit context
await VsixTelemetry.RunWithContextAsync(async () =>
{
    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
    // Context flows correctly
});

Implementation Notes

  • Capture Activity.Current before thread switch
  • Restore it after the switch using Activity.SetCurrent()
  • Consider using AsyncLocal<T> for automatic propagation
  • Test with various VS async patterns

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions