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

🐛 Fix SSL: CERTIFICATE_VERIFY_FAILED error #262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nithinmanoj10
Copy link

@nithinmanoj10 nithinmanoj10 commented Dec 28, 2023

tl;dr

Made the fix for SSL: CERTIFICATE_VERIFY_FAILED error that is encountered while downloading the dataset for each dataloader. This is fixed by passing an unverified SSL context to the urllib.request.urlopen() method.


This pull request is in regards to the following issues #255, #243 and #236. I had also faced a similar issue when trying to download the datasets for the dataloader.

Reproducing the error

Ran the following Python script to load the METRLADatasetLoader

import torch_geometric_temporal
from torch_geometric_temporal.dataset import METRLADatasetLoader

metrla = METRLADatasetLoader()

And encountered the following error

Traceback (most recent call last):
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/urllib/request.py", line 1348, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/http/client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/http/client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/http/client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/http/client.py", line 1037, in _send_output
    self.send(msg)
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/http/client.py", line 975, in send
    self.connect()
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/http/client.py", line 1454, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/ssl.py", line 1071, in _create
    self.do_handshake()
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/ssl.py", line 1342, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/c/Nithin stuff/Open Source Contributions/pytorch_geometric_temporal/torch_geometric_temporal/dataset/metr_la.py", line 25, in __init__
    self._read_web_data()
  File "/mnt/c/Nithin stuff/Open Source Contributions/pytorch_geometric_temporal/torch_geometric_temporal/dataset/metr_la.py", line 41, in _read_web_data
    self._download_url(url, os.path.join(self.raw_data_dir, "METR-LA.zip"))
  File "/mnt/c/Nithin stuff/Open Source Contributions/pytorch_geometric_temporal/torch_geometric_temporal/dataset/metr_la.py", line 28, in _download_url
    with urllib.request.urlopen(url) as dl_file:
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/urllib/request.py", line 519, in open
    response = self._open(req, data)
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/urllib/request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/urllib/request.py", line 496, in _call_chain
    result = func(*args)
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/urllib/request.py", line 1391, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/home/nithin/anaconda3/envs/seastar-new/lib/python3.10/urllib/request.py", line 1351, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)>

And noticed that the tests for the dataset loaders were failing due to the similar error

pygt-pr-test

The fix for the error

The fix was inspired by what PyTorch Geometric had done to bypass the SSL: CERTIFICATE_VERIFY_FAILED error. All they did was pass an unverified SSL context to the urllib.request.urlopen() method call.

context = ssl._create_unverified_context()
self._dataset = json.loads(
    urllib.request.urlopen(url, context=context).read()
)

The PyTorch Geometric code were they did that to download the datasets can be found here.

A similar solution was also mentioned in a comment inside issue #236.

And upon running the tests, we are not encountering the previous errors anymore

pygt-pr-test-fix

Made the fix for SSL: CERTIFICATE_VERIFY_FAILED error that is encountered while downloading the dataset for each dataloader. This is fixed by passing an unverified SSL context to the urllib.request.urlopen() method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant