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

Download does not automatically recover when the connection times out #4240

Closed
IGTHORN opened this issue Nov 19, 2014 · 10 comments
Closed

Download does not automatically recover when the connection times out #4240

IGTHORN opened this issue Nov 19, 2014 · 10 comments

Comments

@IGTHORN
Copy link

@IGTHORN IGTHORN commented Nov 19, 2014

I set youtube-dl to download a youtube playlist overnight. During the night the connection times out and youtube-dl sits idle for a long time until eventually the connection times out.

This might be due to some minor packet loss on my line:
Ping statistics for 8.8.8.8: Packets: Sent = 200, Received = 193, Lost = 7 (3% loss),

I ran it last night with --verbose and got the following output:

D:\youtube-dl>youtube-dl -U
youtube-dl is up-to-date (2014.11.16)


D:\youtube-dl>youtube-dl.exe --verbose --retries 999 --download-archive _test10202.txt
 https://www.youtube.com/playlist?list=PLkT4HgHxrZK9YInmLpeylxsTp5zM3IJHR
[debug] System config: []
[debug] User config: []
[debug] Command-line args: ['--verbose', '--retries', '999', '--download-archive', '_test10202.txt', 'https://
www.youtube.com/playlist?list=PLkT4HgHxrZK9YInmLpeylxsTp5zM3IJHR']
[debug] Encodings: locale cp1252, fs mbcs, out cp437, pref cp1252
[debug] youtube-dl version 2014.11.16
[debug] Python version 2.7.8 - Windows-7-6.1.7601-SP1
[debug] exe versions: none
[debug] Proxy map: {}
[youtube:playlist] PLkT4HgHxrZK9YInmLpeylxsTp5zM3IJHR: Downloading webpage
[download] Downloading playlist: test10202-deletable
[youtube:playlist] playlist test10202-deletable: Collected 46 video ids (downloading 46 of them)
[download] Downloading video #1 of 46
[youtube] Confirming age
[youtube] MtY2WGfquDk: Downloading webpage
[youtube] MtY2WGfquDk: Downloading video info webpage
[youtube] MtY2WGfquDk: Extracting video information
[youtube] MtY2WGfquDk: Downloading DASH manifest
[debug] Invoking downloader on u'https://r1---sn-woc7en7z.googlevideo.com/videoplayback?ms=au&initcwndbps=1387
50&mv=m&sparams=id%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Csource%2Cupn%
2Cexpire&source=youtube&sver=3&mt=1416350398&fexp=904846%2C907259%2C916614%2C919330%2C927622%2C931350%2C932404
%2C936117%2C938692%2C942491%2C943909%2C947209%2C947215%2C947221%2C948116%2C948124%2C952302%2C952605%2C952901%2
C953912%2C957103%2C957105%2C957201&itag=22&upn=SDjNjywZtrg&mm=31&signature=48E6D15D502BCE56EB165E56E4CF65C2E24
4F5EB.B9DDB2DBC9A6A4D3FADAD814119C0C6764AE62B2&ipbits=0&expire=1416372101&id=o-AMdzPK1EhlLqc24grYRSJpujyJc3TCX
5cSj_pVufKbEv&key=yt5&ratebypass=yes&requiressl=yes&ip=197.214.116.29'
[download] Destination: Classics - Planescape Torment (Windows)-MtY2WGfquDk.mp4
[download] 100% of 283.36MiB in 40:55
[download] Downloading video #2 of 46
[youtube] 72us6pnbEvE: Downloading webpage
[youtube] 72us6pnbEvE: Downloading video info webpage
[youtube] 72us6pnbEvE: Extracting video information
[youtube] 72us6pnbEvE: Downloading DASH manifest
[debug] Invoking downloader on u'https://r2---sn-woc7en7z.googlevideo.com/videoplayback?id=o-AEwsEhH1Bm5VbPH46
TqhipIHki8_Lau33ym5H6tFyNWY&upn=YdBIgQ738Eo&requiressl=yes&ms=au&mt=1416352917&ratebypass=yes&mv=m&sparams=id%
2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&source=you
tube&mm=31&signature=D135764C334B4E017270E45AFDEB8F1CA952EF39.93D41F6D24EB7729DD376979E37B89B7DC75A7EA&key=yt5
&fexp=904846%2C907259%2C916614%2C919330%2C927622%2C931350%2C932404%2C936117%2C938692%2C942491%2C943909%2C94720
9%2C947215%2C947221%2C948116%2C948124%2C952302%2C952605%2C952901%2C953912%2C957103%2C957105%2C957201&sver=3&it
ag=18&expire=1416374574&ip=197.214.116.29&ipbits=0&initcwndbps=70000'
[download] Destination: Richard P Feynman - Quantum Mechanical View of Reality 1 (Part 1)-72us6pnbEvE.mp4
[download] 100% of 78.68MiB in 11:31
[download] Downloading video #3 of 46
[youtube] cQBuB-J4nI4: Downloading webpage
[youtube] cQBuB-J4nI4: Downloading video info webpage
[youtube] cQBuB-J4nI4: Extracting video information
[youtube] cQBuB-J4nI4: Downloading DASH manifest
[debug] Invoking downloader on u'https://r2---sn-woc7en76.googlevideo.com/videoplayback?requiressl=yes&ms=au&m
v=m&ipbits=0&initcwndbps=82500&ip=197.214.116.29&source=youtube&id=o-ABZcxRiPRjSWGGO9oj2IqW6sBo83JKja0xqWDmsTq
ovz&expire=1416375274&key=yt5&mm=31&fexp=904846%2C907259%2C916614%2C919330%2C927622%2C931350%2C932404%2C936117
%2C938692%2C942491%2C943909%2C947209%2C947215%2C947221%2C948116%2C948124%2C952302%2C952605%2C952901%2C953912%2
C957103%2C957105%2C957201&itag=22&mt=1416353610&upn=1ClEWtFQa30&sver=3&ratebypass=yes&sparams=id%2Cinitcwndbps
%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&signature=0C6DFA191C588
69927B641FA23BFF73FE1BC6F0D.F93D8AD6D964B8B044A9BA0FEA9A3AB04EAFC29C'
[download] Destination: Dota 2 Song of the Siren Roshan Steal (XMG Captains Draft 2.0)-cQBuB-J4nI4.mp4
[download] 100% of 24.39MiB in 04:46
[download] Downloading video #4 of 46
[youtube] 0KBFxnK8DEw: Downloading webpage
[youtube] 0KBFxnK8DEw: Downloading video info webpage
[youtube] 0KBFxnK8DEw: Extracting video information
[youtube] 0KBFxnK8DEw: Downloading DASH manifest
[debug] Invoking downloader on u'https://r1---sn-woc7en76.googlevideo.com/videoplayback?key=yt5&ip=197.214.116
.29&mm=31&itag=22&id=o-AHoWBfHsQ3OS366FvxwwqMNoogvmHJPuNXDFGNLCF31y&ms=au&mt=1416353883&initcwndbps=102500&mv=
m&expire=1416375566&signature=971A3255D8BE0EEE20AE54407311D8ABC3B1A5F4.6D5C71650F42D47225740C0847866E6B204867E
2&upn=w2HpZqhi5YY&sparams=id%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Csou
rce%2Cupn%2Cexpire&ipbits=0&requiressl=yes&ratebypass=yes&fexp=904846%2C907259%2C916614%2C919330%2C927622%2C93
1350%2C932404%2C936117%2C938692%2C942491%2C943909%2C947209%2C947215%2C947221%2C948116%2C948124%2C952302%2C9526
05%2C952901%2C953912%2C957103%2C957105%2C957201&sver=3&source=youtube'
[download] Destination: Dota 2 Headshot v58.0-0KBFxnK8DEw.mp4
[download] 100% of 60.47MiB in 11:23
[download] Downloading video #5 of 46
[youtube] gOSohcDcws0: Downloading webpage
[youtube] gOSohcDcws0: Downloading video info webpage
[youtube] gOSohcDcws0: Extracting video information
[youtube] gOSohcDcws0: Downloading DASH manifest
[debug] Invoking downloader on u'https://r1---sn-q4f7dnez.googlevideo.com/videoplayback?ip=197.214.116.29&ms=a
u&mv=m&source=youtube&key=yt5&id=o-ADmSn_Hn6qx-mK2VjeOae6DpPx9NNubltVFQgEgESDk5&fexp=904846%2C907259%2C916614%
2C919330%2C927622%2C931350%2C932404%2C936117%2C938692%2C942491%2C943909%2C947209%2C947215%2C947221%2C948116%2C
948124%2C952302%2C952605%2C952901%2C953912%2C957103%2C957105%2C957201&mt=1416354587&ipbits=0&mm=31&expire=1416
376257&initcwndbps=122500&sver=3&requiressl=yes&sparams=id%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2
Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&upn=d7ySmZ3-Dms&itag=22&signature=06863E9FB2DFD9F4382A392E71D
F136C0E761CD8.5B7C212F3745096415B2C6305A996D85A9F79AD3&ratebypass=yes'
[download] Destination: Dota 2 - XMG Captains Draft 2.0 - Evil Geniuses vs Team Secret - Game 1-gOSohcDcws0.mp4
[download]   2.0% of 1.42GiB at 52.62KiB/s ETA 07:40:47 ERROR: unable to download video data: The read operation timed out
Traceback (most recent call last):
  File "youtube_dl\YoutubeDL.pyo", line 1064, in process_info
  File "youtube_dl\YoutubeDL.pyo", line 1040, in dl
  File "youtube_dl\downloader\common.pyo", line 292, in download
  File "youtube_dl\downloader\http.pyo", line 140, in real_download
  File "socket.pyo", line 380, in read
  File "httplib.pyo", line 567, in read
  File "socket.pyo", line 380, in read
  File "ssl.pyo", line 246, in recv
  File "ssl.pyo", line 165, in read
SSLError: The read operation timed out

I captured another instance, verbose output trimmed to the stack trace and single video:

[download] Downloading video #18 of 46
[youtube] kf6rTOQdlLg: Downloading webpage
[youtube] kf6rTOQdlLg: Downloading video info webpage
[youtube] kf6rTOQdlLg: Extracting video information
[youtube] kf6rTOQdlLg: Downloading DASH manifest
[debug] Invoking downloader on u'https://r1---sn-woc7en76.googlevideo.com/videoplayback?id=o-AO8vuBF_tLYuxwlGI
sy3RGIWUQjxK4Xd1J65mh2k7um6&signature=975549B152F69BC0F8B9A8C8A0A99055C46CA706.7D97DA8335EC70CA1532AB85AC6EDE0
0BB91A7CB&key=yt5&mm=31&fexp=907259%2C914075%2C924637%2C927622%2C932404%2C936118%2C937432%2C938689%2C943909%2C
947209%2C947215%2C948124%2C952302%2C952605%2C952901%2C953912%2C957103%2C957105%2C957201%2C959201&ip=197.214.11
6.29&requiressl=yes&ms=au&source=youtube&mv=m&ipbits=0&initcwndbps=212500&ratebypass=yes&sver=3&upn=f-ocBNeD8a
I&expire=1416417705&sparams=id%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Cs
ource%2Cupn%2Cexpire&mt=1416396074&itag=22'
[download] Destination: Dota 2 Symphony of Skills 55-kf6rTOQdlLg.mp4
[download]  44.8% of 47.30MiB at 23.91KiB/s ETA 18:39 ERROR: unable to download video data: The read operation timed out
Traceback (most recent call last):
  File "youtube_dl\YoutubeDL.pyo", line 1064, in process_info
  File "youtube_dl\YoutubeDL.pyo", line 1040, in dl
  File "youtube_dl\downloader\common.pyo", line 292, in download
  File "youtube_dl\downloader\http.pyo", line 140, in real_download
  File "socket.pyo", line 380, in read
  File "httplib.pyo", line 567, in read
  File "socket.pyo", line 380, in read
  File "ssl.pyo", line 246, in recv
  File "ssl.pyo", line 165, in read
SSLError: The read operation timed out
@goiken
Copy link

@goiken goiken commented Nov 18, 2015

Can confirm the issue.

@sehrgut
Copy link
Contributor

@sehrgut sehrgut commented Jan 10, 2016

I was just searching to see if this issue already existed before reporting it. I can confirm it happens for me as well

Owner@sehrbook /cygdrive/e/Videos
$ youtube-dl.exe --retries="infinite" --verbose 'https://www.facebook.com/100006878506221/videos/1679312832307984/'
[debug] System config: []
[debug] User config: []
[debug] Command-line args: [u'--retries=infinite', u'--verbose', u'https://www.facebook.com/100006878506221/videos/1679312832307984/']
[debug] Encodings: locale cp1252, fs mbcs, out None, pref cp1252
[debug] youtube-dl version 2016.01.09
[debug] Python version 2.7.10 - Windows-8-6.2.9200
[debug] exe versions: none
[debug] Proxy map: {}
[facebook] 1679312832307984: Downloading webpage
[debug] Invoking downloader on u'https://video-iad3-1.xx.fbcdn.net/hvideo-xfp1/v/t43.1792-2/12451028_953909387979006_2090296038_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InN2ZV9oZCJ9&oh=f18edad53ab719c011075dd496430c07&oe=5691EE97'
[download] Resuming download at byte 2167250
[download] Destination: Un p'tit Jive, ce rock Am▒ricain tr▒s vivant, et en plein Paris svp, sur l'es...-1679312832307984.mp4
[download]  31.5% of 9.90MiB at  8.38KiB/s ETA 13:48   ERROR: unable to download video data: ('The read operation timed out',)
Traceback (most recent call last):
  File "youtube_dl\YoutubeDL.pyo", line 1609, in process_info
  File "youtube_dl\YoutubeDL.pyo", line 1551, in dl
  File "youtube_dl\downloader\common.pyo", line 342, in download
  File "youtube_dl\downloader\http.pyo", line 162, in real_download
  File "socket.pyo", line 384, in read
  File "httplib.pyo", line 612, in read
  File "socket.pyo", line 384, in read
  File "ssl.pyo", line 734, in recv
  File "ssl.pyo", line 621, in read
SSLError: ('The read operation timed out',)

This is exactly the sort of situation retrying is supposed to handle. However, it appears that http.py isn't expecting SSLError to be recoverable, when often they are. Not all SSLError will be cryptographic problems, but it appears the only nod to this is that the catch of socket.error (SSLError's superclass) retries ECONNRESET.

