Werkzeug server is shutting down in Django application

Question:

after updating the Werkzeug version from 2.0.3 to 2.1.0, I keep getting errors every time I run the server, and here is the error log:

Exception happened during processing of request from ('127.0.0.1', 44612)                                                                                                                                  
Traceback (most recent call last):                                                                                                                                                                         
  File "/usr/lib/python3.8/socketserver.py", line 683, in process_request_thread                                                                                                                           
    self.finish_request(request, client_address)                                                                                                                                                           
  File "/usr/lib/python3.8/socketserver.py", line 360, in finish_request                                                                                                                                   
    self.RequestHandlerClass(request, client_address, self)                                                                                                                                                
  File "/usr/lib/python3.8/socketserver.py", line 747, in __init__                                                                                                                                         
    self.handle()                                                                                                                                                                                          
  File "/home/oladhari/.virtualenvs/reachat/lib/python3.8/site-packages/werkzeug/serving.py", line 363, in handle                                                                                          
    super().handle()                                                                                                                                                                                       
  File "/usr/lib/python3.8/http/server.py", line 427, in handle                                                                                                                                            
    self.handle_one_request()                                                                                                                                                                              
  File "/usr/lib/python3.8/http/server.py", line 415, in handle_one_request                                                                                                                                
    method()                                                                                                                                                                                               
  File "/home/oladhari/.virtualenvs/reachat/lib/python3.8/site-packages/werkzeug/serving.py", line 243, in run_wsgi                                                                                        
    self.environ = environ = self.make_environ()                                                                                                                                                           
  File "/home/oladhari/.virtualenvs/reachat/lib/python3.8/site-packages/django_extensions/management/commands/runserver_plus.py", line 326, in make_environ                                                
    del environ['werkzeug.server.shutdown']                                                                                                                                                                
KeyError: 'werkzeug.server.shutdown'  

this exception keep appearing while incrementing by 2 ( (‘127.0.0.1’, 44612) -> (‘127.0.0.1’, 44628) and the server crash

checking the changes log, I have found this detail:

Remove previously deprecated code. #2276

Remove the non-standard shutdown function from the WSGI environ when running the development server. See the docs for alternatives.

here is the link to the changes log

it asks to check the documentation for alternatives but can not find any

please let me know how I would resolve this error, thank you
NB: my python version is 3.8

Asked By: ladhari

||

Answers:

Literally just ran into this today. According to their (git repo issue 1715) and assuming you are running runserver_plus, there are three options that worked for some users. The first worked for me:

  1. Not altering your files and adding the option --keep-meta-shutdown. My full command looks like python manage.py runserver_plus --cert-file /path/to/cert.pem --key-file /path/to/key.pem --keep-meta-shutdown localhost:9000
  2. Comment out open lines 325 and 326 under your runserver_plus.py
  3. Upgrading python to 3.10

Hope this helps!

Answered By: RudeBoy617

The Django-extensions issue for this recommends running runserver_plus with the --keep-meta-shutdown argument.

Alternatively, you can just downgrade Werkzeug to 2.0.* until this is fixed:

pip install Werkzeug==2.0.*

If using a requirements.txt file, use the following:

Werkzeug==2.0.*
Answered By: Zags
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.