Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed potential deadlock in FbConnectionPoolManager #1076

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ public Pool(ConnectionString connectionString)

public void Dispose()
{
Item[] items;

lock (_syncRoot)
{
if (_disposed)
return;
_disposed = true;
CleanConnectionsImpl();

items = _available.ToArray();
_available.Clear();
}

CleanConnectionsImpl(items);
}

public FbConnectionInternal GetConnection(out bool createdNew)
Expand Down Expand Up @@ -103,6 +109,8 @@ public void ReleaseConnection(FbConnectionInternal connection, bool returnToAvai

public void PrunePool()
{
List<Item> release;

lock (_syncRoot)
{
CheckDisposedImpl();
Expand All @@ -117,26 +125,33 @@ public void PrunePool()
{
keep = keep.Concat(available.Except(keep).OrderByDescending(x => x.Created).Take(_connectionString.MinPoolSize - keepCount)).ToList();
}
var release = available.Except(keep).ToList();
Parallel.ForEach(release, x => x.Release());
release = available.Except(keep).ToList();
_available = new Stack<Item>(keep);
}

// Call Release() outside the lock to avoid potential deadlocks
Parallel.ForEach(release, x => x.Release());
}

public void ClearPool()
{
Item[] items;

lock (_syncRoot)
{
CheckDisposedImpl();

CleanConnectionsImpl();
items = _available.ToArray();
_available.Clear();
}

// Call Release() outside the lock to avoid potential deadlocks
CleanConnectionsImpl(items);
}

void CleanConnectionsImpl()
void CleanConnectionsImpl(Item[] items)
{
Parallel.ForEach(_available, x => x.Release());
Parallel.ForEach(items, x => x.Release());
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down