Skip to content

Commit 401e7d2

Browse files
committed
try
1 parent 6852546 commit 401e7d2

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ namespace Ydb.Sdk.Ado.Session;
22

33
internal sealed class ImplicitSessionSource : ISessionSource
44
{
5-
private enum State { Open = 0, Closing = 1, Closed = 2 }
6-
75
private readonly IDriver _driver;
86
private readonly ManualResetEventSlim _allReleased = new(false);
97

@@ -27,12 +25,14 @@ public ValueTask<ISession> OpenSession(CancellationToken cancellationToken)
2725

2826
private bool TryAcquireLease()
2927
{
30-
if (Volatile.Read(ref _state) == (int)State.Closed)
28+
if (Volatile.Read(ref _state) == 2)
3129
return false;
3230

33-
Interlocked.Increment(ref _activeLeaseCount);
31+
var newCount = Interlocked.Increment(ref _activeLeaseCount);
32+
33+
var state = Volatile.Read(ref _state);
3434

35-
if (Volatile.Read(ref _state) == (int)State.Closed)
35+
if (state == 2 || (state == 1 && newCount == 1))
3636
{
3737
Interlocked.Decrement(ref _activeLeaseCount);
3838
return false;
@@ -44,29 +44,23 @@ private bool TryAcquireLease()
4444
internal void ReleaseLease()
4545
{
4646
if (Interlocked.Decrement(ref _activeLeaseCount) == 0 &&
47-
Volatile.Read(ref _state) != (int)State.Open)
47+
Volatile.Read(ref _state) != 0)
4848
{
4949
_allReleased.Set();
5050
}
5151
}
5252

5353
public async ValueTask DisposeAsync()
5454
{
55-
var prev = Interlocked.CompareExchange(ref _state, (int)State.Closing, (int)State.Open);
56-
switch (prev)
57-
{
58-
case (int)State.Closed:
59-
return;
60-
case (int)State.Closing:
61-
break;
62-
}
55+
if (Interlocked.CompareExchange(ref _state, 1, 0) != 0)
56+
return;
6357

6458
if (Volatile.Read(ref _activeLeaseCount) != 0)
6559
_allReleased.Wait();
6660

6761
try
6862
{
69-
Volatile.Write(ref _state, (int)State.Closed);
63+
Volatile.Write(ref _state, 2);
7064
await _driver.DisposeAsync();
7165
}
7266
finally

0 commit comments

Comments
 (0)