Hello Django community
I have an issue here I am not sure how to approach.
I run a Django website in production, everything runs well for a while, thousands of hits, all good.
Then during weekends, there is an authorized third party that use something like burpsuite to scan my site. It scans for a while, my site works until eventually, it crash, generating web 500 errors. Touching wsgi.py restart the site and all is good until next scan.
The cause of the error 500 is psycopg not being present as a module:
[Sat Mar 30 08:49:28.944928 2024] [wsgi:error] [pid 739152] [remote this-ip] mod_wsgi (pid=739152): Failed to exec Python script file mysite/www/wsgi.py'.
[Sat Mar 30 08:49:28.944993 2024] [wsgi:error] [pid 739152] [remote this-ip] mod_wsgi (pid=739152): Exception occurred processing WSGI script mysite/www/wsgi.py'.
[Sat Mar 30 08:49:28.966613 2024] [wsgi:error] [pid 739152] [remote this-ip] Traceback (most recent call last):
[Sat Mar 30 08:49:28.966660 2024] [wsgi:error] [pid 739152] [remote this-ip] File "mysite/venv/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 25, in <module>
[Sat Mar 30 08:49:28.966665 2024] [wsgi:error] [pid 739152] [remote this-ip] import psycopg as Database
[Sat Mar 30 08:49:28.966683 2024] [wsgi:error] [pid 739152] [remote this-ip] ModuleNotFoundError: No module named 'psycopg'
if I look at the error in base.py, line 25:
try:
try:
import psycopg as Database # line 25
except ImportError:
import psycopg2 as Database
except ImportError:
raise ImproperlyConfigured("Error loading psycopg2 or psycopg module")
So this looks for either psycopg or psycopg2; we use psycopg2; pip list show that is is indeed installed:
psycopg2 2.9.6
yet at line 25 when trying to import psycopg and is not trying the except: for psycopg2
also I have many errors related to psycopg during the same event until I touch wsgi.py, some of these errors are reaching line 27 of base.py with an output like this
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module
I am not sure how to approach this, since this happens when there is a scan, and else works well the rest of the time. It feels like if the venv is ‘shutting down somewhat’ and another default system venv may not have the modules installed ? But why is it shutting down in the first place?
As a remark, about the queries in the apache log that are apparently breaking the site : if I try to replay the queries myself using curl, with the referrer, user-agent and all this, it will be handled well (redirection, page not found and no error 500)
As it is sporadic (weekly) it is difficult to investigate - is there any tricks with the logging that could improve the details levels (presently at DEBUG level) or other way to monitor the health of the venv maybe?
any tips would greatly help, thank you