Skip to content

Stop throwing if saving a request fails #242

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

Closed
wants to merge 1 commit into from
Closed
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
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