Skip to content

[4.4] Fix bufferviews + rewrite timeouts on send #843

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 2 commits into from
Oct 28, 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
9 changes: 6 additions & 3 deletions neo4j/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import logging
from logging import getLogger
from random import choice
import socket
from threading import (
Condition,
RLock,
Expand All @@ -52,6 +53,7 @@
from neo4j._exceptions import (
BoltError,
BoltHandshakeError,
SocketDeadlineExceeded,
)
from neo4j._deadline import (
connection_deadline,
Expand Down Expand Up @@ -509,11 +511,12 @@ def _append(self, signature, fields=(), response=None):
self.responses.append(response)

def _send_all(self):
data = self.outbox.view()
if data:
with self.outbox.view() as data:
if not data:
return
try:
self.socket.sendall(data)
except OSError as error:
except (OSError, socket.timeout, SocketDeadlineExceeded) as error:
self._set_defunct_write(error)
self.outbox.clear()

Expand Down
29 changes: 14 additions & 15 deletions neo4j/io/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,21 @@ def _chunk_data(self):
)
num_chunks = num_full_chunks + bool(chunk_rest)

data_view = memoryview(self._raw_data)
header_start = len(self._chunked_data)
data_start = header_start + 2
raw_data_start = 0
for i in range(num_chunks):
chunk_size = min(data_len - raw_data_start,
self._max_chunk_size)
self._chunked_data[header_start:data_start] = struct_pack(
">H", chunk_size
)
self._chunked_data[data_start:(data_start + chunk_size)] = \
data_view[raw_data_start:(raw_data_start + chunk_size)]
header_start += chunk_size + 2
with memoryview(self._raw_data) as data_view:
header_start = len(self._chunked_data)
data_start = header_start + 2
raw_data_start += chunk_size
del data_view
raw_data_start = 0
for i in range(num_chunks):
chunk_size = min(data_len - raw_data_start,
self._max_chunk_size)
self._chunked_data[header_start:data_start] = struct_pack(
">H", chunk_size
)
self._chunked_data[data_start:(data_start + chunk_size)] = \
data_view[raw_data_start:(raw_data_start + chunk_size)]
header_start += chunk_size + 2
data_start = header_start + 2
raw_data_start += chunk_size
self._raw_data.clear()

def wrap_message(self):
Expand Down
12 changes: 6 additions & 6 deletions neo4j/packstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ def receive(self, sock, n_bytes):
end = self.used + n_bytes
if end > len(self.data):
self.data += bytearray(end - len(self.data))
view = memoryview(self.data)
while self.used < end:
n = sock.recv_into(view[self.used:end], end - self.used)
if n == 0:
raise OSError("No data")
self.used += n
with memoryview(self.data) as view:
while self.used < end:
n = sock.recv_into(view[self.used:end], end - self.used)
if n == 0:
raise OSError("No data")
self.used += n