Skip to content

[4.4] Roll back data written to output buffer on packing failure #641

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
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
3 changes: 2 additions & 1 deletion neo4j/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ def _append(self, signature, fields=(), response=None):
:param fields: the fields of the message as a tuple
:param response: a response object to handle callbacks
"""
self.packer.pack_struct(signature, fields)
with self.outbox.tmp_buffer():
self.packer.pack_struct(signature, fields)
self.outbox.wrap_message()
self.responses.append(response)

Expand Down
21 changes: 20 additions & 1 deletion neo4j/io/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
# limitations under the License.


from contextlib import contextmanager
import socket
from struct import pack as struct_pack

from neo4j.exceptions import (
AuthError,
Neo4jError,
ServiceUnavailable,
SessionExpired,
Expand Down Expand Up @@ -94,11 +94,14 @@ def __init__(self, max_chunk_size=16384):
self._chunked_data = bytearray()
self._raw_data = bytearray()
self.write = self._raw_data.extend
self._tmp_buffering = 0

def max_chunk_size(self):
return self._max_chunk_size

def clear(self):
if self._tmp_buffering:
raise RuntimeError("Cannot clear while buffering")
self._chunked_data = bytearray()
self._raw_data.clear()

Expand Down Expand Up @@ -128,13 +131,29 @@ def _chunk_data(self):
self._raw_data.clear()

def wrap_message(self):
if self._tmp_buffering:
raise RuntimeError("Cannot wrap message while buffering")
self._chunk_data()
self._chunked_data += b"\x00\x00"

def view(self):
if self._tmp_buffering:
raise RuntimeError("Cannot view while buffering")
self._chunk_data()
return memoryview(self._chunked_data)

@contextmanager
def tmp_buffer(self):
self._tmp_buffering += 1
old_len = len(self._raw_data)
try:
yield
except Exception:
del self._raw_data[old_len:]
raise
finally:
self._tmp_buffering -= 1


class ConnectionErrorHandler:
"""
Expand Down