0

When you create a new cosmos DB some code samples are shown in Azure. Most of the code works. However I have this single function that just freezes/hangs when Executed. Any ideas how to solve or troubleshoot?

Code:

public async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate, string collection)
{
    IDocumentQuery<T> query = Client.CreateDocumentQuery<T>(
        UriFactory.CreateDocumentCollectionUri(DatabaseId, collection),
        new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
        .Where(predicate)
        .AsDocumentQuery();

    List<T> results = new List<T>();
    while (query.HasMoreResults)
    {
        try
        {
            var result = await query.ExecuteNextAsync<T>();
            results.AddRange(result);
        }
        catch(Exception ex)
        {

        }
    }

    return results;
}

Calling the function:

return await ItemsRepo.GetItemsAsync(p=>p.RunId == runId, "files-and-folders-with-unique-permissions");

Finally the windows app:

var items = _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text).Result;

Full code:

Repository:

public async Task<IEnumerable<Item>> GetFilesAndFoldersWithUniquePermissions(string runId)
    {
        var collectionLink = UriFactory.CreateDocumentCollectionUri("kk-governance-reporting", "files-and-folders-with-unique-permissions");
        return await ItemsRepo.GetItemsAsync(p=>p.RunId == runId, "files-and-folders-with-unique-permissions");
    }

App:

private void BtnGenerateReport_Click(object sender, EventArgs e)
    {
        var items = _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text).Result;

        string json = JsonConvert.SerializeObject(items.ToArray());
        json = JToken.Parse(json).ToString(Formatting.Indented);

        //write string to file
        System.IO.File.WriteAllText(@"c:\temp\unique-files-and-folders.txt", json);

        MessageBox.Show("Completed");
    }
Thomas Segato
  • 4,567
  • 11
  • 55
  • 104
  • 1
    The first thing I would do is to ensure that there is no exception thrown in this empty catch block and also that this happens with both the emulator and a real cosmosdb service in the cloud. – Nick Chapsas Jan 03 '19 at 16:06
  • I added the try catch, and no exception is thrown. It just hangs. Its kind of funny that all other methods towards cosmos db works. I add and update documents in other functions. Its only tested towards the azure service. – Thomas Segato Jan 03 '19 at 16:49
  • 1
    Is your caller async too and the call to GetItemsAsync is being awaited? – Matias Quaranta Jan 03 '19 at 17:02
  • Nopes it a classic windows forms app. But all other calls works as expected.I updated post with final call from Win App. – Thomas Segato Jan 03 '19 at 19:09
  • @ThomasSegato Mixing async-await and `.Result` blocking calls can cause deadlocks. – Nkosi Jan 03 '19 at 20:13
  • @Nkosi sounds correct, would explain the freezing. I have added the two functions to the main post, what would be the correct approach to do? – Thomas Segato Jan 03 '19 at 20:23

1 Answers1

0

Mixing async-await and .Result blocking calls can cause deadlocks.

In this case, event handlers allow for async void.

Reference Async/Await - Best Practices in Asynchronous Programming

So I suggest you refactor the App to await in the event handler

private async void BtnGenerateReport_Click(object sender, EventArgs e) {
    var items = await _db.GetFilesAndFoldersWithUniquePermissions(txtRunId.Text); //<-- 

    string json = JsonConvert.SerializeObject(items.ToArray());
    json = JToken.Parse(json).ToString(Formatting.Indented);

    //write string to file
    System.IO.File.WriteAllText(@"c:\temp\unique-files-and-folders.txt", json);

    MessageBox.Show("Completed");
}

To allow the code to flow as expected.

Nkosi
  • 235,767
  • 35
  • 427
  • 472