[API Proposal]: ValueTask->Task: allow deferred callback hook for IVTS scenario
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start with the linked ValueTask.cs code around the custom Task subscription for typed and untyped IVTS cases. Trace how status checks and completion operations currently subscribe, then define the deferred-state behavior for the two proposed usage scenarios. Done means the subscription is deferred until a result-related operation and the pending and fault-detected paths are covered by tests.
Written by the indexing model from the issue text.
Description
Background and motivation
There are times when awaitable items are used as "futures", but in a pre-pending state. Meaning: trying to await them right now will be a doomed deadlock, unless you have some kind of cancellation/timeout in place. Example:
// SE.Redis
var tran = dB.CreateTransaction();
var pending = tran.IncrAsync(key);
//... others
await pending; // ERROR
// the above is doomed until after
await tran.ExecuteAsync();
// down here (after the ExecuteAsync): this would be fine
var count = await pending;
This is detectable in custom awaitables, and in valuetasks based on IVTS - we can check the state in OnCompleted etc, and if it is doomed: transition into a faulted state. However, this currently doesn't work if someone has used AsTask() to get the pending operation as a Task, because the custom Task for IVTS subscribes immediately, here (x2, typed/untyped). This means that the moment they call AsTask, we would transition it to doomed.
API Proposal
No public API change.
Internally, use a deferred subscription mechanism for Pending IVTS cases. The IVTS is already stored (otherwise, we could use AsyncState). Proposal: add a new deferred state; when in that state:
- status checks (IsCompleted, etc) should refer to the IVTS instead of locally
- result related operations (Wait, GetResult, OnCompleted, ContinueWith, etc) would only then, first time subscribe to the IVTS via OnCompleted etc, transitioning back into local mode; once in local mode, normal service is resumed
API Usage
No public signature change. Behaviourally, the following scenario becomes possible:
var task = prePendingIVTS.AsTask();
MakeHappy(prePendingIVTS); //
await task; // fine
And
var task = prePendingIVTS.AsTask();
await task; // boom, fault detected
The point here is correctness and simplicity of debugging. Currently, because we can't prevent people turning VTs to Ts via AsTask(), we have to omit the deadlock detection that would have told them what they did wrong.
Probably a @stephentoub topic!
I can provide examples and tests if this is of interest.
Alternative Designs
Risks
No response
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·