Skip to content

Async: await socket closure #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/source/async_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Async API Documentation

.. versionadded:: 5.0

.. warning::
There are known issue with Python 3.8 and the async driver where it
gradually slows down. Generally, it's recommended to use the latest
supported version of Python for best performance, stability, and security.

******************
AsyncGraphDatabase
******************
Expand Down
2 changes: 1 addition & 1 deletion neo4j/_async/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ async def close(self):
pass
log.debug("[#%04X] C: <CLOSE>", self.local_port)
try:
self.socket.close()
await self.socket.close()
except OSError:
pass
finally:
Expand Down
13 changes: 7 additions & 6 deletions neo4j/_async_compat/network/_bolt_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ async def sendall(self, data):
io_fut = self._writer.drain()
return await self._wait_for_io(io_fut)

def close(self):
async def close(self):
self._writer.close()
await self._writer.wait_closed()

@classmethod
async def _connect_secure(cls, resolved_address, timeout, keep_alive, ssl):
Expand Down Expand Up @@ -210,7 +211,7 @@ async def _connect_secure(cls, resolved_address, timeout, keep_alive, ssl):
log.debug("[#0000] C: <TIMEOUT> %s", resolved_address)
log.debug("[#0000] C: <CLOSE> %s", resolved_address)
if s:
cls.close_socket(s)
await cls.close_socket(s)
raise ServiceUnavailable(
"Timed out trying to establish connection to {!r}".format(
resolved_address))
Expand Down Expand Up @@ -296,15 +297,15 @@ async def _handshake(self, resolved_address):
return self, agreed_version, handshake, data

@classmethod
def close_socket(cls, socket_):
async def close_socket(cls, socket_):
if isinstance(socket_, socket):
try:
socket_.shutdown(SHUT_RDWR)
socket_.close()
except OSError:
pass
else:
socket_.close()
await socket_.close()

@classmethod
async def connect(cls, address, *, timeout, custom_resolver, ssl_context,
Expand Down Expand Up @@ -339,12 +340,12 @@ async def connect(cls, address, *, timeout, custom_resolver, ssl_context,
log.debug("[#%04X] C: <CONNECTION FAILED> %s", local_port,
err_str)
if s:
cls.close_socket(s)
await cls.close_socket(s)
errors.append(error)
failed_addresses.append(resolved_address)
except Exception:
if s:
cls.close_socket(s)
await cls.close_socket(s)
raise
if not errors:
raise ServiceUnavailable(
Expand Down