Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program stops downloading VOD at 90-99% (the last time at 36%) #772

Closed
2 tasks done
seekomann opened this issue Aug 2, 2023 · 9 comments · Fixed by #792
Closed
2 tasks done

Program stops downloading VOD at 90-99% (the last time at 36%) #772

seekomann opened this issue Aug 2, 2023 · 9 comments · Fixed by #792
Labels
bug Something isn't working

Comments

@seekomann
Copy link

Checklist

Edition

Windows GUI Application

Describe your issue here

The last ~10 VOD parts (.ts) have 0 KB size in the cache folder.

Previously, the program rarely had problems with a 99% download, but the second download attempt was always successful. Now I can't download VOD at all.

Add any related files or extra information here

I have problem with these VODs:
https://www.twitch.tv/videos/1886532192
https://www.twitch.tv/videos/1887391942

@seekomann seekomann added the bug Something isn't working label Aug 2, 2023
@ScrubN
Copy link
Collaborator

ScrubN commented Aug 2, 2023

If you are able to reproduce again with the current version, please see if 1.53.1 solves the problem.

@seekomann
Copy link
Author

I forgot to write that I can download 480p VOD, but 1080p has problem. So i guess it's because of the file size, 4GiB is fine, but 16GiB is a problem.

I successfully downloaded full second VOD on the new version 1.53.2, but the first VOD had the same problem. The last attempt stops at 91% (Download Threads is 1, disk with cache folder has 150 GB of free space), one last 2029.ts part has 0 KB size.

@seekomann
Copy link
Author

I tried downloading VOD to the cache folder and finally downloaded second VOD.
Before that, my folder with downloaded VODs was on a separate drive where I didn't have a cache folder. Maybe that was the problem, idk.

@c3Vw
Copy link

c3Vw commented Aug 11, 2023

I believe i've found the reason for downloading issues, it is related to twitch instability.

In general TwitchDownloader doesn't retry downloading partial files that were stuck. If you have many .ts files to download (like 1000 or so) some of them will not download properly because of twitch servers/cdns. Let's say you have 4 download threads setup.
Each interrupted .ts file download locks that download thread. If 4 files fail during downloading, no more partial files get downloaded and the whole process is stuck. Depending on number of files to download, and download threads process will stop at different % progress, it might also stop at 100% because all files were gathered but not downloaded correctly.
Here's example:

  • 10 download threads
  • 1465 .ts files to download
  • 9 files failed (at the end of process only 1 download thread was downloading remaining parts)
    image

image

App needs to check if some of .ts files didn't lock the whole download process

UPDATE:
actually I left the downloader running with status as above and the process finished after 4 hours of staying idle. So probably those threads did timed out and redownloaded the missing parts. Maybe timeout should've happend earlier or download speed should be verified to check if thread is stuck?

9 errors were encountered while downloading: Video part 800.ts failed after 10 retries at MoveNext
The following parts will be redownloaded: 412.ts, 795.ts, 796.ts, 797.ts, 798.ts, 800.ts, 807.ts, 1443.ts, 1446.ts
9 errors were encountered while downloading: Video part 412.ts failed after 10 retries at MoveNext

@ScrubN
Copy link
Collaborator

ScrubN commented Aug 11, 2023

actually I left the downloader running with status as above and the process finished after 4 hours of staying idle. So probably those threads did timed out and redownloaded the missing parts. Maybe timeout should've happend earlier

Thank you for leaving it idle! I added some verification logic in #756 in case downloads were interrupted.

It's annoying that it supposedly took hours though, in theory it should have taken at most 8¼ minutes to timeout all 9 parts (assuming 1 thread). I will reduce the timeout period since we now have redownloading partially implemented.

@ScrubN
Copy link
Collaborator

ScrubN commented Aug 12, 2023

I have found the true source of the 99% stop. When the request to Twitch is closed before the video file is completely written, the content stream is no longer receiving data but still remains open. In theory this would let the cancel button still work and in practice it does.
After a long enough wait I was also able to reproduce the unexpected resume. I'm honestly not really sure why it happens, maybe the httpclient just randomly retries? At any rate, we need a way of detecting when this happens to retry immediately.

@c3Vw
Copy link

c3Vw commented Aug 12, 2023

I am able to reproduce this problem pretty often. How can I help to debug and solve this issue?
I'm not C# dev, but i tried some debugging on my own and the idea I come up with is to check in DownloadVideoPartAsync whether file part size is greater than 0 after download thread was running for 5 seconds. It's dirty solution which was slowing down the whole process, but I was able to identify video parts that were stuck. As far as I see, the generic CopyToAsync doesn't provide any progress to validate actual speed.
Furthemore this issue is not identified by properly by try-catch as the HttpClient doesn't throw timeout exception - it's the whole thread that is stuck. Maybe adding manual validation for task would work (https://stackoverflow.com/questions/4238345/asynchronously-wait-for-taskt-to-complete-with-timeout)

It's just basic idea, after playing with the code for couple hours

@ScrubN
Copy link
Collaborator

ScrubN commented Aug 12, 2023

I am able to reproduce this problem pretty often

Is your internet very unstable? I know others experience it too, but I was only able to reproduce it myself by disconnecting from the internet for 120 seconds.

the idea I come up with is to check in DownloadVideoPartAsync whether file part size is greater than 0 after download thread was running for 5 seconds

The cache files are not actually 0KB, file explorer just has not indexed the file yet because it is still opened by TwitchDownloader. If you open the file properties window you will see it reports the true size on disk. Additionally, "just check after x seconds" means spawning a background thread for each download thread which is a huge waste of resources. If we had just one download thread then I wouldn't really mind, but when users have 20+ threads per download it's a problem.

As far as I see, the generic CopyToAsync doesn't provide any progress to validate actual speed.

I am aware and wrote an extension method to resolve this for the clip downloader.

Furthemore this issue is not identified by properly by try-catch as the HttpClient doesn't throw timeout exception - it's the whole thread that is stuck

The HttpClient will throw a timeout exception when the request is not answered within the timeout period. Once we begin downloading the file to the disk, the HttpClient is not responsible for the response message.

Maybe adding manual validation for task would work (https://stackoverflow.com/questions/4238345/asynchronously-wait-for-taskt-to-complete-with-timeout)

That stackoverflow implementation is disgusting and wastes resources. I have a few better ideas that should work while consuming less resources.

@ScrubN
Copy link
Collaborator

ScrubN commented Aug 16, 2023

I have an implementation that verifiably solves the problem, but it may impact users with very slow internet (<1.3Mbps (160KB/s) for 1080p60 downloads). I have a WIP alternative implementation that monitors the download speed of each thread, but it's causing some extremely weird behavior and I don't understand why.

I will probably just double the timeout such that it will only affect users with extremely slow internet (<600Kbps (75KB/s) for 1080p60 downloads). According to worldpopulationreview.com, this may affect a handful countries if users are 800%+ behind the average broadband speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants