QueryAsync causes program to crash

Hi.

Using .Net SDK 2.4.8 and Couchbase version 4.5.0-2601 Enterprise Edition (build-2601)
I have Couchbase Enterprise Edition running through Docker on my laptop. I have a small program where I query the a bucket to retrieve an ID. When I use ‘Query<>’ it works and returns the correct data but when I use ‘QueryAsync<>’ the program crashes on me without any explanation. Code is below:

Program.cs

using Couchbase;
using Couchbase.Configuration.Client;
using System;
using System.Collections.Generic;

namespace CouchbaseQuery
{
class Program
{
    static void Main(string[] args)
    {
        var config = new ClientConfiguration()
        {
            Servers = new List<Uri>
            {
                new Uri("http://localhost:8091/")
            },
            BucketConfigs = new Dictionary<string, BucketConfiguration>
            {
                {
                    "FeatureValue", new BucketConfiguration
                    {
                        BucketName = "FeatureValue",
                        UseSsl = false,
                        Password = "",
                        DefaultOperationLifespan = 2000,
                        PoolConfiguration = new PoolConfiguration
                        {
                            MaxSize = 10,
                            MinSize = 5,
                            SendTimeout = 12000
                        }
                    }
                }}
        };
        ClusterHelper.Initialize(config);
        new Couchbase().DeleteFeature().ConfigureAwait(false);        
    }
}

}

Couchbase.cs

  using Couchbase;
using System;
using System.Threading.Tasks;
using Couchbase.N1QL;

namespace CouchbaseQuery
{
    public class Couchbase
    {
        public async Task DeleteFeature()
        {
            var bucket = ClusterHelper.GetBucket("FeatureValue");
            using (bucket)
            {
                var featureName = "TestFeatureNamee5ae96a8-2803-495a-b054-ade239890cf6";

                var q = new QueryRequest()
                    .Statement("SELECT meta(`FeatureValue`).id FROM `FeatureValue` WHERE featureName = '$1' ")
                    .AddPositionalParameter(featureName);
                var queryString =
                    $"SELECT meta(`FeatureValue`).id FROM `FeatureValue` WHERE featureName = '{featureName}' ";
                    
                    try
                    {
                        var result = await bucket.QueryAsync<dynamic>(q).ConfigureAwait(false);
                        foreach (var row in result.Rows)
                        {
                            Console.WriteLine(row);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                                          
            }
        }
    }
}

In the couchbase.cs class I have tried using both a query string and a query result. Both give same problem.

@kevin.dawson

At first glance, I think your problem might be that you’re calling an async function from a synchronous Main without waiting for it to complete. So your Main function is returning and the program closing before the async operations in the worker thread pool are done.

Try one of two things:

  1. Add .Wait() to the end of the async call. Note that this is not recommended practice in production due to deadlock issues, but should work here.
new Couchbase().DeleteFeature().Wait();
  1. If you’ve upgraded to VS2017.3, you can use the new language feature in C# 7.1 for an async Main function. C# 7 Series, Part 2: Async Main | Microsoft Learn

@btburnett3

This worked for the console application however I used this in order to just illustrate the problem I am having. The actual code it applies to would end up in production ( However I can’t display that code here), which you mentioned using solution 1 isn’t recommended. The method that would call the async DeleteFeature method is also an async method. I have even moved the code into the calling method to see if it would make any difference but currently the code just hangs on the QueryAsync<> call.

@kevin.dawson

This sounds like a deadlock issue, but it’s hard to be certain. Could you provide the code that’s still having the problem that’s fully async? Also, what’s calling your async method? Is it an MVC action or something different?

Sorry for late repy, I implemented the .Wait() solution as what I am working on is a POC and if problems arise from this solution will be looked into at a later date.

Thank you for your help @btburnett3