Skip to content

Commit d0d02bf

Browse files
authored
Async: await socket closure (#701)
Additionally, add warning about known performance issue for aync driver with Python 3.8.
1 parent 4fb25a2 commit d0d02bf

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

docs/source/async_api.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Async API Documentation
1414

1515
.. versionadded:: 5.0
1616

17+
.. warning::
18+
There are known issue with Python 3.8 and the async driver where it
19+
gradually slows down. Generally, it's recommended to use the latest
20+
supported version of Python for best performance, stability, and security.
21+
1722
******************
1823
AsyncGraphDatabase
1924
******************

neo4j/_async/io/_bolt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ async def close(self):
658658
pass
659659
log.debug("[#%04X] C: <CLOSE>", self.local_port)
660660
try:
661-
self.socket.close()
661+
await self.socket.close()
662662
except OSError:
663663
pass
664664
finally:

neo4j/_async_compat/network/_bolt_socket.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ async def sendall(self, data):
136136
io_fut = self._writer.drain()
137137
return await self._wait_for_io(io_fut)
138138

139-
def close(self):
139+
async def close(self):
140140
self._writer.close()
141+
await self._writer.wait_closed()
141142

142143
@classmethod
143144
async def _connect_secure(cls, resolved_address, timeout, keep_alive, ssl):
@@ -210,7 +211,7 @@ async def _connect_secure(cls, resolved_address, timeout, keep_alive, ssl):
210211
log.debug("[#0000] C: <TIMEOUT> %s", resolved_address)
211212
log.debug("[#0000] C: <CLOSE> %s", resolved_address)
212213
if s:
213-
cls.close_socket(s)
214+
await cls.close_socket(s)
214215
raise ServiceUnavailable(
215216
"Timed out trying to establish connection to {!r}".format(
216217
resolved_address))
@@ -296,15 +297,15 @@ async def _handshake(self, resolved_address):
296297
return self, agreed_version, handshake, data
297298

298299
@classmethod
299-
def close_socket(cls, socket_):
300+
async def close_socket(cls, socket_):
300301
if isinstance(socket_, socket):
301302
try:
302303
socket_.shutdown(SHUT_RDWR)
303304
socket_.close()
304305
except OSError:
305306
pass
306307
else:
307-
socket_.close()
308+
await socket_.close()
308309

309310
@classmethod
310311
async def connect(cls, address, *, timeout, custom_resolver, ssl_context,
@@ -339,12 +340,12 @@ async def connect(cls, address, *, timeout, custom_resolver, ssl_context,
339340
log.debug("[#%04X] C: <CONNECTION FAILED> %s", local_port,
340341
err_str)
341342
if s:
342-
cls.close_socket(s)
343+
await cls.close_socket(s)
343344
errors.append(error)
344345
failed_addresses.append(resolved_address)
345346
except Exception:
346347
if s:
347-
cls.close_socket(s)
348+
await cls.close_socket(s)
348349
raise
349350
if not errors:
350351
raise ServiceUnavailable(

0 commit comments

Comments
 (0)