@@ -446,13 +446,17 @@ cdef class Protocol(BaseProtocol):
446446 cdef int _receive_packet(self , Message message,
447447 bint check_request_boundary = False ) except - 1 :
448448 cdef:
449+ bint orig_check_request_boundary
449450 ReadBuffer buf = self ._read_buf
450451 uint16_t refuse_message_len
451452 const char_type* ptr
453+ orig_check_request_boundary = buf._check_request_boundary
452454 buf._check_request_boundary = \
453455 check_request_boundary and self ._caps.supports_end_of_response
454- buf.wait_for_packets_sync()
455- buf._check_request_boundary = False
456+ try :
457+ buf.wait_for_packets_sync()
458+ finally :
459+ buf._check_request_boundary = orig_check_request_boundary
456460 if buf._current_packet.packet_type == TNS_PACKET_TYPE_MARKER:
457461 self ._reset(message)
458462 elif buf._current_packet.packet_type == TNS_PACKET_TYPE_REFUSE:
@@ -832,7 +836,12 @@ cdef class BaseAsyncProtocol(BaseProtocol):
832836 await buf.wait_for_packets_async()
833837 buf._check_request_boundary = False
834838 if buf._current_packet.packet_type == TNS_PACKET_TYPE_MARKER:
835- await self ._reset(in_pipeline)
839+ if in_pipeline:
840+ # skip to next packet as the marker packet doesn't contain any
841+ # useful information
842+ buf.wait_for_packets_sync()
843+ else :
844+ await self ._reset()
836845 elif buf._current_packet.packet_type == TNS_PACKET_TYPE_REFUSE:
837846 self ._write_buf._packet_sent = False
838847 buf.skip_raw_bytes(2 )
@@ -843,26 +852,20 @@ cdef class BaseAsyncProtocol(BaseProtocol):
843852 ptr = buf.read_raw_bytes(refuse_message_len)
844853 message.error_info.message = ptr[:refuse_message_len].decode()
845854
846- async def _reset(self , bint in_pipeline = False ):
855+ async def _reset(self ):
847856 cdef:
848857 uint8_t marker_type, packet_type
849- uint8_t expected_marker_type
850858
851- # send reset marker, if not in a pipeline
852- # NOTE: the expected marker type is different inside a pipeline!
853- if in_pipeline:
854- expected_marker_type = TNS_MARKER_TYPE_BREAK
855- else :
856- expected_marker_type = TNS_MARKER_TYPE_RESET
857- self ._send_marker(self ._write_buf, TNS_MARKER_TYPE_RESET)
859+ # send reset marker
860+ self ._send_marker(self ._write_buf, TNS_MARKER_TYPE_RESET)
858861
859862 # read and discard all packets until a reset marker is received
860863 while True :
861864 packet_type = self ._read_buf._current_packet.packet_type
862865 if packet_type == TNS_PACKET_TYPE_MARKER:
863866 self ._read_buf.skip_raw_bytes(2 )
864867 self ._read_buf.read_ub1(& marker_type)
865- if marker_type == expected_marker_type :
868+ if marker_type == TNS_MARKER_TYPE_RESET :
866869 break
867870 await self ._read_buf.wait_for_packets_async()
868871
@@ -907,17 +910,24 @@ cdef class BaseAsyncProtocol(BaseProtocol):
907910 database. An end pipeline message is sent to the database and then
908911 the responses to all of the messages are processed.
909912 """
910- cdef Message message, end_message
913+ cdef:
914+ ssize_t num_responses_to_discard
915+ ReadBuffer buf = self ._read_buf
916+ Message message, end_message
911917 end_message = conn_impl._create_message(EndPipelineMessage)
912918 end_message.send(self ._write_buf)
919+ buf._check_request_boundary = True
920+ buf._in_pipeline = True
913921 try :
922+ num_responses_to_discard = len (messages) + 1
914923 for message in messages:
915924 try :
916- await self ._receive_packet(message,
917- check_request_boundary = True ,
918- in_pipeline = True )
925+ if not buf.has_response():
926+ await buf.wait_for_response_async()
927+ buf._start_packet( )
919928 message.preprocess()
920- message.process(self ._read_buf)
929+ message.process(buf)
930+ num_responses_to_discard -= 1
921931 self ._process_call_status(conn_impl, message.call_status)
922932 message._check_and_raise_exception()
923933 except Exception as e:
@@ -926,11 +936,15 @@ cdef class BaseAsyncProtocol(BaseProtocol):
926936 message.pipeline_result_impl._capture_err(e)
927937 await self ._receive_packet(end_message,
928938 check_request_boundary = True )
929- end_message.process(self ._read_buf)
939+ end_message.process(buf)
940+ num_responses_to_discard = 0
930941 end_message._check_and_raise_exception()
931942 except :
932- await self ._reset( )
943+ await buf.discard_pipeline_responses(num_responses_to_discard )
933944 raise
945+ finally :
946+ buf._check_request_boundary = False
947+ buf._in_pipeline = False
934948
935949
936950class AsyncProtocol (BaseAsyncProtocol , asyncio.Protocol ):
0 commit comments