I would recommend handling SSLError separately, since only the subtype CertificateError is unrecoverable (i.e. not network-based). Rather than handling SSLError as a socket.error, which assumes that the only recoverable socket error is ECONNRESET, the fact that many SSLError are transient network problems should allow a more nuanced handling of of SSLError.

Specifically, the OpenSSL docs state explicitly that the following errno are retriable ("The TLS/SSL I/O function should be called again later."):

SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE, SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT, SSL_ERROR_WANT_X509_LOOKUP, SSL_ERROR_WANT_ASYNC

SSL_ERROR_SYSCALL is possibly retriable, but the Python stdlib provides no way to inspect the error more deeply. Still, this should probably be retried by default as long as retries != infinite, since if it's not retriable (say EMFILE at the OS level), it will fail quickly and run through the retries with no harm done.

SSL_ERROR_SSL is the only one that is probably not retriable, since it would likely be a crypto problem due to mismatched certs or a misconfigured server. Still, the error has enough detail that it is (I believe) broken out into a couple different exception types in Python, one of which being CertificateError.

@weedy
Copy link

@weedy weedy commented Nov 26, 2016

youtube-dl obviously has a retry mechanism.

[download]  79.2% of ~9.08GiB at  1.68MiB/s ETA 03:12[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 1 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 2 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 3 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 4 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 5 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 6 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 7 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 8 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 9 of 10)...
[download] Got server HTTP error: HTTP Error 404: Not Found. Retrying fragment Frag6952 (attempt 10 of 10)...
[download] Skipping fragment Frag6952...

