QueryAsync is never finished

#1

I am trying to do severall calls to views asynchronously, using the async version of Query(), like this:

var bucket = ClusterHelper.GetBucket();
var query = bucket.CreateQuery(… );
var queryTask = bucket.QueryAsync(query);


var reply = queryTask.Result;

It will hang on the last line. If I add a “queryTask.Wait();”, this will never return either.

Am I missing something?

#2

@mjepson

It might be a synchronization context problem. Try adding ConfigurAwait(false) before the call to Wait or Result. This will allow the call to return on any context, instead of requiring it to be in the context where the task was started.

Brant

#3

Sadly, this doesn’t help. It still blocks endlessly on the Wait() or Result statement.

#4

@mjepson -

Either use await or use the synchronous version of bucket.Query(…). There is no benefit to synchronously waiting on a single result using the Task methods.

However, queryTask.Result should not block forever; can you create a Jira ticket? Please include an example project with steps to reproduce.

-Jeff

#5

I am doing other stuff while waiting for the result (which takes up to 2 seconds), so there is a benefit to asynchronously waiting on that single task for me. The synchronous version is working perfectly, but now the whole thing just takes too long.

I do not have a Jira account, so I cannot make a ticket I’m afraid.

#6

@mjepson -

I was able to recreate the behavior is ASP.NET/IIS only; I am guessing this is the environment you are developing with? I created a ticket: https://issues.couchbase.com/browse/NCBC-1058

I believe your forums credentials should work with Jira; it’s useful because you can be a watcher and be notified when the status on the ticket changes.

-Jeff

#7

Hey Jeff,

Yes, I am using IIS, the code is in a RESTfull API which I am writing, to see if we can use the data from our new Couchbase cluster for our analytics, as we are doing with a cube in MS SQL Server now.
Sorry for not mentioning that, hadn’t thought it would be of importance.

Thank you for creating the Jira ticket.

#8

@mjepson -

A fix has been merged into master on github. This will be packaged in the next official SDK release.

-Jeff

#9

Thank you very much, especially for the quick response.

1 Like
#10

Hi Jeff,

Sorry to say that the fix did not work for me. I have built the master from github (I can see your change is in there), but it still blocks indefinitely on the call to Wait() or Result.

  • Michael
#11

@mjepson -

Create an example project and add it to the ticket above (NCBC-1058).

-Jeff

#12

Hi Jeff,

I still cannot log on to Jira, not with the account I have at couchbase anyway.

But I can simply make a controller extending System.Web.ApiController and just do a QueryAsync on either a development or production View and then do a .Wait() or a .Result on the task and it will just block.

  • Michael
#13

I missed the part about this being a View! The bug I fixed was for N1QL queries and looks like the View API has the same bug. I’ll push a patch sometime next week.

-Jeff

#14

Oops, sorry about that. I thought bucket.CreateQuery(…) was just for view queries.

Thanks for the help though.

#15

Hi.

We seem to have the same problem here with couch views. It doesn’t appear in every QueryAsync though. It only happens in a single request.

Any idea when this patch will go in? Or maybe you could point to a specific commit so that we can apply it ourself just to verify?

#16

@sintosioannis -

Based upon what you have described it may not be the same issue. The ticket is here: https://issues.couchbase.com/browse/NCBC-1074

-Jeff

#17

@mjepson and @sintosioannis

I was able to confirm the problem you were seeing, and have created a fix for @jmorris to review:

In the meantime, there is a successful workaround for the problem. Simply change your Web API or MVC action to be asynchronous and use an “await” instead of .Result or .Wait().

This is, technically speaking, really the best way to address the problem. Using the synchronous approach of .Result or .Wait() causes a thread from the thread pool to be locked and unusable until the task completes. Using the async/await approach instead frees the thread for reuse until the task is completed, at which point a new thread will be used to complete further work.

As an example:

public async Task<ReturnType> Get() {
    var bucket = ClusterHelper.GetBucket();
    var query = bucket.CreateQuery(...);
    
    var task = bucket.QueryAsync(query);

    // Do some other work here while the query runs in the background

    var queryResult = await task;

    // Do more work with the query results

    return new ReturnType() {...};
}

Thanks,
Brant

2 Likes
#18

@btburnett3 thanks for your time. Unfortunately it didn’t solve our problem.

We had our code exactly as the example you provided and it was hanging like that. As an alternative we have switched to the sync version of the query. The weird thing is that we are using async everywhere else and it works fine.

The problem appears in this specific request.

Thanks,
Ioannis

#19

@sintosioannis

Strange that the workaround didn’t fix it. Regardless, the next version of the SDK released will have the fix in it, so that should take care of you problem. You can also pull down the latest version and build it yourself if you need it sooner.

Thanks,
Brant