Skip to content
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

Fix socket wrapper close methods #705

Merged
merged 1 commit into from
Apr 8, 2022
Merged
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
29 changes: 18 additions & 11 deletions neo4j/_async_compat/network/_bolt_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,20 @@ async def _handshake(self, resolved_address):
# If no data is returned after a successful select
# response, the server has closed the connection
log.debug("[#%04X] S: <CLOSE>", local_port)
self.close()
await self.close()
raise ServiceUnavailable(
"Connection to {address} closed without handshake response".format(
address=resolved_address))
if data_size != 4:
# Some garbled data has been received
log.debug("[#%04X] S: @*#!", local_port)
self.close()
await self.close()
raise BoltProtocolError(
"Expected four byte Bolt handshake response from %r, received %r instead; check for incorrect port number" % (
resolved_address, data), address=resolved_address)
elif data == b"HTTP":
log.debug("[#%04X] S: <CLOSE>", local_port)
self.close()
await self.close()
raise ServiceUnavailable(
"Cannot to connect to Bolt service on {!r} "
"(looks like HTTP)".format(resolved_address))
Expand All @@ -298,14 +298,14 @@ async def _handshake(self, resolved_address):

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

@classmethod
async def connect(cls, address, *, timeout, custom_resolver, ssl_context,
Expand Down Expand Up @@ -417,6 +417,10 @@ def recv_into(self, buffer, nbytes):
def sendall(self, data):
return self._wait_for_io(self._socket.sendall, data)

def close(self):
self._socket.shutdown(SHUT_RDWR)
self._socket.close()

@classmethod
def _connect(cls, resolved_address, timeout, keep_alive):
"""
Expand Down Expand Up @@ -555,8 +559,11 @@ def _handshake(cls, s, resolved_address):
@classmethod
def close_socket(cls, socket_):
try:
socket_.shutdown(SHUT_RDWR)
socket_.close()
if isinstance(socket_, BoltSocket):
socket.close()
else:
socket_.shutdown(SHUT_RDWR)
socket_.close()
except OSError:
pass

Expand Down