make sure that we don't call HashBlobWriter.finished_cb multiple times when calling close()

This commit is contained in:
Kay Kurokawa 2017-09-13 14:01:20 -04:00 committed by Jack Robison
parent 468a16af58
commit 39c4db3471
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF

View file

@ -47,6 +47,7 @@ class HashBlobWriter(object):
self.write_handle = BytesIO() self.write_handle = BytesIO()
self.length_getter = length_getter self.length_getter = length_getter
self.finished_cb = finished_cb self.finished_cb = finished_cb
self.finished_cb_d = None
self._hashsum = get_lbry_hash_obj() self._hashsum = get_lbry_hash_obj()
self.len_so_far = 0 self.len_so_far = 0
@ -62,7 +63,7 @@ class HashBlobWriter(object):
self._hashsum.update(data) self._hashsum.update(data)
self.len_so_far += len(data) self.len_so_far += len(data)
if self.len_so_far > self.length_getter(): if self.len_so_far > self.length_getter():
self.finished_cb( self.finished_cb_d = self.finished_cb(
self, self,
Failure(InvalidDataError("Length so far is greater than the expected length." Failure(InvalidDataError("Length so far is greater than the expected length."
" %s to %s" % (self.len_so_far, " %s to %s" % (self.len_so_far,
@ -70,7 +71,7 @@ class HashBlobWriter(object):
else: else:
self.write_handle.write(data) self.write_handle.write(data)
if self.len_so_far == self.length_getter(): if self.len_so_far == self.length_getter():
self.finished_cb(self) self.finished_cb_d = self.finished_cb(self)
def close_handle(self): def close_handle(self):
if self.write_handle is not None: if self.write_handle is not None:
@ -78,12 +79,13 @@ class HashBlobWriter(object):
self.write_handle = None self.write_handle = None
def close(self, reason=None): def close(self, reason=None):
# we've already closed, so do nothing # if we've already called finished_cb because we either finished writing
if self.write_handle is None: # or closed already, do nothing
if self.finished_cb_d is not None:
return return
if reason is None: if reason is None:
reason = Failure(DownloadCanceledError()) reason = Failure(DownloadCanceledError())
self.finished_cb(self, reason) self.finished_cb_d = self.finished_cb(self, reason)
class HashBlob(object): class HashBlob(object):