Skip to content

Commit ae74c09

Browse files
YdbConnection.OpenAsync throws OperationCancelException when the CancellationToken is cancelled. (#501)
1 parent a8563bb commit ae74c09

File tree

6 files changed

+15
-28
lines changed

6 files changed

+15
-28
lines changed

src/Ydb.Sdk/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- ADO.NET: `YdbConnection.OpenAsync` throws OperationCancelException when the `CancellationToken` is cancelled.
12
- Feat ADO.NET: decimal type with arbitrary precision/scale ([#498](https://github.com/ydb-platform/ydb-dotnet-sdk/issues/498)).
23
- Fixed bug: interval value parsing in microseconds and double instead of ticks ([#497](https://github.com/ydb-platform/ydb-dotnet-sdk/issues/497)).
34
- ADO.NET: Changed `IBulkUpsertImporter.AddRowAsync` signature: `object?[] row``params object[]`.

src/Ydb.Sdk/src/Ado/Session/PoolingSessionSource.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,11 @@ private async ValueTask<ISession> RentAsync(CancellationToken cancellationToken)
126126
continue;
127127
}
128128

129-
await using var _ = finalToken.Register(
130-
() => waiterTcs.TrySetCanceled(),
131-
useSynchronizationContext: false
129+
await using var _ = finalToken.Register(() => waiterTcs.TrySetException(
130+
new YdbException($"The connection pool has been exhausted, either raise 'MaxSessionPool' " +
131+
$"(currently {_maxSessionSize}) or 'CreateSessionTimeout' " +
132+
$"(currently {_createSessionTimeout} seconds) in your connection string.")
133+
), useSynchronizationContext: false
132134
);
133135
await using var disposeRegistration = _disposeCts.Token.Register(
134136
() => waiterTcs.TrySetException(new YdbException("The session source has been shut down.")),

src/Ydb.Sdk/src/Ado/YdbConnection.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,8 @@ public override void ChangeDatabase(string databaseName)
110110
public override async Task OpenAsync(CancellationToken cancellationToken)
111111
{
112112
ThrowIfConnectionOpen();
113-
try
114-
{
115-
Session = await PoolManager.GetSession(ConnectionStringBuilder, cancellationToken);
116-
}
117-
catch (OperationCanceledException e)
118-
{
119-
throw new YdbException(StatusCode.ClientTransportTimeout,
120-
$"The connection pool has been exhausted, either raise 'MaxSessionPool' " +
121-
$"(currently {ConnectionStringBuilder.MaxSessionPool}) or 'CreateSessionTimeout' " +
122-
$"(currently {ConnectionStringBuilder.CreateSessionTimeout} seconds) in your connection string.", e
123-
);
124-
}
113+
114+
Session = await PoolManager.GetSession(ConnectionStringBuilder, cancellationToken);
125115

126116
OnStateChange(ClosedToOpenEventArgs);
127117

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Specification.Tests/YdbConnectionTests.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,4 @@ public override void ServerVersion_returns_value()
2828
{
2929
base.ServerVersion_returns_value();
3030
}
31-
32-
public override async Task OpenAsync_is_canceled()
33-
{
34-
await using var connection = CreateConnection();
35-
connection.ConnectionString = ConnectionString;
36-
var task = connection.OpenAsync(CanceledToken);
37-
await Assert.ThrowsAnyAsync<YdbException>(() => task);
38-
Assert.True(task.IsFaulted);
39-
}
4031
}

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/Session/PoolingSessionSourceMockTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ public async Task StressTest_HighContention_OpenClose()
295295
catch (OperationCanceledException)
296296
{
297297
}
298+
catch (YdbException)
299+
{
300+
}
298301
}, cts.Token));
299302
}
300303

@@ -316,7 +319,9 @@ public async Task Get_Session_From_Exhausted_Pool()
316319
var cts = new CancellationTokenSource();
317320
cts.CancelAfter(500);
318321

319-
await Assert.ThrowsAsync<TaskCanceledException>(async () => await sessionSource.OpenSession(cts.Token));
322+
Assert.Equal("The connection pool has been exhausted, either raise 'MaxSessionPool' (currently 1) " +
323+
"or 'CreateSessionTimeout' (currently 5 seconds) in your connection string.",
324+
(await Assert.ThrowsAsync<YdbException>(async () => await sessionSource.OpenSession(cts.Token))).Message);
320325
session.Close();
321326

322327
Assert.Equal(1, mockFactory.NumSession);

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/YdbConnectionTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ public async Task OpenAsync_WhenCancelTokenIsCanceled_ThrowYdbException()
201201
connection.ConnectionString = ConnectionString + ";MinSessionPool=1";
202202
using var cts = new CancellationTokenSource();
203203
cts.Cancel();
204-
Assert.Equal("The connection pool has been exhausted, either raise 'MaxSessionPool' (currently 10) " +
205-
"or 'CreateSessionTimeout' (currently 5 seconds) in your connection string.",
206-
(await Assert.ThrowsAsync<YdbException>(async () => await connection.OpenAsync(cts.Token))).Message);
204+
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await connection.OpenAsync(cts.Token));
207205
Assert.Equal(ConnectionState.Closed, connection.State);
208206
}
209207

0 commit comments

Comments
 (0)