When UseStreaming(true), cancellationToken doesn't cancel the query


#1

When using the streaming setting, cancellationToken doesn’t work; the query continues to run. If I comment out the UseStreaming(true), cancellationToken works fine; the long running query disappears from Query Monitor. How is this supposed to work?

.
.
var query = QueryRequest.Create(queryString);
query.UseStreaming(true);
var queryResult = this.Bucket.QueryAsync(query, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
.
.


#2

@dsullivan -

It may be a bug; note that with streaming the HTTP request stream is passed to the IQueryResult impl without closing it. When you iterate on the IQueryResult (which implements IEnumerable) each row is read and deserialized one at a time. When its false, the entire response stream is read into memory and the results converted to type T and stored in a list before passing it to the IQueryResult impl.

In both cases, the token is passed to the HTTPClient , however streaming uses SendAsync and non-streaming uses PostAsync.


#3

Is there any way to force the query to cancel using the QueryResult? or any other work around?


#4

@jmorris -
Is there any way to force the query to cancel using the QueryResult? or any other work around?


#5

@dsullivan

Sorry to jump in here, but I believe @jmorris is out today.

I would try calling Dispose on the IQueryResult if you need to cancel during your iteration of the result stream. Something like this (using will cause Dispose):

using (var result = bucket.Query<MyType>(query, cancellationToken))
{
    result.EnsureSuccess();

    foreach (var item in result) {
        if (cancellationToken.IsCancellationRequested)
        {
            break;
        }

        // Do work 
    } 

    // When streaming, errors can occur after the  data, so check again
    result.EnsureSuccess();
}

Disclaimer: Syntax may not be right, just wrote this from memory in the post.