Skip to content

Commit 40b0308

Browse files
committed
Ensure no acquire after close for ConnectionPool
1 parent 9993a0e commit 40b0308

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

neo4j/v1/bolt.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ class ConnectionPool(object):
378378
""" A collection of connections to one or more server addresses.
379379
"""
380380

381+
closed = False
382+
381383
def __init__(self, connector):
382384
self.connector = connector
383385
self.connections = {}
@@ -393,6 +395,9 @@ def acquire(self, address):
393395
""" Acquire a connection to a given address from the pool.
394396
This method is thread safe.
395397
"""
398+
if self.closed:
399+
raise ServiceUnavailable("This connection pool is closed so no new "
400+
"connections may be acquired")
396401
with self.lock:
397402
try:
398403
connections = self.connections[address]
@@ -433,6 +438,7 @@ def close(self):
433438
This method is thread safe.
434439
"""
435440
with self.lock:
441+
self.closed = True
436442
for address in list(self.connections):
437443
self.remove(address)
438444

test/test_connection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from socket import create_connection
2222

23-
from neo4j.v1 import ConnectionPool
23+
from neo4j.v1 import ConnectionPool, ServiceUnavailable
2424

2525
from test.util import ServerTestCase
2626

@@ -98,3 +98,9 @@ def test_releasing_twice(self):
9898
self.assert_pool_size(address, 0, 1)
9999
self.pool.release(connection)
100100
self.assert_pool_size(address, 0, 1)
101+
102+
def test_cannot_acquire_after_close(self):
103+
with ConnectionPool(lambda a: QuickConnection(create_connection(a))) as pool:
104+
pool.close()
105+
with self.assertRaises(ServiceUnavailable):
106+
_ = pool.acquire("X")

0 commit comments

Comments
 (0)