Skip to content

Solution to Issue 27 - #28

Merged
subasish-couchbase merged 2 commits into
mainfrom
Issue-27-Bad-Usage-of-ValueTask
Mar 27, 2026
Merged

subasish-couchbase merged 2 commits into
mainfrom
Issue-27-Bad-Usage-of-ValueTask

Conversation

@azaddhirajkumar

@azaddhirajkumar azaddhirajkumar commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

#27

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request provides a solution to Issue 27 by refining the synchronous retrieval of Couchbase buckets within asynchronous contexts. The core change involves adding .AsTask() to BucketAsync calls before synchronously awaiting their results, which is crucial for correctly handling ValueTask instances and preventing potential deadlocks or unexpected behavior in C# asynchronous programming. This fix is applied to both the core service collection extensions and the accompanying documentation examples, ensuring that users follow best practices for interacting with Couchbase asynchronously.

Highlights

  • Issue Resolution: Addressed and resolved Issue 27, which likely pertained to asynchronous operation handling.
  • Asynchronous Call Correction: Implemented a fix by adding .AsTask() to BucketAsync calls before synchronously awaiting their results with .GetAwaiter().GetResult(). This ensures proper handling of ValueTask instances and prevents potential deadlocks.
  • Documentation Update: Updated example code snippets in the README.md to reflect the corrected asynchronous call pattern, providing accurate guidance for users.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the CouchbaseServiceCollectionExtensions.cs file and README.md examples to correctly handle ValueTask when synchronously waiting for asynchronous operations by adding .AsTask() before calling .GetAwaiter().GetResult(). However, the review comments point out that the underlying "sync over async" pattern, even with this fix, is generally problematic and can lead to deadlocks. The reviewer suggests providing fully asynchronous service registration methods or documenting the risks, and updating the README.md examples to demonstrate safer, fully asynchronous initialization patterns.

Comment thread Couchbase.SemanticKernel/CouchbaseServiceCollectionExtensions.cs
Comment thread README.md Outdated
{
var cluster = sp.GetRequiredService<ICluster>();
var bucket = cluster.BucketAsync("bucket-name").GetAwaiter().GetResult();
var bucket = cluster.BucketAsync("bucket-name").AsTask().GetAwaiter().GetResult();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The examples in the README demonstrate a 'sync over async' pattern by using .GetAwaiter().GetResult(). This is a dangerous practice that can lead to deadlocks. This applies to the ICluster registration on line 157 as well.

It would be better to show an example of how to perform this initialization asynchronously. For ASP.NET Core applications, this can be done in Program.cs before building the host.

For example:

// In Program.cs (Top-level statements)
var cluster = await Cluster.ConnectAsync(...);
var bucket = await cluster.BucketAsync(...);
var scope = bucket.Scope(...);

builder.Services.AddSingleton<ICluster>(cluster);
builder.Services.AddSingleton<IScope>(scope);

builder.Services.AddCouchbaseVectorStore();

This avoids blocking and is a much safer pattern for consumers of your library to follow.

Comment thread README.md Outdated
{
var cluster = sp.GetRequiredService<ICluster>();
var bucket = cluster.BucketAsync("bucket-name").GetAwaiter().GetResult();
var bucket = cluster.BucketAsync("bucket-name").AsTask().GetAwaiter().GetResult();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the previous example, this code uses the 'sync over async' anti-pattern (.GetAwaiter().GetResult()), which can cause deadlocks. This also applies to the ICluster registration on line 188.

Please consider updating the documentation to show a fully asynchronous initialization pattern, as it's much safer for users. An example of an async pattern:

// In Program.cs (Top-level statements with ASP.NET Core)
var cluster = await Cluster.ConnectAsync(...);
var bucket = await cluster.BucketAsync(...);
var scope = bucket.Scope(...);

builder.Services.AddSingleton<ICluster>(cluster);
builder.Services.AddSingleton<IScope>(scope);

builder.Services.AddCouchbaseVectorStore();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recommendation would be the same as well. We don't have to bother about the ValueTask or GetAwaiter().GetResult and making sync call within async method.

// Create Couchbase instances
var clusterOptions = new ClusterOptions
{
    ConnectionString = configuration["Couchbase:ConnectionString"],
    UserName = configuration["Couchbase:Username"],
    Password = configuration["Couchbase:Password"]
};
var cluster = await Cluster.ConnectAsync(clusterOptions);
var bucket = await cluster.BucketAsync(configuration["Couchbase:BucketName"]!);
var scope = bucket.Scope(configuration["Couchbase:ScopeName"]!);

// Using Kernel Builder
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddSingleton<ICluster>(cluster);
kernelBuilder.Services.AddSingleton<IScope>(scope);
kernelBuilder.Services.AddCouchbaseVectorStore();

var kernel = kernelBuilder.Build();

@subasish-couchbase subasish-couchbase left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to create new tickets for the changes as discussed

Comment thread README.md Outdated
{
var cluster = sp.GetRequiredService<ICluster>();
var bucket = cluster.BucketAsync("bucket-name").GetAwaiter().GetResult();
var bucket = cluster.BucketAsync("bucket-name").AsTask().GetAwaiter().GetResult();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recommendation would be the same as well. We don't have to bother about the ValueTask or GetAwaiter().GetResult and making sync call within async method.

// Create Couchbase instances
var clusterOptions = new ClusterOptions
{
    ConnectionString = configuration["Couchbase:ConnectionString"],
    UserName = configuration["Couchbase:Username"],
    Password = configuration["Couchbase:Password"]
};
var cluster = await Cluster.ConnectAsync(clusterOptions);
var bucket = await cluster.BucketAsync(configuration["Couchbase:BucketName"]!);
var scope = bucket.Scope(configuration["Couchbase:ScopeName"]!);

// Using Kernel Builder
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddSingleton<ICluster>(cluster);
kernelBuilder.Services.AddSingleton<IScope>(scope);
kernelBuilder.Services.AddCouchbaseVectorStore();

var kernel = kernelBuilder.Build();

Comment thread README.md Outdated
@@ -191,7 +191,7 @@ builder.Services.AddSingleton<ICluster>(sp =>
builder.Services.AddSingleton<IScope>(sp =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid this simply by creating an instance and registering it as singleton.

This way we don't have to bother about ValueTask or GetAwaiter().GetResult and making sync call within async method.
Another thing we need to think along is why GetBucketASync() has a return type of ValueTask...may be the .Net SDK team knows the answer

// Create Couchbase instances
var clusterOptions = new ClusterOptions
{
    ConnectionString = configuration["Couchbase:ConnectionString"],
    UserName = configuration["Couchbase:Username"],
    Password = configuration["Couchbase:Password"]
};
var cluster = await Cluster.ConnectAsync(clusterOptions);
var bucket = await cluster.BucketAsync(configuration["Couchbase:BucketName"]!);
var scope = bucket.Scope(configuration["Couchbase:ScopeName"]!);

// Using Kernel Builder
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddSingleton<ICluster>(cluster);
kernelBuilder.Services.AddSingleton<IScope>(scope);
kernelBuilder.Services.AddCouchbaseVectorStore();

var kernel = kernelBuilder.Build();

@subasish-couchbase
subasish-couchbase merged commit f66854c into main Mar 27, 2026
5 checks passed
@azaddhirajkumar
azaddhirajkumar deleted the Issue-27-Bad-Usage-of-ValueTask branch March 30, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants