Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task CloseConnectionAsync(Connection connection, CancellationToken
{
try
{
await _client.CloseConnectionAsync(ToProto(connection), cancellationToken: cancellationToken);
await _client.CloseConnectionAsync(ToProto(connection), cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RpcException exception)
{
Expand All @@ -184,7 +184,7 @@ public async Task CloseConnectionAsync(Connection connection, CancellationToken
{
Connection = ToProto(connection),
Mutations = mutations,
}, cancellationToken: cancellationToken);
}, cancellationToken: cancellationToken).ConfigureAwait(false);
return response.CommitTimestamp == null ? null : response;
}
catch (RpcException exception)
Expand Down Expand Up @@ -223,13 +223,13 @@ public async Task<Rows> ExecuteAsync(Connection connection, ExecuteSqlRequest st
{
if (_useStreamingRows)
{
return await ExecuteStreamingAsync(connection, statement, cancellationToken);
return await ExecuteStreamingAsync(connection, statement, cancellationToken).ConfigureAwait(false);
}
var rows = await _client.ExecuteAsync(new ExecuteRequest
{
Connection = ToProto(connection),
ExecuteSqlRequest = statement,
}, cancellationToken: cancellationToken);
}, cancellationToken: cancellationToken).ConfigureAwait(false);
return FromProto(connection, rows);
}
catch (RpcException exception)
Expand All @@ -246,7 +246,7 @@ private async Task<StreamingRows> ExecuteStreamingAsync(Connection connection, E
Connection = ToProto(connection),
ExecuteSqlRequest = statement,
}));
return await StreamingRows.CreateAsync(connection, stream, cancellationToken);
return await StreamingRows.CreateAsync(connection, stream, cancellationToken).ConfigureAwait(false);
}

public long[] ExecuteBatch(Connection connection, ExecuteBatchDmlRequest statements)
Expand Down Expand Up @@ -283,7 +283,7 @@ public async Task<long[]> ExecuteBatchAsync(Connection connection, ExecuteBatchD
{
Connection = ToProto(connection),
ExecuteBatchDmlRequest = statements,
}, cancellationToken: cancellationToken);
}, cancellationToken: cancellationToken).ConfigureAwait(false);
var result = new long[stats.ResultSets.Count];
for (var i = 0; i < result.Length; i++)
{
Expand All @@ -306,7 +306,7 @@ public async Task<long[]> ExecuteBatchAsync(Connection connection, ExecuteBatchD
{
try
{
return await _client.MetadataAsync(ToProto(rows), cancellationToken: cancellationToken);
return await _client.MetadataAsync(ToProto(rows), cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RpcException exception)
{
Expand All @@ -323,7 +323,7 @@ public async Task<long[]> ExecuteBatchAsync(Connection connection, ExecuteBatchD
{
try
{
return await _client.NextResultSetAsync(ToProto(rows), cancellationToken: cancellationToken);
return await _client.NextResultSetAsync(ToProto(rows), cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RpcException exception)
{
Expand Down Expand Up @@ -356,7 +356,7 @@ public async Task<long[]> ExecuteBatchAsync(Connection connection, ExecuteBatchD
Rows = ToProto(rows),
NumRows = numRows,
Encoding = (long)encoding,
}, cancellationToken: cancellationToken);
}, cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RpcException exception)
{
Expand All @@ -373,7 +373,7 @@ public async Task CloseRowsAsync(Rows rows, CancellationToken cancellationToken
{
try
{
await _client.CloseRowsAsync(ToProto(rows), cancellationToken: cancellationToken);
await _client.CloseRowsAsync(ToProto(rows), cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RpcException exception)
{
Expand All @@ -400,7 +400,7 @@ public void BeginTransaction(Connection connection, TransactionOptions transacti
{
try
{
var response = await _client.CommitAsync(ToProto(connection), cancellationToken: cancellationToken);
var response = await _client.CommitAsync(ToProto(connection), cancellationToken: cancellationToken).ConfigureAwait(false);
return response.CommitTimestamp == null ? null : response;
}
catch (RpcException exception)
Expand All @@ -418,7 +418,7 @@ public async Task RollbackAsync(Connection connection, CancellationToken cancell
{
try
{
await _client.RollbackAsync(ToProto(connection), cancellationToken: cancellationToken);
await _client.RollbackAsync(ToProto(connection), cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (RpcException exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static StreamingRows Create(Connection connection, AsyncServerStreamingCa
public static async Task<StreamingRows> CreateAsync(Connection connection, AsyncServerStreamingCall<RowData> stream, CancellationToken cancellationToken = default)
{
var rows = new StreamingRows(connection, stream);
rows._pendingRow = await rows.NextAsync(cancellationToken);
rows._pendingRow = await rows.NextAsync(cancellationToken).ConfigureAwait(false);
return rows;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ private void MarkDone()
}
try
{
var hasNext = await _stream.ResponseStream.MoveNext(cancellationToken);
var hasNext = await _stream.ResponseStream.MoveNext(cancellationToken).ConfigureAwait(false);
if (!hasNext)
{
MarkDone();
Expand Down Expand Up @@ -180,7 +180,7 @@ public override async Task<bool> NextResultSetAsync(CancellationToken cancellati
return false;
}
// Read data until we reach the next result set.
await ReadUntilEndAsync(cancellationToken);
await ReadUntilEndAsync(cancellationToken).ConfigureAwait(false);

return HasNextResultSet();
}
Expand All @@ -206,7 +206,7 @@ private void ReadUntilEnd()
private async Task ReadUntilEndAsync(CancellationToken cancellationToken)
{
// Read the remaining rows in the current result set.
while (!_pendingNextResultSetCall && await NextAsync(cancellationToken) != null)
while (!_pendingNextResultSetCall && await NextAsync(cancellationToken).ConfigureAwait(false) != null)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" PrivateAssets="All" />
<PackageReference Include="Alpha.Google.Cloud.SpannerLib.GrpcServer" Version="1.0.0-alpha.20251027150914" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<Version>1.0.0-alpha.20251027150914</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="binaries/any/*" Pack="true" PackagePath="runtimes/any/native/" />
<None Include="binaries/linux-x64/*" Pack="true" PackagePath="runtimes/linux-x64/native/" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Task<long[]> ExecuteBatchAsync(Connection connection, ExecuteBatchDmlRequ

public async Task<ResultSetMetadata?> MetadataAsync(Rows rows, CancellationToken cancellationToken = default)
{
return await Task.Run(() => Metadata(rows), cancellationToken);
return await Task.Run(() => Metadata(rows), cancellationToken).ConfigureAwait(false);
}
Comment on lines 175 to 178

Choose a reason for hiding this comment

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

medium

This change to use ConfigureAwait(false) is correct and follows best practices for library code.

For consistency and to fully prevent synchronization context capturing, consider applying a similar async/await pattern to other asynchronous methods in this class that currently return Task.Run(...) directly. These methods are not flagged by ConfigureAwaitChecker.Analyzer because they don't use the await keyword, but they will still capture the synchronization context when awaited by a caller.

For example, WriteMutationsAsync could be changed to:

public async Task<CommitResponse?> WriteMutationsAsync(Connection connection,
    BatchWriteRequest.Types.MutationGroup mutations, CancellationToken cancellationToken = default)
{
    return await Task.Run(() => WriteMutations(connection, mutations), cancellationToken).ConfigureAwait(false);
}

This pattern should be applied to the following methods as well:

  • ExecuteAsync
  • ExecuteBatchAsync
  • CloseRowsAsync
  • CommitAsync
  • RollbackAsync


public ResultSetMetadata? NextResultSet(Rows rows)
Expand All @@ -185,7 +185,7 @@ public Task<long[]> ExecuteBatchAsync(Connection connection, ExecuteBatchDmlRequ

public async Task<ResultSetMetadata?> NextResultSetAsync(Rows rows, CancellationToken cancellationToken = default)
{
return await Task.Run(() => NextResultSet(rows), cancellationToken);
return await Task.Run(() => NextResultSet(rows), cancellationToken).ConfigureAwait(false);
}

public ResultSetStats? Stats(Rows rows)
Expand All @@ -202,7 +202,7 @@ public Task<long[]> ExecuteBatchAsync(Connection connection, ExecuteBatchDmlRequ

public async Task<ListValue?> NextAsync(Rows rows, int numRows, ISpannerLib.RowEncoding encoding, CancellationToken cancellationToken = default)
{
return await Task.Run(() => Next(rows, numRows, encoding), cancellationToken);
return await Task.Run(() => Next(rows, numRows, encoding), cancellationToken).ConfigureAwait(false);
}

public void CloseRows(Rows rows)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" PrivateAssets="All" />
<PackageReference Include="Alpha.Google.Cloud.SpannerLib.Native" Version="1.0.0-alpha.20251027150914" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

using System;
using System.Data;
using System.Text;

namespace Google.Cloud.SpannerLib.Native;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<Version>1.0.0-alpha.20251027150914</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="libraries/any/*" Pack="true" PackagePath="runtimes/any/native/" />
<None Include="libraries/linux-x64/*" Pack="true" PackagePath="runtimes/linux-x64/native/" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected virtual async ValueTask DisposeAsyncCore()
{
if (Id > 0)
{
await CloseLibObjectAsync();
await CloseLibObjectAsync().ConfigureAwait(false);
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,6 @@ protected override void CloseLibObject()

protected override async ValueTask CloseLibObjectAsync()
{
await Spanner.CloseConnectionAsync(this);
await Spanner.CloseConnectionAsync(this).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Rows(Connection connection, long id, bool initMetadata = true) : base(con
/// <returns>The next data row or null if there are no more data</returns>
public virtual async Task<ListValue?> NextAsync(CancellationToken cancellationToken = default)
{
return await Spanner.NextAsync(this, 1, ISpannerLib.RowEncoding.Proto, cancellationToken);
return await Spanner.NextAsync(this, 1, ISpannerLib.RowEncoding.Proto, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -112,7 +112,7 @@ public virtual bool NextResultSet()
/// <returns>True if there was another result set, and false otherwise</returns>
public virtual async Task<bool> NextResultSetAsync(CancellationToken cancellationToken = default)
{
return NextResultSet(await Spanner.NextResultSetAsync(this, cancellationToken));
return NextResultSet(await Spanner.NextResultSetAsync(this, cancellationToken).ConfigureAwait(false));
}

private bool NextResultSet(ResultSetMetadata? metadata)
Expand All @@ -136,7 +136,7 @@ protected override void CloseLibObject()

protected override async ValueTask CloseLibObjectAsync()
{
await Spanner.CloseRowsAsync(this);
await Spanner.CloseRowsAsync(this).ConfigureAwait(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" PrivateAssets="All" />
<PackageReference Include="Google.Cloud.Spanner.V1" Version="5.7.0" />
</ItemGroup>

Expand Down
Loading