@@ -210,7 +210,6 @@ await Task.Run(() =>
210210 }
211211 Task . WaitAll ( tasks . ToArray ( ) ) ;
212212 InitNetwork ( ) ;
213- Downloads . Clear ( ) ;
214213 RecoverDownloads ( ) . Wait ( ) ;
215214 } ) ;
216215 Running = true ;
@@ -494,7 +493,7 @@ private void TryScheduleAttachmentDownload(SignalAttachment attachment)
494493 // this is the recommended way to call CreateDownload
495494 // see https://docs.microsoft.com/en-us/uwp/api/windows.networking.backgroundtransfer.backgrounddownloader#Methods
496495 DownloadOperation download = downloader . CreateDownload ( new Uri ( RetrieveAttachmentUrl ( attachmentPointer ) ) , tmpDownload ) ;
497- attachment . Guid = "" + download . Guid ;
496+ attachment . Guid = download . Guid . ToString ( ) ;
498497 SignalDBContext . UpdateAttachmentGuid ( attachment ) ;
499498 Downloads . Add ( attachment . Id , download ) ;
500499 Task . Run ( async ( ) =>
@@ -559,13 +558,20 @@ private async Task RecoverDownloads()
559558 SignalAttachment attachment = SignalDBContext . GetAttachmentByGuidNameLocked ( download . Guid . ToString ( ) ) ;
560559 if ( attachment != null )
561560 {
562- Downloads . Add ( attachment . Id , download ) ;
563- var t = Task . Run ( async ( ) =>
561+ if ( ! Downloads . ContainsKey ( attachment . Id ) )
564562 {
565- Logger . LogInformation ( "Attaching to download {0} ({1})" , attachment . Id , download . Guid ) ;
566- await download . AttachAsync ( ) ;
567- await HandleSuccessfullDownload ( attachment , download . ResultFile , download ) ;
568- } ) ;
563+ Logger . LogInformation ( "Creating attach task for attachment {0} ({1})" , attachment . Id , download . Guid ) ;
564+ Downloads . Add ( attachment . Id , download ) ;
565+ var t = Task . Run ( async ( ) =>
566+ {
567+ await download . AttachAsync ( ) ;
568+ await HandleSuccessfullDownload ( attachment , download . ResultFile , download ) ;
569+ } ) ;
570+ }
571+ else
572+ {
573+ Logger . LogInformation ( "Attachment {0} ({1}) already has a running task" , attachment . Id , download . Guid ) ;
574+ }
569575 }
570576 else
571577 {
0 commit comments