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

[release/6.0] SQLite: Don't modify collection while enumerating inside ReclaimLeakedConnections #27428

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;

namespace Microsoft.Data.Sqlite
Expand Down Expand Up @@ -157,17 +158,17 @@ private bool ReclaimLeakedConnections()
{
var leakedConnectionsFound = false;

List<SqliteConnectionInternal> leakedConnections;
lock (_connections)
{
foreach (var connection in _connections)
{
if (connection.Leaked)
{
Return(connection);
leakedConnections = _connections.Where(c => c.Leaked).ToList();
}

leakedConnectionsFound = true;
}
}
foreach (var connection in leakedConnections)
{
leakedConnectionsFound = true;

Return(connection);
}

return leakedConnectionsFound;
Expand Down
21 changes: 21 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteConnectionFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Data;
using System.IO;
using System.Runtime.CompilerServices;
using SQLitePCL;
using Xunit;

Expand Down Expand Up @@ -218,6 +219,26 @@ public void EnableExtensions_doesnt_bleed_across_connections()
Assert.Equal(disabledMessage, ex.Message);
}

[Fact]
public void Clear_works_when_connection_leaked()
{
using var connection = new SqliteConnection(ConnectionString);
connection.Open();

LeakConnection();
GC.Collect();

SqliteConnection.ClearPool(connection);

[MethodImpl(MethodImplOptions.NoInlining)]
static void LeakConnection()
{
// Don't add using!
var connection = new SqliteConnection(ConnectionString);
connection.Open();
}
}

public void Dispose()
{
SqliteConnection.ClearAllPools();
Expand Down