Skip to content

Commit 2886ec7

Browse files
Stop throwing if saving a request fails
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.
1 parent f511960 commit 2886ec7

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

cachecontrol/filewrapper.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from io import BytesIO
6+
import logging
7+
8+
9+
logger = logging.getLogger(__name__)
610

711

812
class CallbackFileWrapper(object):
@@ -51,9 +55,26 @@ def __is_fp_closed(self):
5155
# TODO: Add some logging here...
5256
return False
5357

58+
def __call_callback(self):
59+
if self.__callback is not None and self.__buf is not None:
60+
try:
61+
self.__callback(self.__buf.getvalue())
62+
except Exception as e:
63+
self.__callback = None
64+
self.__buf = None
65+
logger.exception('Failed to save a cached request', exc_info=True)
66+
67+
def __append(self, data):
68+
if self.__callback is not None and self.__buf is not None:
69+
try:
70+
self.__buf.write(data)
71+
except Exception as e:
72+
self.__callback = None
73+
self.__buf = None
74+
logger.exception('Failed to save a cached request', exc_info=True)
75+
5476
def _close(self):
55-
if self.__callback:
56-
self.__callback(self.__buf.getvalue())
77+
self.__call_callback()
5778

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

6586
def read(self, amt=None):
6687
data = self.__fp.read(amt)
67-
self.__buf.write(data)
88+
self.__append(data)
6889
if self.__is_fp_closed():
6990
self._close()
7091

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

80-
self.__buf.write(data)
101+
self.__append(data)
81102
if self.__is_fp_closed():
82103
self._close()
83104

0 commit comments

Comments
 (0)