Skip to content

Commit 218156c

Browse files
committed
Clear PythonParser state when connection or parsing errors occur.
1 parent 1ed7f52 commit 218156c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

redis/asyncio/connection.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ def __init__(self, socket_read_size: int):
217217
self._chunks = []
218218
self._pos = 0
219219

220+
def _clear(self):
221+
self._buffer = b""
222+
self._chunks.clear()
223+
220224
def on_connect(self, connection: "Connection"):
221225
"""Called when the stream connects"""
222226
self._stream = connection._reader
@@ -247,12 +251,17 @@ async def read_response(self, disable_decoding: bool = False):
247251
# augment parsing buffer with previously read data
248252
self._buffer += b"".join(self._chunks)
249253
self._chunks.clear()
250-
self._pos = 0
251-
response = await self._read_response(disable_decoding=disable_decoding)
252-
# Successfully parsing a response allows us to clear our parsing buffer
253-
self._buffer = b""
254-
self._chunks.clear()
255-
return response
254+
try:
255+
self._pos = 0
256+
response = await self._read_response(disable_decoding=disable_decoding)
257+
except (ConnectionError, InvalidResponse):
258+
# We don't want these errors to be resumable
259+
self._clear()
260+
raise
261+
else:
262+
# Successfully parsing a response allows us to clear our parsing buffer
263+
self._clear()
264+
return response
256265

257266
async def _read_response(
258267
self, disable_decoding: bool = False
@@ -275,6 +284,7 @@ async def _read_response(
275284
# if the error is a ConnectionError, raise immediately so the user
276285
# is notified
277286
if isinstance(error, ConnectionError):
287+
self._clear() # Successful parse
278288
raise error
279289
# otherwise, we're dealing with a ResponseError that might belong
280290
# inside a pipeline response. the connection's read_response()

0 commit comments

Comments
 (0)