SSL certificate error with python requests module while sending GCM messages

I am using python requests module to send GCM(Google Cloud Messages) messages. I am sending millions of messages in a day. But sometimes this weird error pops up
The GCM url is https://android.googleapis.com/gcm/send

`[Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib`

This generally happens when i am sending a lot of messages.
Have a look at this post too http://stackoverflow.com/questions/16699736/ssl-certificate-error-with-python-requests-module-while-sending-gcm-messages


As there are a lot of moving parts, we need some additional information:
- Could you give a complete stacktrace?
- Which version of Python?
- Which version of requests?
- Which operating system?


I am unable to generate the stacktrace right now but the rest of the details are here 

Python version 2.7.3
Requests version 1.2.0
OS  Ubuntu 12.04
Machine  AWS EC2


This is the stacktrace

Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python2.7/dist-packages/apport/**init**.py", line 1, in <module>
    from apport.report import Report
  File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
    import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
ImportError: No module named subprocess

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "send_message.py", line 522, in mongo_send_new
    resp = gcm_multi.send()
  File "gcm.py", line 20, in send
    response = requests.post(self.gcm_url, headers=self.headers, data=self.data)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 88, in post
    return request('post', url, data=data, *_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, *_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 354, in request
    resp = self.send(prep, *_send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 460, in send
    r = adapter.send(request, *_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 250, in send
    raise SSLError(e)
requests.exceptions.SSLError: [Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib


This error normally means that certificates haven't been loaded properly. I find that a bit odd, though, because it's being thrown out of urllib3, not from our internal certificate verifying code.


I would like to tell you the behavior of this error. Say i am making 10000 HTTPS requests and i am able to make 5000 successfully now when this exception comes i catch this and proceed with the next request but even the subsequent requests also throw this exception. Then i have to stop the process and start it again and after some time this exception starts coming back.
Is there some way i can get around this?


Looking at `response = requests.post(self.gcm_url, headers=self.headers, data=self.data)` you are not using a persistent session. Regarding the fact, that you are sending 10000s of messages to the same host you should use a session.
This may not solve the problem itself but you shouldn't run into it anymore as the cert is only loaded a few time (as often as a new connection is added to the underlying connectionpool).


@lovesh Does using a `Session` resolve your problem?


Yes it does.
Thanks

On Sat, Jun 8, 2013 at 1:35 PM, Cory Benfield notifications@github.comwrote:

> @lovesh https://github.com/lovesh Does using a Session resolve your
> problem?
> 
> —
> Reply to this email directly or view it on GitHubhttps://github.com/kennethreitz/requests/issues/1389#issuecomment-19145022
> .

## 

Thanks for your time


Brilliant! Let us know if you have any further problems. =)