From 95bdcc7ad1e3146754e7510a7a69bdb9b27266f2 Mon Sep 17 00:00:00 2001 From: David Martos Date: Wed, 19 Feb 2025 10:49:02 +0100 Subject: [PATCH 1/4] don't select closed conn in pool --- lib/src/pool/pool_impl.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/pool/pool_impl.dart b/lib/src/pool/pool_impl.dart index c986e34..f1fa385 100644 --- a/lib/src/pool/pool_impl.dart +++ b/lib/src/pool/pool_impl.dart @@ -177,7 +177,7 @@ class PoolImplementation implements Pool { Future<_PoolConnection> _selectOrCreate( Endpoint endpoint, ResolvedConnectionSettings settings) async { final oldc = - _connections.firstWhereOrNull((c) => c._mayReuse(endpoint, settings)); + _connections.firstWhereOrNull((c) => c._mayReuse(endpoint, settings) && c.isOpen); if (oldc != null) { // NOTE: It is important to update the _isInUse flag here, otherwise // race conditions may create conflicts. From 2984236fb6100e574f947ee8e15945cc654b7ecd Mon Sep 17 00:00:00 2001 From: David Martos Date: Fri, 21 Feb 2025 11:28:48 +0100 Subject: [PATCH 2/4] move to _mayReuse --- lib/src/pool/pool_impl.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/pool/pool_impl.dart b/lib/src/pool/pool_impl.dart index f1fa385..e70ee0a 100644 --- a/lib/src/pool/pool_impl.dart +++ b/lib/src/pool/pool_impl.dart @@ -177,7 +177,7 @@ class PoolImplementation implements Pool { Future<_PoolConnection> _selectOrCreate( Endpoint endpoint, ResolvedConnectionSettings settings) async { final oldc = - _connections.firstWhereOrNull((c) => c._mayReuse(endpoint, settings) && c.isOpen); + _connections.firstWhereOrNull((c) => c._mayReuse(endpoint, settings)); if (oldc != null) { // NOTE: It is important to update the _isInUse flag here, otherwise // race conditions may create conflicts. @@ -232,7 +232,7 @@ class _PoolConnection implements Connection { this._pool, this._endpoint, this._connectionSettings, this._connection); bool _mayReuse(Endpoint endpoint, ResolvedConnectionSettings settings) { - if (_isInUse || endpoint != _endpoint || _isExpired()) { + if (_isInUse || endpoint != _endpoint || _isExpired() || !isOpen) { return false; } if (!_connectionSettings.isMatchingConnection(settings)) { From 7b5c1e1c5e8bb2f60e0799a0ee3cfe7ddea0210a Mon Sep 17 00:00:00 2001 From: David Martos Date: Fri, 21 Feb 2025 11:29:43 +0100 Subject: [PATCH 3/4] changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ceec3ff..f3a2d1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Don't reuse pool connections that are closed/closing. + ## 3.5.3 - New typed exceptions: `UniqueViolationException`, `ForeignKeyViolationException`. [#416](https://github.com/isoos/postgresql-dart/pull/416) by [hurrba](https://github.com/hurrba) From 4e39bc78920d9b165d4ce76ef45121f320d87e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20So=C3=B3s?= Date: Fri, 21 Feb 2025 15:12:56 +0100 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3a2d1e..6a23eb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,9 @@ # Changelog -## Unreleased - -- Don't reuse pool connections that are closed/closing. - ## 3.5.3 - New typed exceptions: `UniqueViolationException`, `ForeignKeyViolationException`. [#416](https://github.com/isoos/postgresql-dart/pull/416) by [hurrba](https://github.com/hurrba) +- Fix: don't reuse pool connections that are closed/closing. [#417](https://github.com/isoos/postgresql-dart/pull/417) by [davidmartos96](https://github.com/davidmartos96) ## 3.5.2