It just doesn't catch everything

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/youtube_dl/YoutubeDL.py", line 1699, in process_info
    success = dl(filename, info_dict)
  File "/usr/lib/python2.7/site-packages/youtube_dl/YoutubeDL.py", line 1641, in dl
    return fd.download(name, info)
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/common.py", line 354, in download
    return self.real_download(filename, info_dict)
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/hls.py", line 115, in real_download
    success = ctx['dl'].download(frag_filename, {'url': frag_url})
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/common.py", line 354, in download
    return self.real_download(filename, info_dict)
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/http.py", line 61, in real_download
    data = self.ydl.urlopen(request)
  File "/usr/lib/python2.7/site-packages/youtube_dl/YoutubeDL.py", line 2001, in urlopen
    return self._opener.open(req, timeout=self._socket_timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/site-packages/youtube_dl/utils.py", line 1034, in https_open
    req, **kwargs)
  File "/usr/lib/python2.7/urllib2.py", line 1200, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1136, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 453, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 409, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/lib/python2.7/socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)
  File "/usr/lib/python2.7/ssl.py", line 734, in recv
    return self.read(buflen)
  File "/usr/lib/python2.7/ssl.py", line 621, in read
    v = self._sslobj.read(len or 1024)
SSLError: ('The read operation timed out',)
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/youtube_dl/YoutubeDL.py", line 1699, in process_info
    success = dl(filename, info_dict)
  File "/usr/lib/python2.7/site-packages/youtube_dl/YoutubeDL.py", line 1641, in dl
    return fd.download(name, info)
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/common.py", line 354, in download
    return self.real_download(filename, info_dict)
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/hls.py", line 115, in real_download
    success = ctx['dl'].download(frag_filename, {'url': frag_url})
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/common.py", line 354, in download
    return self.real_download(filename, info_dict)
  File "/usr/lib/python2.7/site-packages/youtube_dl/downloader/http.py", line 61, in real_download
    data = self.ydl.urlopen(request)
  File "/usr/lib/python2.7/site-packages/youtube_dl/YoutubeDL.py", line 2001, in urlopen
    return self._opener.open(req, timeout=self._socket_timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/site-packages/youtube_dl/utils.py", line 1034, in https_open
    req, **kwargs)
  File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
