Prevent race condition with downloaded id

This commit is contained in:
Ben Sarmiento
2024-02-05 03:27:45 +01:00
parent dcdb83349a
commit c8bbfc8650
4 changed files with 38 additions and 26 deletions

View File

@@ -118,8 +118,6 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
}
})
t.log.Infof("Found %d broken torrents to repair in total", toRepair.Cardinality())
var wg sync.WaitGroup
toRepair.Each(func(torrent *Torrent) bool {
wg.Add(1)
@@ -143,8 +141,6 @@ func (t *TorrentManager) Repair(torrent *Torrent, wg *sync.WaitGroup) {
return
}
t.log.Infof("Attempting repair for torrent %s", t.GetKey(torrent))
// blocks for approx 45 minutes if active torrents are full
if !t.canCapacityHandle() {
t.log.Error("Blocked for too long due to limit of active torrents, cannot continue with the repair")
@@ -160,7 +156,7 @@ func (t *TorrentManager) Repair(torrent *Torrent, wg *sync.WaitGroup) {
}
func (t *TorrentManager) repair(torrent *Torrent) {
t.log.Infof("Started repair process for torrent %s", t.GetKey(torrent))
t.log.Infof("Started repair process for torrent %s (ids=%v)", t.GetKey(torrent), torrent.DownloadedIDs.Union(torrent.InProgressIDs).ToSlice())
// handle torrents with incomplete links for selected files
if !t.assignUnassignedLinks(torrent) {
@@ -192,6 +188,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
}
}
})
torrent.DownloadedIDs.Add(info.ID)
t.saveTorrentChangesToDisk(torrent, nil)
t.log.Infof("Successfully repaired torrent %s by redownloading", t.GetKey(torrent))
return
@@ -309,6 +306,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
if selection == "" {
// only delete the old torrent if we are redownloading all files
oldTorrentIDs = torrent.DownloadedIDs.Union(torrent.InProgressIDs).ToSlice()
t.log.Debugf("Redownloading torrent %s with all files (torrents=%v)", t.GetKey(torrent), oldTorrentIDs)
tmpSelection := ""
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
tmpSelection += fmt.Sprintf("%d,", file.ID) // select all files