Repository: youtube-dl
Issue: [Youtube] ERROR: unable to download video data: <urlopen error [SSL: INVALID_SESSION_ID] invalid session id
Labels: request,external-bugs

<!--

######################################################################
  WARNING!
  IGNORING THE FOLLOWING TEMPLATE WILL RESULT IN ISSUE CLOSED AS INCOMPLETE
######################################################################

-->


## Checklist

<!--
Carefully read and work through this check list in order to prevent the most common mistakes and misuse of youtube-dl:
- First of, make sure you are using the latest version of youtube-dl. Run `youtube-dl --version` and ensure your version is 2021.12.17. If it's not, see https://yt-dl.org/update on how to update. Issues with outdated version will be REJECTED.
- Make sure that all provided video/audio/playlist URLs (if any) are alive and playable in a browser.
- Make sure that all URLs and arguments with special characters are properly quoted or escaped as explained in http://yt-dl.org/escape.
- Search the bugtracker for similar issues: http://yt-dl.org/search-issues. DO NOT post duplicates.
- Read bugs section in FAQ: http://yt-dl.org/reporting
- Finally, put x into all relevant boxes (like this [x])
-->

- [ ] I'm reporting a broken site support issue
- [x] I've verified that I'm running youtube-dl version **2021.12.17**
- [x] I've checked that all provided URLs are alive and playable in a browser
- [x] I've checked that all URLs and arguments with special characters are properly quoted or escaped
- [x] I've searched the bugtracker for similar bug reports including closed ones
- [x] I've read bugs section in FAQ


## Verbose log

<!--
Provide the complete verbose output of youtube-dl that clearly demonstrates the problem.
Add the `-v` flag to your command line you run youtube-dl with (`youtube-dl -v <your command line>`), copy the WHOLE output and insert it below. It should look similar to this:
 [debug] System config: []
 [debug] User config: []
 [debug] Command-line args: [u'-v', u'http://www.youtube.com/watch?v=BaW_jenozKcj']
 [debug] Encodings: locale cp1251, fs mbcs, out cp866, pref cp1251
 [debug] youtube-dl version 2021.12.17
 [debug] Python version 2.7.11 - Windows-2003Server-5.2.3790-SP2
 [debug] exe versions: ffmpeg N-75573-g1d0487f, ffprobe N-75573-g1d0487f, rtmpdump 2.4
 [debug] Proxy map: {}
 <more lines>
-->

```
# youtube-dl --version
2021.12.17

# youtube-dl -v https://www.youtube.com/watch\?v\=b5Kbzgx1w9A
[debug] System config: []
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: ['-v', 'https://www.youtube.com/watch?v=b5Kbzgx1w9A']
[debug] Encodings: locale UTF-8, fs utf-8, out utf-8, pref UTF-8
[debug] youtube-dl version 2021.12.17
[debug] Python version 3.8.12 (CPython) - FreeBSD-13.0-RELEASE-p4-amd64-64bit-ELF
[debug] exe versions: ffmpeg 4.4.1, ffprobe 4.4.1, rtmpdump 2.4
[debug] Proxy map: {}
[youtube] b5Kbzgx1w9A: Downloading webpage
[debug] Default format spec: bestvideo+bestaudio/best
WARNING: Requested formats are incompatible for merge and will be merged into mkv.
[debug] Invoking downloader on 'https://rr6---sn-gvnuxaxjvh-c35z.googlevideo.com/videoplayback?expire=1645740445&ei=Pa0XYrr8MPKS1d8P66qs8AY&id=o-AJb_l8PiCAQrb2XMIHE6i6_9du6G7c5uQMAWFyPNon3l&itag=248&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278&source=youtube&requiressl=yes&mh=6e&mm=31%2C29&mn=sn-gvnuxaxjvh-c35z%2Csn-n8v7kne6&ms=au%2Crdu&mv=m&mvi=6&pcm2cms=yes&pl=23&initcwndbps=707500&vprv=1&mime=video%2Fwebm&ns=e8mJx0XBp7jRzBMnM6sgYiYG&gir=yes&clen=238929645&dur=3890.386&lmt=1640041115174139&mt=1645718473&fvip=6&keepalive=yes&fexp=24001373%2C24007246&c=WEB&txp=5432434&n=WKsPPQKun4uNUbYH9&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRAIgOgShB-b6rJ6_mi2st6qwHScTl17tLL46KtJBUO3bLysCIFFPO7iraa7C52XH3aJUTxar0zI4qlTgWy6SuXnMszbx&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRgIhAMsBM-YtDgxFBmECITIrkvzMwQGBBnMqgQF208ZzHfw7AiEAsnEYaNBdmGkhqXw-SgfLSsrV0YHjkoPuHo7n1CyP_AM%3D'
ERROR: unable to download video data: <urlopen error [SSL: INVALID_SESSION_ID] invalid session id (_ssl.c:1136)>
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/urllib/request.py", line 1354, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/usr/local/lib/python3.8/http/client.py", line 1256, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.8/http/client.py", line 1302, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.8/http/client.py", line 1251, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.8/http/client.py", line 1011, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.8/http/client.py", line 951, in send
    self.connect()
  File "/usr/local/lib/python3.8/http/client.py", line 1425, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "/usr/local/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/local/lib/python3.8/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/usr/local/lib/python3.8/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: INVALID_SESSION_ID] invalid session id (_ssl.c:1136)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 1970, in process_info
    partial_success = dl(fname, new_info)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 1915, in dl
    return fd.download(name, info)
  File "/usr/local/bin/youtube-dl/youtube_dl/downloader/common.py", line 366, in download
    return self.real_download(filename, info_dict)
  File "/usr/local/bin/youtube-dl/youtube_dl/downloader/http.py", line 351, in real_download
    establish_connection()
  File "/usr/local/bin/youtube-dl/youtube_dl/downloader/http.py", line 116, in establish_connection
    raise err
  File "/usr/local/bin/youtube-dl/youtube_dl/downloader/http.py", line 110, in establish_connection
    ctx.data = self.ydl.urlopen(request)
  File "/usr/local/bin/youtube-dl/youtube_dl/YoutubeDL.py", line 2288, in urlopen
    return self._opener.open(req, timeout=self._socket_timeout)
  File "/usr/local/lib/python3.8/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.8/urllib/request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/local/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/usr/local/bin/youtube-dl/youtube_dl/utils.py", line 2735, in https_open
    return self.do_open(functools.partial(
  File "/usr/local/lib/python3.8/urllib/request.py", line 1357, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: INVALID_SESSION_ID] invalid session id (_ssl.c:1136)>
```


