Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Review: should IResettableService.ResetStateAsync() return ValueTask #16675

Closed
ajcvickers opened this issue Jul 19, 2019 · 5 comments · Fixed by #16761
Closed

API Review: should IResettableService.ResetStateAsync() return ValueTask #16675

ajcvickers opened this issue Jul 19, 2019 · 5 comments · Fixed by #16761
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Contributor

Currently IResettableService.ResetStateAsync() returns ValueTask since the one place we need an async path is in RelationalConnection where we use it to async dispose the DbConnection and the DbTransaction:

public virtual async ValueTask DisposeAsync()
{
    if (CurrentTransaction != null)
    {
        await CurrentTransaction.DisposeAsync();
    }
    ClearTransactions(clearAmbient: true);
    if (_connectionOwned
        && _connection != null)
    {
        await DbConnection.DisposeAsync();
        _connection = null;
        _openedCount = 0;
    }
}

So there are no async paths that this uses that call any Task returning async methods.

@ajcvickers
Copy link
Contributor Author

Make it Task

@roji
Copy link
Member

roji commented Jul 26, 2019

Note that I checked, and ValueTask.AsTask() doesn't allocate if the ValueTask is completed (it just returned Task.CompletedTask. So there's really no loss of perf in returning Task here instead of ValueTask, and a better API being exposed.

@roji
Copy link
Member

roji commented Jul 26, 2019

Note: #16761 also adds a cancellation token to IResettableService - resetting isn't the same as disposing, so it seems like the general logic of having a token should hold (and it also calms down our API consistency tests).

/cc @divega

@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 26, 2019
@divega
Copy link
Contributor

divega commented Jul 27, 2019

@roji is cancelling the reset meaningful?

@roji
Copy link
Member

roji commented Jul 27, 2019

I admit I only added this after the API consistency tests raised an error, and I hesitated about this for a while. The general practice seems to be to always allow async operations to be cancellable by default, except for some very specific cases, such as the DisposeAsync pit of failure. That specific pit of failure doesn't seem to exist here, so there's at least no strong reason not to make it cancellable.

Here's a (very) theoretical case where this could be useful. We currently reset DbContext's services sequentially, one by one. This in theory could be done in parallel with Task.WhenAll (although if services are expected to use the same connection resource, that would be a big bug). In this scenario, if one of the services throws an exception (i.e. failed to reset), we should issue cancellations on the others before discarding the poolable context.

Basically it's about IResettableService being a public abstraction, so we're not sure exactly where it will end up. If you have strong feelings against this (especially if you see some pit of failure) we can revert.

@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview8 Jul 29, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview8, 3.0.0 Nov 11, 2019
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants