Skip to content

Some cleanup work around cancellation tokens - #2443

Merged
roji merged 1 commit into
npgsql:devfrom
roji:CancellationTokens
Aug 26, 2019
Merged

roji merged 1 commit into
npgsql:devfrom
roji:CancellationTokens

Conversation

@roji

@roji roji commented Apr 18, 2019

Copy link
Copy Markdown
Member
  • Make sure async methods returned cancelled tasks rather than throw exceptions when passed a cancelled token.
  • Added cancellation token parameter to NpgsqlDataReader.Get{Stream,TextReader}Async(). This is a binary breaking change.

Closes #2417

@austindrenski austindrenski left a comment

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.

Just one question, otherwise looks good.

Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlLargeObjectManager.cs Resolved
@worldleaderpretend

Copy link
Copy Markdown

Hello, I added some sample code related to this work item in #2201

@roji
roji force-pushed the CancellationTokens branch 2 times, most recently from 8225cb9 to 5512d8e Compare June 26, 2019 15:29
@roji

roji commented Jun 26, 2019

Copy link
Copy Markdown
Member Author

@YohDeadfall I've (finally) updated this PR, can you please give it a quick review so we can merge?

@roji

roji commented Jun 26, 2019

Copy link
Copy Markdown
Member Author

@worldleaderpretend I'll take a look at the wait issue you raised separately, as we may want to patch 4.0.x.

@roji

roji commented Jun 26, 2019

Copy link
Copy Markdown
Member Author

@worldleaderpretend can you please open a new issue with your repro? Checking the cancellation token in the method start will take care of it, but it does actually leave a race condition if cancellation is triggered slightly after that.

@worldleaderpretend

Copy link
Copy Markdown

Sure thing, here ya go #2506. Thanks for the fast response!

Comment thread src/Npgsql/NpgsqlConnection.cs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a tiny semantical difference here though.

Task.FromCanceled will create a Task.Status == Canceled with TaskCanceledException.
While throwing from within an ordinary async method would transition the task to Faulted with an OperationCanceledException (which is the base class for TaskCanceledException) because the task wasn't started with the passed in token.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I may be misunderstanding, but consider the following:

[Test]
public void ThrowInAsyncAfterYield()
{
    var t = Foo(new CancellationToken(canceled: true));
    Assert.That(async () => await t, Throws.Exception.TypeOf<TaskCanceledException>());
    Assert.That(t.Status, Is.EqualTo(TaskStatus.Canceled));

    static async Task Foo(CancellationToken token)
    {
        await Task.Yield();
        token.ThrowIfCancellationRequested();
    }
}

[Test]
public void ReturnTaskCanceledInNonAsync()
{
    var t = Foo(new CancellationToken(canceled: true));
    Assert.That(async () => await t, Throws.Exception.TypeOf<TaskCanceledException>());
    Assert.That(t.Status, Is.EqualTo(TaskStatus.Canceled));

    static Task Foo(CancellationToken token)
        => token.IsCancellationRequested
            ? Task.FromCanceled(token)
            : Task.CompletedTask;
}

ThrowInAsyncAfterYield's task is ultimately Canceled, not Faulted.

You may also want to look at dotnet/corefx#38453 and the conversation on it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@NinoFloris anything more to say here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No sorry, have been meaning to reply. But it's interesting it works this way. Does that TaskCanceled exception also include that cancellationtoken from the arguments? In that case I'm really curious what magic the compiler does there.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You're asking where (or whether) the token is stored on cancelled tasks, right? It could internally be a union with the exception that needs to be recorded on faulted tasks, for example...

Otherwise token.ThrowIfCancellationRequested() simply goes throw new OperationCanceledException(token), which gets caught and results in the task being Cancelled, as far as I understand it.

Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlLargeObjectManager.cs Resolved
@roji
roji force-pushed the CancellationTokens branch 2 times, most recently from 759096f to e4c0005 Compare August 19, 2019 16:34
@roji

roji commented Aug 19, 2019

Copy link
Copy Markdown
Member Author

@NinoFloris @YohDeadfall I've addressed your comments, can you please take another look? It would be good to merge this.

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@roji
roji force-pushed the CancellationTokens branch from e4c0005 to 90e580c Compare August 26, 2019 08:37
@roji
roji requested a review from YohDeadfall August 26, 2019 08:37
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
@roji
roji force-pushed the CancellationTokens branch from 90e580c to 95b311e Compare August 26, 2019 10:45
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
* Make sure async methods returned cancelled tasks rather than throw
  exceptions when passed a cancelled token.
* Added cancellation token parameter to
  NpgsqlDataReader.Get{Stream,TextReader}Async(). This is a binary
  breaking change.
* Added "default" to some cancellation token parameters which were
  missing it.

Closes npgsql#2417
@roji
roji force-pushed the CancellationTokens branch from 95b311e to 817a536 Compare August 26, 2019 11:04
@roji
roji merged commit ecdc5f8 into npgsql:dev Aug 26, 2019
@roji
roji deleted the CancellationTokens branch August 26, 2019 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make sure all async methods return cancelled tasks correctly

5 participants