Python 3.12, wsgi-lsapi, and Django

#1
After spending several hours attempting to get this working, I have come to the conclusion that there appears to be a bug with the wsgi-lsapi applet. I'll begin by showing you what the stderr.log produces upon every request:

Python:
Traceback (most recent call last):
  File "/var/www/example.com/myapp/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/core/wsgi.py", line 1, in <module>
    import django
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/__init__.py", line 1, in <module>
    from django.utils.version import get_version
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/utils/version.py", line 4, in <module>
    import subprocess
  File "/usr/lib/python3.12/subprocess.py", line 46, in <module>
    import locale
  File "/var/www/example.com/myapp/locale.py", line 1, in <module>
    from django.utils.translation import gettext_lazy, gettext_noop, ngettext_lazy
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/utils/translation/__init__.py", line 8, in <module>
    from django.utils.autoreload import autoreload_started, file_changed
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/utils/autoreload.py", line 19, in <module>
    from django.core.signals import request_finished
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/core/signals.py", line 1, in <module>
    from django.dispatch import Signal
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/dispatch/__init__.py", line 9, in <module>
    from django.dispatch.dispatcher import Signal, receiver  # NOQA
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/example.com/.venv/lib/python3.12/site-packages/django/dispatch/dispatcher.py", line 1, in <module>
    import asyncio
  File "/usr/lib/python3.12/asyncio/__init__.py", line 8, in <module>
    from .base_events import *
  File "/usr/lib/python3.12/asyncio/base_events.py", line 41, in <module>
    from . import events
  File "/usr/lib/python3.12/asyncio/events.py", line 211, in <module>
    class AbstractEventLoop:
  File "/usr/lib/python3.12/asyncio/events.py", line 519, in AbstractEventLoop
    stdin=subprocess.PIPE,
          ^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'subprocess' has no attribute 'PIPE' (most likely due to a circular import)
[UID:65534][246060] packetLen < 0

The error,
Code:
AttributeError: partially initialized module 'subprocess' has no attribute 'PIPE' (most likely due to a circular import)
, does not occur under any other context. When I run my project manually with python or use Apache and mod_wsgi, everything is fine. I have ensured that wsgi-lsapi was compiled for the correct version of python that I'm using, as I did like so after extracting the source:

Bash:
python3.12 ./configure.py
make
cp lswsgi /usr/local/lsws/fcgi-bin/

My context within my virtual host looks as follows:

Code:
Type = App Server
URI = /
Location = /var/www/example.com
Binary Path = /usr/local/lsws/fcgi-bin/lswsgi
Application Type = WSGI
Startup File = myapp/wsgi.py
Environment = PYTHONPATH=/var/www/example.com:/var/www/example.com/myapp:/var/www/example.com/.venv/lib/python3.12
Environment = LS_PYTHONBIN=/var/www/example.com/.venv/bin/python3.12

This is all following the official documentation practically verbatim found here https://docs.openlitespeed.org/config/appserver/python/. I experimented to see if it was possible to have uWSGI as an external applet to serve my project through OpenLiteSpeed, however this appears to be unsupported to the best of my knowledge. I would prefer to use wsgi-lsapi if at all possible. I have not experimented with python versions less than 3.12, although if it was working for earlier versions, I would assume it is something particular with newer python builds.
 
#2
I should probably mention my specs:

OS: Ubuntu 22.04
Hardware: 12 CPU / 12GB RAM VPS
OLS version: 1.7.19
Django version: 5.1
Python version: 3.12
 
#3
This issue is solved. Here was the problem:

wsgi-lsapi was quietly changing working directories to /var/www/example.com/myapp, which had a file named locale.py in it (thus interrupting a similarly named file from being loaded from the stdlib). I consider this a bug as the location variable under the context setting was specifically set as /var/www/example.com. I'll mention, this was after updating my PYTHONPATH to only /var/www/example.com/.venv/lib/python3.12.

How was this solved? I used os.chdir before any important imports in my wsgi.py script.

After this, I encountered a second strange issue. For some reason, it simply would not launch Python with the correct locale (UTF-8), and thus some packages were unable to load. This was despite localectl showing UTF-8 as the system locale. I had to add LANG, LC_ALL, and LC_CTYPE as environment variables to correct this.

I would also note that restarting OLS does not terminate the wsgi-lsapi binary, and will even reconnect to an old one. I discovered I had to manually kill wsgi-lsapi between server restarts to get these changes to reflect.

In any case, this issue is resolved. I hope this helps someone else in the future.
 
Top