test(pool): verify the overriding of `close()` to act as `putconn()`
We would like to maintain this possibility on a best-effort basis. The
hack has been mentioned in #1046 and upstream in
https://github.com/sqlalchemy/sqlalchemy/discussions/12522 and is useful
to integrate psycopg pool with the SQLAlchemy NullPool.
Daniele Varrazzo [Fri, 21 Feb 2025 16:27:29 +0000 (17:27 +0100)]
fix(pool): check that there is some connection in the pool before shrinking
There is a race condition between shrinking the pool and closing an
expired connection which might result in attempting to pop from the
empty _pool deque. Add a check to prevent that.
The bug is not serious as it doesn't compromise the pool working but it causes
noise in the logging.
Daniele Varrazzo [Wed, 30 Oct 2024 13:26:34 +0000 (14:26 +0100)]
chore: ignore type error reported by mypy 1.12, 1.13
If a generator returns None, the StopIteration.args tuple is empty. The
fact we explicitly checks for it and in that case we return none looks
now an error for Mypy.
I don't see an obvious way around it.
Maybe related to https://github.com/python/mypy/issues/1933 which is an
historical issue (opened in 2016).
Reported in https://github.com/python/mypy/issues/18073
Daniele Varrazzo [Sun, 23 Jun 2024 11:12:34 +0000 (13:12 +0200)]
fix: fix compatibility with numpy 2.0
The only meaningful code change is with the bool class, whose type name
changed from `numpy.bool_` to `numpy.bool`. Note that using `bool`
causes a FutureWarning on import for numpy > 1.20, therefore we avoid
testing it (we still successfully test the `bool_` alias).
Other changes are related to tests: certain constant aliases are no more
available, so we skip those tests conditionally to numpy version.
Pin a minimum numpy version, to make sure we test also a numpy v1.
Picking 1.18, released back in Dec 2019, as it's the first minor
version offering binary packages for Python 3.8. This min dependency will
need to be updated when dropping Python 3.8 support.
Daniele Varrazzo [Sun, 31 Mar 2024 20:57:42 +0000 (20:57 +0000)]
fix: more careful stripping of error prefixes
Only strip the known prefixes, both in English and in the currently
known localizations.
Added script to generate regexp to match every backend localization. The
script was executed on PostgreSQL commit f4ad0021af (on master branch,
before v17).
fix(pool): avoid possible deadlock (until timeout) on pool closing
With the previous change to avoid finding open connections in the pool
(#784), stopping the worker was moved into the critical section. This
can create a deadlock in case a worker is in the process of obtaining a
new connection, because putting it to the pool requires the lock. The
deadlock only last for the default 5s timeout passed to _stop_workers().
Solve the problem by guarding _add_to_pool() to avoid it to try to add
the connection if the pool is closed.
However, refactor the pool closing sequence too and close the workers
and other resources that now out of the state outside the critical
section to keep the operation running under lock to a minimum.
fix(pool): make sure there are no connection in the pool after close()
The case has been reported in #784. While not easy to reproduce, it
seems that it might be caused by the pool being closed while a worker is
still trying to create a connection, which will be put in the _pool
state after supposedly no other operation should have been performed.
Stop the workers and then empty the pool only after they have stopped to
run.
Also refactor the cleanup of the pool and waiting queue, moving them
to close(). There is no reason why a method called "stop workers" should
empty them, and there is no other code path that use such feature.
Denis Laxalde [Mon, 4 Mar 2024 07:37:58 +0000 (08:37 +0100)]
feat(tools): check last modification times in async_to_sync.py
We now only process _async.py files with a modification time higher than
their sync counterpart. A -B,--convert-all option is added to force
conversion of all (specified) input files and skip last-modification
time check.
fix: avoid to create reference loops in datetime adapters
Setting the reference to a bound method in the state creates a reference
loop.
The issue is minimal because the gc will be able to break these loops
anyway and because it mostly happens with exotic or unsupported
date/interval styles.
fix: perform multiple attemps if a host name resolve to multiple hosts
We now perform DNS resolution in Python both in the sync and async code.
Because the two code paths are now very similar, make sure they pass the
same tests.
Nick Pope [Tue, 28 Nov 2023 12:46:59 +0000 (12:46 +0000)]
chore: align to master
The pool branch is diverging unnecessarily from master because so far I
have only cherry picked the changes useful for the pool and compatible
with 3.2.x, but this is creating unnecessary differences and making
cherry-picking harder and harder.
From now on we should use the policy of cherry-picking everything unless
non compatible (not a new feature or a breaking change), similarly to
what we do on the maint-3.1 branch.
Daniele Varrazzo [Fri, 29 Dec 2023 23:41:07 +0000 (00:41 +0100)]
fix(pool): use an exponential backoff to separate failing checks
This is a consistent behaviour with with the exponential backoff used in
reconnection attempts, and actually we could reuse the same object
implementing the backoff + jitter.
fix: define the Row TypeVar as defaulting to TupleRow
This allows to return `Self` uniformly from the `Connection.connect()` class
method, which in turns allows to subclass the connection without the
need of redefining the complex signature.
Denis Laxalde [Wed, 18 Oct 2023 08:35:50 +0000 (10:35 +0200)]
chore: use AnyIO 4.0+ in tests dependencies
We set the 'loop_factory' asyncio option for parametrizing the
anyio_backend() fixture as anyio 4.0 has replaced the 'policy' option by
'loop_factory'.
In tests/pool/test_pool_async_noasyncio.py, we previously used
the anyio_backend_options fixture to retrieve the loop policy; now we
build it directly. This could be improved by using the loop_factory, but
only from Python 3.11 with an asyncio.Runner().
Daniele Varrazzo [Sat, 14 Oct 2023 07:59:57 +0000 (09:59 +0200)]
refactor(pool): more logical ordering for kwargs
- conn construction params (class, kwargs, the class affects the pool
type annotations so first is better);
- sizing (documented as an important parameter for the pool);
- open?
- callbacks (in the order they are used);
- name;
- other details more unlikely to be touched.