Disclaimer: This is more of a Heroku (or general web hosting) problem, as everything works fine in localhost.
I built a flask web app which works fine when I’m using localhost, and I tried to host the site on Heroku, which resulted in an application error.
pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed:
Here’s a segment of how I referenced the MongoDB (I replaced the password with )
MONGO_URI = 'mongodb+srv://Skylier:<password>@cluster0.itxpiji.mongodb.net/sat_web?retryWrites=true&w=majority&tlsCAFile=isrgrootx1.pem'
mongo_client = MongoClient(
MONGO_URI,
socketTimeoutMS=30000,
connectTimeoutMS=30000,
tlsAllowInvalidCertificates=True
)
db = mongo_client['sat_web']
questions_collection = db['questions']
user_collection = db['user']
I am certain that the problem is with the mongodb, as I tried a version without the database with dummy data and it works fine, but after just adding these 2 lines for testing, I get the SSL handshake failed problem.
document = questions_collection.find_one({ 'openid': openid})
correct_list = document["correct"]
Things I’ve done:
- Given IP Access to the site on MongoDB Atlas
- Download the isrgrootx1.pem file and added it to the git repo (I saw another stackoverflow post so)
- Somehow pay 7 more dollars a month to Heroku to add the MongoDB URI to basic dynos
- Allow invalid tls certificates (as seen above)
- This is my first time doing this stuff and honestly idk what to do even though I’m certain it’s something to do with the MongoDB/Heroku connection.
Here’s the full error message (I actually don’t know if it’s useful sorry)
2024-07-22T06:19:10.626012+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/record" host=sat-web-06446f0c8624.herokuapp.com request_id=ee07527d-0549-4ece-9c03-7762e5b79780 fwd="123.240.32.28" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
2024-07-22T06:19:10.768710+00:00 app[web.1]: [2024-07-22 06:19:10,766] ERROR in app: Exception on /record [GET]
2024-07-22T06:19:10.768721+00:00 app[web.1]: Traceback (most recent call last):
2024-07-22T06:19:10.768722+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
2024-07-22T06:19:10.768722+00:00 app[web.1]: response = self.full_dispatch_request()
2024-07-22T06:19:10.768722+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768723+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/flask/app.py", line 882, in full_dispatch_request
2024-07-22T06:19:10.768723+00:00 app[web.1]: rv = self.handle_user_exception(e)
2024-07-22T06:19:10.768724+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768724+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/flask/app.py", line 880, in full_dispatch_request
2024-07-22T06:19:10.768724+00:00 app[web.1]: rv = self.dispatch_request()
2024-07-22T06:19:10.768725+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768725+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/flask/app.py", line 865, in dispatch_request
2024-07-22T06:19:10.768725+00:00 app[web.1]: return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
2024-07-22T06:19:10.768725+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768726+00:00 app[web.1]: File "/app/app.py", line 110, in record
2024-07-22T06:19:10.768726+00:00 app[web.1]: document = questions_collection.find_one({ 'openid': openid})
2024-07-22T06:19:10.768726+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768726+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/collection.py", line 1495, in find_one
2024-07-22T06:19:10.768726+00:00 app[web.1]: for result in cursor.limit(-1):
2024-07-22T06:19:10.768727+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/cursor.py", line 1243, in next
2024-07-22T06:19:10.768727+00:00 app[web.1]: if len(self.__data) or self._refresh():
2024-07-22T06:19:10.768727+00:00 app[web.1]: ^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768727+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/cursor.py", line 1160, in _refresh
2024-07-22T06:19:10.768728+00:00 app[web.1]: self.__send_message(q)
2024-07-22T06:19:10.768728+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/cursor.py", line 1039, in __send_message
2024-07-22T06:19:10.768728+00:00 app[web.1]: response = client._run_operation(
2024-07-22T06:19:10.768728+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768784+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/_csot.py", line 108, in csot_wrapper
2024-07-22T06:19:10.768784+00:00 app[web.1]: return func(self, *args, **kwargs)
2024-07-22T06:19:10.768785+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768785+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/mongo_client.py", line 1431, in _run_operation
2024-07-22T06:19:10.768785+00:00 app[web.1]: return self._retryable_read(
2024-07-22T06:19:10.768785+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768786+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/mongo_client.py", line 1540, in _retryable_read
2024-07-22T06:19:10.768786+00:00 app[web.1]: return self._retry_internal(
2024-07-22T06:19:10.768786+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768786+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/_csot.py", line 108, in csot_wrapper
2024-07-22T06:19:10.768786+00:00 app[web.1]: return func(self, *args, **kwargs)
2024-07-22T06:19:10.768787+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768787+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/mongo_client.py", line 1507, in _retry_internal
2024-07-22T06:19:10.768787+00:00 app[web.1]: ).run()
2024-07-22T06:19:10.768787+00:00 app[web.1]: ^^^^^
2024-07-22T06:19:10.768788+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/mongo_client.py", line 2353, in run
2024-07-22T06:19:10.768788+00:00 app[web.1]: return self._read() if self._is_read else self._write()
2024-07-22T06:19:10.768789+00:00 app[web.1]: ^^^^^^^^^^^^
2024-07-22T06:19:10.768790+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/mongo_client.py", line 2483, in _read
2024-07-22T06:19:10.768790+00:00 app[web.1]: self._server = self._get_server()
2024-07-22T06:19:10.768790+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768790+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/mongo_client.py", line 2439, in _get_server
2024-07-22T06:19:10.768791+00:00 app[web.1]: return self._client._select_server(
2024-07-22T06:19:10.768791+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768791+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/mongo_client.py", line 1322, in _select_server
2024-07-22T06:19:10.768791+00:00 app[web.1]: server = topology.select_server(
2024-07-22T06:19:10.768791+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768792+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/topology.py", line 368, in select_server
2024-07-22T06:19:10.768792+00:00 app[web.1]: server = self._select_server(
2024-07-22T06:19:10.768792+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768793+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/topology.py", line 346, in _select_server
2024-07-22T06:19:10.768793+00:00 app[web.1]: servers = self.select_servers(
2024-07-22T06:19:10.768793+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768793+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/topology.py", line 253, in select_servers
2024-07-22T06:19:10.768793+00:00 app[web.1]: server_descriptions = self._select_servers_loop(
2024-07-22T06:19:10.768794+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-22T06:19:10.768794+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/topology.py", line 303, in _select_servers_loop
2024-07-22T06:19:10.768794+00:00 app[web.1]: raise ServerSelectionTimeoutError(
2024-07-22T06:19:10.768801+00:00 app[web.1]: pymongo.errors.ServerSelectionTimeoutError: SSL handshake failed: ac-ew71uet-shard-00-01.itxpiji.mongodb.net:27017: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1000)
Any ideas on what I can try will be much appreciated ;-;
(Also feel free to question how I did some of the stuff I already done as I don’t really know if I done them correctly)