microsoft / microsoft/vs-threading

AsyncLazy can duck tape two callers into the same thread

Open
#457 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When two caller accesses the same AsyncLazy, it can end up with the second caller's continuation code to be executed inside the first caller's stack, which can cause some odd issues (and potentially lead into dead locks.) We found similar issue in CPS's configuredProjectCache, and I think AsyncLazy has a similar issue.

The problem happens, when the AsyncLazy function can finish without yielding the thread. After the first caller creates the value task, it will call resumableAwait.Resume() to execute the task. If the second caller happens to call in during that time, it will chain a task continuation to the value task. When the value task finishes, it will run into the task continuation code of the second task, before returning from the Resume() function. That leads the first task to be blocked by the second task's continuation to finish.

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 by reading the AsyncLazy implementation and tracing how the first caller resumes its value task and how a second caller chains a continuation. Compare the behavior with the referenced CPS configuredProjectCache issue; done means the second caller's continuation no longer runs inside the first caller's stack or leaves it vulnerable to deadlock.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.