URLError: <urlopen error timed out>
@brookemahoney
Copy link

@brookemahoney brookemahoney commented Jan 26, 2017

For now, as a workaround, you can do something like the following in the terminal. The loop attempts to run the command in the getvids function up to 10 times or until it finishes successfully.

function getvids() {
  youtube-dl --playlist-items 1-3 https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re
}

for i in {1..10}; do getvids && break || sleep 15; done
@daewizal
Copy link

@daewizal daewizal commented Mar 19, 2017

jeffymahoney how to run this loop? I got SyntaxError: invalid syntax

@IGTHORN
Copy link
Author

@IGTHORN IGTHORN commented Mar 19, 2017

@daewizal That's not Python that he's using. If you're on windows, you can create a batch script to "retry". For example, put the following in a file called youtube-dl-retry.cmd

:retry
youtube-dl.exe --verbose --retries 999 https://www.youtube.com/watch?v=YkygAoXqP4A
IF NOT "%ERRORLEVEL%"=="0" GOTO :retry
@echo COMPLETE, ERRORLEVEL = %ERRORLEVEL%
pause

This will keep on running the youtube-dl command until it succeeds.

@daewizal
Copy link

@daewizal daewizal commented Mar 19, 2017

@IGTHORN thank you!

@nipunsadvilkar
Copy link

@nipunsadvilkar nipunsadvilkar commented Mar 31, 2017

@IGTHORN : could you also please provide shell script for same youtube-dl-retry.sh?

@IGTHORN
Copy link
Author

@IGTHORN IGTHORN commented Apr 1, 2017

@nipunsadvilkar The script posted by jeffymahoney is a shell script. You can create a file called youtube-dl-retry.sh and paste in his script. Just change the line with youtube-dl so that it contains the video you want to download. Jeff's script will only retry 10 times, so if you want it to retry more than that, change the for loop line to: for i in {1..1000}; do getvids && break || sleep 15; done

@nipunsadvilkar
Copy link

@nipunsadvilkar nipunsadvilkar commented Apr 3, 2017

@IGTHORN , @jeffymahoney : Thank you guys! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.