## Description

<!--
Provide an explanation of your issue in an arbitrary form. Please make sure the description is worded well enough to be understood, see https://github.com/ytdl-org/youtube-dl#is-the-description-of-the-issue-itself-sufficient. Provide any additional information, suggested solution and as much context and examples as possible.
If work on your issue requires account credentials please provide them or explain how one can obtain them.
-->

There is a weird [error](https://github.com/openssl/openssl/blob/acce055778ecbf72e06a254b3a9bf2a2907e5170/ssl/statem/statem_clnt.c#L1467) when trying to download a video.
It probably may be an OpenSSL issue or an OpenSSL usage issue.

I've tried to use cURL with the direct download link from the youtube-dl output and got the same error:
```
curl -v https://rr6---sn-gvnuxaxjvh-c35z.googlevideo.com/videoplayback\?expire\=1645764880\&ei\=sAwYYp3JN6zKx_APtpSliAY\&id\=o-AJy_PYFNsDoHUzK7LPdzlu3rIlGuWYSnPrC9qqjZ1Biw\&itag\=248\&aitags\=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278\&source\=youtube\&requiressl\=yes\&mh\=6e\&mm\=31%2C29\&mn\=sn-gvnuxaxjvh-c35z%2Csn-gvnuxaxjvh-n8vr\&ms\=au%2Crdu\&mv\=m\&mvi\=6\&pl\=24\&initcwndbps\=773750\&spc\=4ocVC_WmRO2YiHoePwGKqnEYeIRR\&vprv\=1\&mime\=video%2Fwebm\&ns\=eodsbipOxmRTGFMcPMTkgycG\&gir\=yes\&clen\=238929645\&dur\=3890.386\&lmt\=1640041115174139\&mt\=1645742941\&fvip\=6\&keepalive\=yes\&fexp\=24001373%2C24007246\&c\=WEB\&txp\=5432434\&n\=rjPiSFtsDQmTDYb_q\&sparams\=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cspc%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt\&sig\=AOq0QJ8wRAIgfioe8e_o6Avgi31Y_ueKckwWjPTv0UIHJri1BwMAptICIGqUpvODQOOeBiT5zv0UdHKIhpe0HU7IGhIMo0JOpSbY\&lsparams\=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps\&lsig\=AG3C_xAwRQIhANiHlxdWrdh_L9F2y571I3ynNoyqxMz5HBJRaJIs0smVAiA1M5vCwUufroN-V8rcBKZKClS3fY0tawl06RJvZ7264A%3D%3D
*   Trying 77.37.252.145:443...
* Connected to rr6---sn-gvnuxaxjvh-c35z.googlevideo.com (77.37.252.145) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
*  CAfile: /usr/local/share/certs/ca-root-nss.crt
*  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (OUT), TLS alert, illegal parameter (559):
* error:141713E7:SSL routines:tls_process_server_hello:invalid session id
* Closing connection 0
curl: (35) error:141713E7:SSL routines:tls_process_server_hello:invalid session id
```
It seems the server returns the empty answer for some reason.