Skip to content

Commit df24bc7

Browse files
author
Zhen
committed
Prevent from closing driver multiple times
Close driver object gracefully when it falls out of scope
1 parent cc365bd commit df24bc7

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

neo4j/bolt/connection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,17 @@ def close(self):
507507
""" Close all connections and empty the pool.
508508
This method is thread safe.
509509
"""
510-
with self.lock:
511-
self._closed = True
512-
for address in list(self.connections):
513-
self.remove(address)
510+
if self._closed:
511+
return
512+
try:
513+
with self.lock:
514+
if not self._closed:
515+
self._closed = True
516+
for address in list(self.connections):
517+
self.remove(address)
518+
except TypeError as e:
519+
print(e)
520+
pass
514521

515522
def closed(self):
516523
""" Return :const:`True` if this pool is closed, :const:`False`

neo4j/v1/api.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
RETRY_DELAY_MULTIPLIER = 2.0
4242
RETRY_DELAY_JITTER_FACTOR = 0.2
4343

44+
4445
def last_bookmark(b0, b1):
4546
""" Return the latest of two bookmarks by looking for the maximum
4647
integer value following the last colon in the bookmark string.
@@ -141,10 +142,7 @@ class Driver(object):
141142
#: Indicator of driver closure.
142143
_closed = False
143144

144-
_lock = None
145-
146145
def __init__(self, pool, **config):
147-
self._lock = RLock()
148146
self._pool = pool
149147
self._max_retry_time = config.get("max_retry_time", default_config["max_retry_time"])
150148

@@ -185,18 +183,14 @@ def close(self):
185183
""" Shut down, closing any open connections that were spawned by
186184
this Driver.
187185
"""
188-
if self._lock is None:
189-
return
190-
with self._lock:
191-
if not self._closed:
192-
self._closed = True
193-
if self._pool is not None:
194-
self._pool.close()
195-
self._pool = None
186+
if not self._closed:
187+
self._closed = True
188+
if self._pool is not None:
189+
self._pool.close()
190+
self._pool = None
196191

197192
def closed(self):
198-
with self._lock:
199-
return self._closed
193+
return self._closed
200194

201195

202196
class Session(object):

0 commit comments

Comments
 (0)