From 235ff8e7418b0f3e24cbbef58e18dd589dc192dc Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Tue, 17 Jan 2023 13:55:20 +0000 Subject: [PATCH] Move taking of lock earlier to avoid race in PruneCallback Fixes #29952 --- .../SqliteConnectionFactory.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteConnectionFactory.cs b/src/Microsoft.Data.Sqlite.Core/SqliteConnectionFactory.cs index 1103aaf2fe8..1587f0cf117 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteConnectionFactory.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteConnectionFactory.cs @@ -138,20 +138,21 @@ private void PruneCallback(object? _) } } - for (var i = _idlePoolGroups.Count - 1; i >= 0; i--) + _lock.EnterWriteLock(); + + try { - var poolGroup = _idlePoolGroups[i]; - if (!poolGroup.Clear()) + for (var i = _idlePoolGroups.Count - 1; i >= 0; i--) { - _idlePoolGroups.Remove(poolGroup); - } - } + var poolGroup = _idlePoolGroups[i]; - _lock.EnterWriteLock(); + if (!poolGroup.Clear()) + { + _idlePoolGroups.Remove(poolGroup); + } + } - try - { var activePoolGroups = new Dictionary(); foreach (var entry in _poolGroups) {