Skip to content

Roll back data written to output buffer on packing failure #640

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/_async/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,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
20 changes: 20 additions & 0 deletions neo4j/_async/io/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import asyncio
from contextlib import contextmanager
import logging
import socket
from struct import pack as struct_pack
Expand Down Expand Up @@ -94,11 +95,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 +132,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
3 changes: 2 additions & 1 deletion neo4j/_sync/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,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
20 changes: 20 additions & 0 deletions neo4j/_sync/io/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import asyncio
from contextlib import contextmanager
import logging
import socket
from struct import pack as struct_pack
Expand Down Expand Up @@ -94,11 +95,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 +132,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