microsoft / microsoft/vs-threading

JoinableTaskSynchronizationContext should override CreateCopy()

Open
#352 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug description

The default implementation on SynchronizationContext.CreateCopy() returns a new instance of its own type.
But that type has very different behavior than JoinableTaskSynchronizationContext, so it would be incorrect to return the base type from our derived type.

Repro steps

I don't know what failures this may lead to yet. But given when ExecutionContext is captured it can (sometimes?) flow SynchronizationContext, I suspect if we can isolate when that happens we can find a repro for a bug.

Expected behavior

We should override the CreateCopy() method and have it return a copy of the current instance. The copy should behave exactly like the original, including common values for all fields so that they share queues, etc. But it should be a unique instance (not this) to avoid exposing bugs in others' code such as that learned by WPF, as pointed out by @weltkante in #347.

Additional context

@jviau noticed some test failures while working on the ReentrantSemaphore in certain conditions that he attributed to our CreateCopy() behavior, IIRC.

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 JoinableTaskSynchronizationContext and the SynchronizationContext.CreateCopy() contract described in the issue. Trace how the current instance stores queues and other shared values, then verify that a distinct copy preserves the original behavior and shared state without returning the same object. Use the existing ReentrantSemaphore-related tests or failures, if present, to confirm the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
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.