Clone set before modifying

This commit is contained in:
Ben Sarmiento
2024-05-27 17:17:45 +02:00
parent f28255a142
commit 964fdfcbf9
5 changed files with 25 additions and 14 deletions

View File

@@ -154,7 +154,7 @@ func (t *TorrentManager) Repair(torrent *Torrent, wg *sync.WaitGroup) {
return
}
if err := torrent.State.Event(context.Background(), "repair_torrent"); err != nil {
if err := torrent.State.Event(context.Background(), "repair_torrent"); err != nil && t.inProgressHashes.Contains(torrent.Hash) {
t.repairLog.Errorf("Failed to mark torrent %s as under repair: %v", t.GetKey(torrent), err)
return
}
@@ -191,7 +191,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
torrent.State.Event(context.Background(), "mark_as_repaired")
t.repairLog.Infof("Successfully repaired torrent %s by redownloading whole torrent", t.GetKey(torrent))
// delete the torrents it replaced
torrent.DownloadedIDs.Each(func(torrentID string) bool {
torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool {
if torrentID != info.ID {
t.setXToBinOnceYDone(torrentID, info.ID)
}
@@ -201,7 +201,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
} else if info != nil && info.Progress != 100 {
t.repairLog.Infof("Torrent %s is still in progress after redownloading whole torrent but it should be repaired once done", t.GetKey(torrent))
// once info.ID is done, we can delete the old torrent
torrent.DownloadedIDs.Each(func(torrentID string) bool {
torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool {
if torrentID != info.ID {
t.setXToBinOnceYDone(torrentID, info.ID)
}
@@ -272,8 +272,9 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
rarCount := 0
unassignedCount := 0
newUnassignedLinks := cmap.New[*realdebrid.Download]()
var assignedLinks []string
torrent.UnassignedLinks.Each(func(link string) bool {
torrent.UnassignedLinks.Clone().Each(func(link string) bool {
// unrestrict each unassigned link that was filled out during torrent init
unrestrict := t.UnrestrictLinkUntilOk(link, true)
if unrestrict == nil {
@@ -287,6 +288,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
// base it on size because why not?
if !assigned && file.State.Is("broken_file") && file.Bytes == unrestrict.Filesize && strings.HasSuffix(strings.ToLower(file.Path), strings.ToLower(unrestrict.Filename)) {
file.Link = link
assignedLinks = append(assignedLinks, link)
if strings.HasPrefix(file.Link, "https://real-debrid.com/d/") {
file.Link = file.Link[0:39]
}
@@ -314,6 +316,12 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
processedCount := assignedCount + unassignedCount + expiredCount
if processedCount%10 == 0 || processedCount == unassignedTotal {
// save each progress
for _, assignedLink := range assignedLinks {
torrent.UnassignedLinks.Remove(assignedLink)
}
t.writeTorrentToFile(torrent)
t.repairLog.Infof("Processed %d out of %d links (%d expired) to broken torrent %s", processedCount, unassignedTotal, expiredCount, t.GetKey(torrent))
}