Skip to content

CancellationToken not responsive #140

@groege

Description

@groege

When cancelling SendQueryAsync with a CancellationToken, there will be no immediate TaskCancelledException thrown

I'm using this workaround right now:

        private static Task<GraphQLResponse> SendQueryAsyncX(this GraphQLHttpClient client, GraphQLRequest request, CancellationToken ct)
        {
            TaskCompletionSource<GraphQLResponse> tcs = new TaskCompletionSource<GraphQLResponse>();
            ct.Register(() => tcs.TrySetCanceled());

            client.SendQueryAsync(request, ct).ContinueWith(result =>
            {
                if (result.IsFaulted)
                {
                    tcs.TrySetException(result.Exception);
                }
                else if (result.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    tcs.TrySetResult(result.Result);
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
            return tcs.Task;
        }

When a CancellationToken is cancelled, the Task should complete without any time lag to allow responsiveness out of the box.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions