Skip to content

Commit 6852546

Browse files
committed
tried to make a “two-phase”
1 parent dd0ba06 commit 6852546

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

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

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

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

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

2628
private bool TryAcquireLease()
2729
{
28-
if (Volatile.Read(ref _state) == 2)
30+
if (Volatile.Read(ref _state) == (int)State.Closed)
2931
return false;
3032

3133
Interlocked.Increment(ref _activeLeaseCount);
3234

33-
if (Volatile.Read(ref _state) == 2)
35+
if (Volatile.Read(ref _state) == (int)State.Closed)
3436
{
3537
Interlocked.Decrement(ref _activeLeaseCount);
3638
return false;
@@ -41,21 +43,30 @@ private bool TryAcquireLease()
4143

4244
internal void ReleaseLease()
4345
{
44-
if (Interlocked.Decrement(ref _activeLeaseCount) == 0 && Volatile.Read(ref _state) == 1)
46+
if (Interlocked.Decrement(ref _activeLeaseCount) == 0 &&
47+
Volatile.Read(ref _state) != (int)State.Open)
48+
{
4549
_allReleased.Set();
50+
}
4651
}
4752

4853
public async ValueTask DisposeAsync()
4954
{
50-
if (Interlocked.CompareExchange(ref _state, 1, 0) != 0)
51-
return;
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+
}
5263

5364
if (Volatile.Read(ref _activeLeaseCount) != 0)
5465
_allReleased.Wait();
5566

5667
try
5768
{
58-
Volatile.Write(ref _state, 2);
69+
Volatile.Write(ref _state, (int)State.Closed);
5970
await _driver.DisposeAsync();
6071
}
6172
finally

0 commit comments

Comments
 (0)