From ef302ea6a9ca41e6106f6e41f63490583933a3f6 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 21 Feb 2021 13:07:58 +0100 Subject: [PATCH] Don't print warning gc'ing a pool in incomplete state --- psycopg3/psycopg3/pool.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/psycopg3/psycopg3/pool.py b/psycopg3/psycopg3/pool.py index 6b8aa28cf..9a83993b6 100644 --- a/psycopg3/psycopg3/pool.py +++ b/psycopg3/psycopg3/pool.py @@ -85,7 +85,6 @@ class ConnectionPool: self._pool: Deque[Tuple[Connection, float]] = deque() self._waiting: Deque["WaitingClient"] = deque() self._lock = threading.RLock() - self._closed = False self.sched = Scheduler() self._wqueue: "Queue[MaintenanceTask]" = Queue() @@ -93,12 +92,19 @@ class ConnectionPool: for i in range(num_workers): t = threading.Thread(target=self.worker, args=(self._wqueue,)) t.daemon = True - t.start() self._workers.append(t) self._sched_runner = threading.Thread(target=self.sched.run) self._sched_runner.daemon = True + + # _close should be the last property to be set in the state + # to avoid warning on __del__ in case __init__ fails. + self._closed = False + + # The object state is complete. Start the worker threads self._sched_runner.start() + for t in self._workers: + t.start() # Populate the pool with initial minconn connections # Block if setup_timeout is > 0, otherwise fill the pool in background @@ -124,7 +130,10 @@ class ConnectionPool: ) def __del__(self) -> None: - self.close() + # If the '_closed' property is not set we probably failed in __init__. + # Don't try anything complicated as probably it won't work. + if hasattr(self, "_closed"): + self.close() @contextmanager def connection( -- 2.47.2