GrahamDumpleton / mod_wsgi

Source code for Apache/mod_wsgi.
Apache License 2.0
1.02k stars 269 forks source link

Apache takes 90 seconds to restart after basic wsgi script is called #892

Open cosmicnet opened 2 weeks ago

cosmicnet commented 2 weeks ago

Hi,

I'm new to WSGI, in putting together a basic test I found that apache will take 90 seconds to restart after any wsgi script has been called.

I can't seem to find examples of why this is happening, or configuration that I can change to fix it.

I've not tested against any other version, so I don't know if it's version specific.

I can only assume that it's waiting for the child process to return. I don't see anything in the sample WSGI script that would be stopping it from returning though?

Without a WSGI script being called in a browser apache restarts in ~ 1 second:

[root@localhost conf]# time systemctl restart httpd

real    0m1.156s
user    0m0.009s
sys     0m0.017s

After one is called:

[root@localhost conf]# time systemctl restart httpd

real    1m30.191s
user    0m0.019s
sys     0m0.032s

or

[root@localhost conf]# time apachectl restart

real    1m30.316s
user    0m0.007s
sys     0m0.032s

There is nothing in the error log to suggest any issue. Just the graceful shutdown message then 90 seconds later the normal messages for start up:

[Mon Jul 01 16:07:19.666768 2024] [mpm_event:notice] [pid 26608:tid 140623484320064] AH00492: caught SIGWINCH, shutting down gracefully
[Mon Jul 01 16:08:49.957619 2024] [core:notice] [pid 26834:tid 139771054176576] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Mon Jul 01 16:08:49.958446 2024] [suexec:notice] [pid 26834:tid 139771054176576] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
[Mon Jul 01 16:08:49.991626 2024] [lbmethod_heartbeat:notice] [pid 26834:tid 139771054176576] AH02282: No slotmem from mod_heartmonitor
[Mon Jul 01 16:08:49.994516 2024] [http2:warn] [pid 26834:tid 139771054176576] AH02951: mod_ssl does not seem to be enabled
[Mon Jul 01 16:08:50.003626 2024] [mpm_event:notice] [pid 26834:tid 139771054176576] AH00489: Apache/2.4.37 (Oracle Linux Server) mod_fcgid/2.3.9 mod_wsgi/5.0.0 Python/3.12 mod_perl/2.0.12 Perl/v5.26.3 configured -- resuming normal operations
[Mon Jul 01 16:08:50.003666 2024] [core:notice] [pid 26834:tid 139771054176576] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'

Setup:

OL8.10 Conda Python 3.12 Apache/2.4.37

Apache conf changes:

LoadModule wsgi_module /opt/conda/lib/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so
...
Options +ExecCGI
AddHandler wsgi-script .wsgi

Standard test script hello.wsgi:

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!\n'
    response_headers = [('Content-type', 'text/plain'),
                  ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

Conda

wget https://repo.anaconda.com/miniconda/Miniconda3-py312_24.5.0-0-Linux-x86_64.sh
bash Miniconda3-py312_24.5.0-0-Linux-x86_64.sh -b -p /opt/conda
rm -f Miniconda3-py312_24.5.0-0-Linux-x86_64.sh
/opt/conda/bin/conda config --set always_yes yes --set changeps1 no
/opt/conda/bin/conda config --add channels conda-forge
/opt/conda/bin/conda install --quiet --freeze-installed -c main conda-pack
/opt/conda/bin/conda install twilio flask python-dotenv
/opt/conda/bin/conda install mod_wsgi
GrahamDumpleton commented 2 weeks ago

Odd. The standard apachectl from the original httpd distribution should only send a signal to the httpd process and nothing more. If it is hanging waiting then it could only be because the Linux distribution has modified apachectl to do something else.

A few comments.

Firstly, relying on ExecCGI/AddHandler to execute Python WSGI scripts is in general not recommended. Use WSGIDaemonProcess/WSGIProcessGroup/WSGIScriptAlias instead.

Second, avoid Anaconda Python distribution if you can and use standard Python distribution. Anaconda Python keeps breaking embedding in various ways so don't guarantee it will work.

Finally, if you aren't using mod_perl and mod_fcgid then disable them and don't load them in Apache in case it is a conflict with them.