Skip to content

Commit ff61b60

Browse files
committed
made dispose two-phase
1 parent e001a79 commit ff61b60

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ internal sealed class ImplicitSessionSource : ISessionSource
44
{
55
private readonly IDriver _driver;
66
private readonly ManualResetEventSlim _allReleased = new(false);
7-
private int _isDisposed;
7+
8+
private int _state;
89
private int _activeLeaseCount;
910

1011
internal ImplicitSessionSource(IDriver driver)
@@ -24,12 +25,12 @@ public ValueTask<ISession> OpenSession(CancellationToken cancellationToken)
2425

2526
private bool TryAcquireLease()
2627
{
27-
if (Volatile.Read(ref _isDisposed) != 0)
28+
if (Volatile.Read(ref _state) == 2)
2829
return false;
2930

3031
Interlocked.Increment(ref _activeLeaseCount);
3132

32-
if (Volatile.Read(ref _isDisposed) != 0)
33+
if (Volatile.Read(ref _state) == 2)
3334
{
3435
Interlocked.Decrement(ref _activeLeaseCount);
3536
return false;
@@ -40,20 +41,21 @@ private bool TryAcquireLease()
4041

4142
internal void ReleaseLease()
4243
{
43-
if (Interlocked.Decrement(ref _activeLeaseCount) == 0 && Volatile.Read(ref _isDisposed) != 0)
44+
if (Interlocked.Decrement(ref _activeLeaseCount) == 0 && Volatile.Read(ref _state) == 1)
4445
_allReleased.Set();
4546
}
4647

4748
public async ValueTask DisposeAsync()
4849
{
49-
if (Interlocked.CompareExchange(ref _isDisposed, 1, 0) != 0)
50+
if (Interlocked.CompareExchange(ref _state, 1, 0) != 0)
5051
return;
5152

5253
if (Volatile.Read(ref _activeLeaseCount) != 0)
5354
_allReleased.Wait();
5455

5556
try
5657
{
58+
Volatile.Write(ref _state, 2);
5759
await _driver.DisposeAsync();
5860
}
5961
finally

0 commit comments

Comments
 (0)