Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Add Delegate cast to Task constructors #5765

Merged
merged 6 commits into from
May 2, 2018
Merged

Conversation

A-And
Copy link
Contributor

@A-And A-And commented May 2, 2018

Running runtest.cmd /corefx on Windows with the System.Threading.Tests in the CoreFX test list hits the following assertion

Debug.Assert(m_contingentProperties == null || m_contingentProperties.m_capturedContext == null,

The assertion is caused by a Task constructors calling an overloaded constructor, both of which call PossiblyCaptureContext in their body. Is this intentional?

Adding the cast resolves the constructor to this one

internal Task(Delegate action, object state, Task parent, CancellationToken cancellationToken,

This seems to fix the problem without glaring regressions, including allowing System.Threading.Tests to run under Windows.

@jkotas
Copy link
Member

jkotas commented May 2, 2018

I would be nice to fix this by making the code more similar to the (more optimized) CoreCLR version: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Threading/Tasks/Task.cs .

The CoreCLR version does not have PossiblyCaptureContext() method at all. Instead, the context is captured in TaskConstructorCore.

@jkotas
Copy link
Member

jkotas commented May 2, 2018

Could you please delete This variant does not capture the ExecutionContext; it is up to the caller to do that comment that is no longer applicable.

@@ -590,6 +581,8 @@ public Task(Action<object> action, object state, CancellationToken cancellationT

AssignCancellationToken(cancellationToken, null, null);
}

PossiblyCaptureContext();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would inline it here, so that it looks just like CoreCLR version. It is not really worth it to have it in a separate function when it is called just from a single place.

Copy link
Member

@jkotas jkotas May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it really be called from there now that it is called from the core Task constructor?

Note that Task<T> inherits from Task.

@@ -476,14 +469,12 @@ public Task(Action<object> action, object state, TaskCreationOptions creationOpt
public Task(Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
: this(action, state, Task.InternalCurrentIfAttached(creationOptions), cancellationToken, creationOptions, InternalTaskOptions.None, null)
{
PossiblyCaptureContext();
}

internal Task(Action<object> action, object state, Task parent, CancellationToken cancellationToken,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor can be deleted now, I think.

@A-And A-And changed the title [WIP] Add Delegate cast to Task constructors Add Delegate cast to Task constructors May 2, 2018
@jkotas jkotas merged commit 671fc1c into dotnet:master May 2, 2018
@A-And A-And deleted the TaskCtor branch May 2, 2018 21:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants