Skip to content

Commit 5b5d86e

Browse files
committed
Dispose semaphore
1 parent 858e407 commit 5b5d86e

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/Npgsql/Internal/NpgsqlConnector.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,14 +1889,21 @@ internal void Close()
18891889
Close(async: false).GetAwaiter().GetResult();
18901890
}
18911891

1892-
SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
1892+
readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
18931893

18941894
internal async Task Close(bool async, CancellationToken cancellationToken = default)
18951895
{
1896-
if (async)
1897-
await _semaphore.WaitAsync(cancellationToken);
1898-
else
1899-
_semaphore.Wait(cancellationToken);
1896+
try
1897+
{
1898+
if (async)
1899+
await _semaphore.WaitAsync(cancellationToken);
1900+
else
1901+
_semaphore.Wait(cancellationToken);
1902+
}
1903+
catch (ObjectDisposedException)
1904+
{
1905+
return;
1906+
}
19001907

19011908
try
19021909
{
@@ -1932,7 +1939,15 @@ internal async Task Close(bool async, CancellationToken cancellationToken = defa
19321939
}
19331940
finally
19341941
{
1935-
_semaphore.Release();
1942+
try
1943+
{
1944+
_semaphore.Release();
1945+
_semaphore.Dispose();
1946+
}
1947+
catch (ObjectDisposedException)
1948+
{
1949+
// Ignore
1950+
}
19361951
}
19371952
}
19381953

0 commit comments

Comments
 (0)