Skip to content

Commit

Permalink
Ensure sufficient available blocks remain if add_parfile fails (#2119)
Browse files Browse the repository at this point in the history
* ensure sufficient available blocks remain if add_parfile fails

* replace while loop, drop use of pop()
  • Loading branch information
jcfp committed Mar 16, 2022
1 parent 100018d commit a9470ae
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions sabnzbd/nzbstuff.py
Expand Up @@ -1140,17 +1140,21 @@ def get_extra_blocks(self, setname: str, needed_blocks: int) -> int:
block_list.append(nzf)
avail_blocks += nzf.blocks

# Sort by smallest blocks last, to be popped first
block_list.sort(key=lambda x: x.blocks, reverse=True)
# Sort the smallest blocks first
block_list.sort(key=lambda x: x.blocks, reverse=False)
logging.info("%s blocks available", avail_blocks)

# Enough?
if avail_blocks >= needed_blocks:
added_blocks = 0
while added_blocks < needed_blocks:
new_nzf = block_list.pop()
for new_nzf in block_list:
if self.add_parfile(new_nzf):
added_blocks += new_nzf.blocks
if added_blocks >= needed_blocks:
break
else:
# End of block_list reached with insufficient blocks added
return 0

logging.info("Added %s blocks to %s", added_blocks, self.final_name)
return added_blocks
Expand Down

0 comments on commit a9470ae

Please sign in to comment.