microsoft / microsoft/vs-threading

VSTHRD004 incorrectly fires on results of methods are returned to the caller

Open
#377 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

analyzers
Dominant language
C#
Stars
1k
Forks
160
Avg merge
1d 12h
Merged PRs (30d)
28

Description

Bug description

VSTHRD004 incorrectly fires on results of methods are returned to the caller.

Repro steps
        public static JoinableTaskFactory.MainThreadAwaitable SwitchToUIThread(this IProjectThreadingService threading, CancellationToken cancellationToken)
        {
            return threading.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); <!-- VSTHRD004
        }
Expected behavior

VSTHRD004 not to fire because the result is returned to the caller.

Actual behavior
Severity	Code	Description	File	Project	Line	Suppression State
Error	VSTHRD004	Calls to JoinableTaskFactory.SwitchToMainThreadAsync() must be awaited.	E:\project-system2\src\Microsoft.VisualStudio.ProjectSystem.Managed\ProjectSystem\ThreadingExtensions.cs	Microsoft.VisualStudio.ProjectSystem.Managed	30	Active

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the VSTHRD004 diagnostic logic and the reproduction in ThreadingExtensions.cs, where SwitchToMainThreadAsync is returned from SwitchToUIThread. Run the shown C# case and confirm that returning the awaitable no longer produces VSTHRD004 while direct unawaited calls remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.