Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Use Task.FromCanceled<TResult>() on NETSTANDARD1_3#772

Merged
cesarblum merged 1 commit into
aspnet:devfrom
justinvp:taskutilities
Apr 28, 2016
Merged

Use Task.FromCanceled<TResult>() on NETSTANDARD1_3#772
cesarblum merged 1 commit into
aspnet:devfrom
justinvp:taskutilities

Conversation

@justinvp
Copy link
Copy Markdown
Contributor

Task.FromCanceled<int>(cancellationToken) can be used on NETSTANDARD1_3 in TaskUtilities.GetCancelledZeroTask().

@dnfclas
Copy link
Copy Markdown

dnfclas commented Apr 26, 2016

Hi @justinvp, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by .NET Foundation and real humans are currently evaluating your PR.

TTYL, DNFBOT;

@cesarblum
Copy link
Copy Markdown
Contributor

Not sure what the benefit of this is. I'm inclined not to take it since it only adds code with no clear improvement.

@justinvp
Copy link
Copy Markdown
Contributor Author

justinvp commented Apr 26, 2016

Benefits:

  • Avoids an unnecessary TaskCompletionSource<int> allocation on Net Standard.
  • The implementation of GetCancelledZeroTask() now matches GetCancelledTask() (the method above it).
  • This comment: // Task<int>.FromCanceled doesn't return Task<int> is misguided and confusing. There is a method that returns a canceled Task<int>: Task.FromCanceled<int>() (whoever wrote the comment wasn't looking at the right method).

@justinvp
Copy link
Copy Markdown
Contributor Author

justinvp commented Apr 26, 2016

Another benefit:

  • FrameRequestStream.ValidateState now matches FrameResponseStream.ValidateState, passing along the cancellation token to the canceled task when the state is Open.

throw new ObjectDisposedException(nameof(FrameRequestStream));
case FrameStreamState.Aborted:
return TaskUtilities.GetCancelledZeroTask();
return TaskUtilities.GetCancelledZeroTask(default(CancellationToken));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to this change, but I noticed that FrameResponseStream.ValidateState only returns a canceled task for the Aborted case when cancellationToken.IsCancellationRequested is true, whereas here it is always returning a canceled task for Aborted regardless of cancellationToken.IsCancellationRequested.

FrameResponseStream.ValidateState (src):

                case FrameStreamState.Aborted:
                    if (cancellationToken.IsCancellationRequested)
                    {
                        // Aborted state only throws on write if cancellationToken requests it
                        return TaskUtilities.GetCancelledTask(cancellationToken);
                    }
                    break;

Is this by design, or should FrameRequestStream be modified to behave the same as FrameResponseStream?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is by design #567 (comment) and the reading and writing are different.

@mikeharder
Copy link
Copy Markdown
Contributor

@CesarBS, @halter73: I do think we should merge this for the reasons listed above by @justinvp.

}

public static Task<int> GetCancelledZeroTask()
public static Task<int> GetCancelledZeroTask(CancellationToken cancellationToken)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would add an overload GetCancelledZeroTask() which calls GetCancelledZeroTask(CancellationToken.None). This is cleaner than requiring the caller to pass default(CancellationToken) or CancellationToken.None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How about making it an optional parameter with a default value of default(CancellationToken)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure, either would be fine.

@cesarblum
Copy link
Copy Markdown
Contributor

cesarblum commented Apr 27, 2016

@justinvp @mikeharder Yeah, the point about avoiding an allocation made me change my mind 😄 It's sad though that we can't have this on all frameworks.

@justinvp
Copy link
Copy Markdown
Contributor Author

justinvp commented Apr 27, 2016

Sorry for not making the benefits more clear from the start. I'll update the PR, addressing @mikeharder's feedback, later today.

Task.FromCanceled<int>(cancellationToken) can be used on
NETSTANDARD1_3 in TaskUtilities.GetCancelledZeroTask().
@justinvp
Copy link
Copy Markdown
Contributor Author

Feedback addressed. Thanks for the review.

@cesarblum
Copy link
Copy Markdown
Contributor

:shipit:

@cesarblum cesarblum merged commit 68f14c0 into aspnet:dev Apr 28, 2016
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.

5 participants