Skip to content

Commit

Permalink
Stop throwing if saving a request fails
Browse files Browse the repository at this point in the history
Saving a cached request may fail for any number of reasons:
* Running out of memory
* Running out of disk space
* Other IO-related errors

Since we have already made the request and received the data we might
as well use it instead of dropping it on the floor just because we can't
save it to satisfy future requests.
  • Loading branch information
hexagonrecursion committed Feb 19, 2021
1 parent f511960 commit 2886ec7
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions cachecontrol/filewrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# SPDX-License-Identifier: Apache-2.0

from io import BytesIO
import logging


logger = logging.getLogger(__name__)


class CallbackFileWrapper(object):
Expand Down Expand Up @@ -51,9 +55,26 @@ def __is_fp_closed(self):
# TODO: Add some logging here...
return False

def __call_callback(self):
if self.__callback is not None and self.__buf is not None:
try:
self.__callback(self.__buf.getvalue())
except Exception as e:
self.__callback = None
self.__buf = None
logger.exception('Failed to save a cached request', exc_info=True)

def __append(self, data):
if self.__callback is not None and self.__buf is not None:
try:
self.__buf.write(data)
except Exception as e:
self.__callback = None
self.__buf = None
logger.exception('Failed to save a cached request', exc_info=True)

def _close(self):
if self.__callback:
self.__callback(self.__buf.getvalue())
self.__call_callback()

# We assign this to None here, because otherwise we can get into
# really tricky problems where the CPython interpreter dead locks
Expand All @@ -64,7 +85,7 @@ def _close(self):

def read(self, amt=None):
data = self.__fp.read(amt)
self.__buf.write(data)
self.__append(data)
if self.__is_fp_closed():
self._close()

Expand All @@ -77,7 +98,7 @@ def _safe_read(self, amt):
# of the chunk.
return data

self.__buf.write(data)
self.__append(data)
if self.__is_fp_closed():
self._close()

Expand Down

0 comments on commit 2886ec7

Please sign in to comment.