[CONPY-256] ConnectionPool.get_connection returns connection twice Created: 2023-03-29  Updated: 2023-04-11  Resolved: 2023-04-11

Status: Closed
Project: MariaDB Connector/Python
Component/s: Connection Pooling
Affects Version/s: 1.1.6
Fix Version/s: 1.1.7

Type: Bug Priority: Major
Reporter: G.Mech Assignee: Georg Richter
Resolution: Fixed Votes: 0
Labels: None
Environment:

Win10 21H2, mariadb Server 10.6.11 (docker)


Attachments: Text File duplicate_connection.patch     File test_duplicate_connection.py    
Python Version: 3.10.4

 Description   

Steps to reproduce

1. Setup server with short WAIT_TIMEOUT:
docker run -e MARIADB_ROOT_PASSWORD="<put_password_here>" -p 3306:3306 mariadb:10.6.11 --wait-timeout=10
2. Create a connection pool (see attached file)
3. Wait a little longer than WAIT_TIMEOUT
4. Get pool_size connections without closing them

Expected outcome

Each connection returned by get_connection is unique.

Actual outcome

One connection is returned twice

Suspected cause

get_connections deletes the connection to be returned from _connections_free and adds it to _connections_used

for i in range(0, len(self._connections_free)):
    conn = self._connections_free[i]
   # abbreviated: call _replace_connection if  necessary;
    conn._used += 1
    self._connections_used.append(conn)
    del self._connections_free[i]

If _replace_connection is called, it adds the new connection at the end of the list, so

del self._connections_free[i]

deletes a different connection.

Possible fix

See attached patch.

diff --git a/mariadb/connectionpool.py b/mariadb/connectionpool.py
index 882ff69..b17c984 100644
--- a/mariadb/connectionpool.py
+++ b/mariadb/connectionpool.py
@@ -218,7 +218,8 @@ class ConnectionPool(object):
 
                 conn._used += 1
                 self._connections_used.append(conn)
-                del self._connections_free[i]
+                x = self._connections_free.index(conn)
+                del self._connections_free[x]
                 return conn
 
         return None



 Comments   
Comment by Georg Richter [ 2023-04-11 ]

Thanks for you bug report and providing a fix.

Generated at Thu Feb 08 03:31:25 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.