More error handling for edge cases.

This commit is contained in:
Itamar Turner-Trauring 2022-07-20 11:28:14 -04:00
parent 1b8b71b306
commit 43c6af6fde

View File

@ -355,7 +355,7 @@ class _ReadRangeProducer:
# the end of the range is inclusive. Actual conversion from # the end of the range is inclusive. Actual conversion from
# Python's exclusive ranges to inclusive ranges is handled by # Python's exclusive ranges to inclusive ranges is handled by
# werkzeug. The case where we're reading beyond the end of the # werkzeug. The case where we're reading beyond the end of the
# share is handled by caller (read_range().) # share is handled by the caller, read_range().
self.request.setHeader( self.request.setHeader(
"content-range", "content-range",
ContentRange( ContentRange(
@ -365,11 +365,24 @@ class _ReadRangeProducer:
self.first_read = False self.first_read = False
if not data and self.remaining > 0: if not data and self.remaining > 0:
# TODO Either data is missing locally (storage issue?) or a bug, d, self.result = self.result, None
# abort response with error. Until d.errback(
# https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3907 is implemented ValueError(
# we continue anyway. f"Should be {remaining} bytes left, but we got an empty read"
pass )
)
self.stopProducing()
return
if len(data) > self.remaining:
d, self.result = self.result, None
d.errback(
ValueError(
f"Should be {remaining} bytes left, but we got more than that ({len(data)})!"
)
)
self.stopProducing()
return
self.start += len(data) self.start += len(data)
self.remaining -= len(data) self.remaining -= len(data)
@ -377,19 +390,20 @@ class _ReadRangeProducer:
self.request.write(data) self.request.write(data)
# TODO remove the second clause in https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3907 if self.remaining == 0:
if self.remaining == 0 or not data: self.stopProducing()
self.request.unregisterProducer()
d = self.result
del self.result
d.callback(b"")
return
def pauseProducing(self): def pauseProducing(self):
pass pass
def stopProducing(self): def stopProducing(self):
pass if self.request is not None:
self.request.unregisterProducer()
self.request = None
if self.result is not None:
d = self.result
self.result = None
d.callback(b"")
def read_range( def read_range(