2

I have had trouble to kill uwsgi processes. Whenever I use below commands to shutdown uwsgi, another uwsgi workers respawned after a few seconds. So after shutdown uwsgi process and start up uwsgi server again, uwsgi processes continues to accumulate in memory.

  • sudo pkill -f uwsgi -9
  • kill -9 cat /tmp/MyProject.pid
  • uwsgi --stop /tmp/MyProject.pid

I want to know how to kill uwsgi processes not making uwsgi workers respawned. I attach ini file as below.

[uwsgi]
chdir            = /home/data/MyProject/
module           = MyProject.wsgi
home             = /home/data/anaconda3/envs/env1
master           = true
pidfile          = /tmp/MyProject.pid
single-interpreter = true
die-on-term     = true 
processes       = 4
socket          = /home/data/MyProject/MyProject.sock
chmod-socket    = 666
vacuum          = true

Thank you in advance!!

Jo Jay
  • 123
  • 1
  • 3
  • 14

1 Answers1

1

at first:

  1. uwsgi is a binary protocol that uWSGI uses to communicate with other servers. uWSGI is an application server.
  2. You should avoid (1, 2) to use -9 (SIGKILL) to stop process if you care about child processes (workers)

what I can say according your information:

Whenever I use below commands to shutdown uwsgi, another uwsgi workers respawned after a few seconds

Looks like you trying to kill worker (child) process but not uWSGI application server (master process). Here is processes = 4 in your config, so application server (master process) watching for minimum running workers (child processes). And if one of it exited (by KILL signal or source code exception, no matter) application server starting new 4th process.

So after shutdown uwsgi process and start up uwsgi server again

If uwsgi process is a worker (child process) - see answer above

If uwsgi process is an application server (master process) - here is another problem. You using KILL (-9) signal to stop server. And that is not allow to exit application properly (see at first's second point). So when your application server is unexpectedly killed, it leave all 4 child processes running without parent (master) process (say hello to orphaned process)

You should to use SIGTERM instdead of SIGKILL. Do you understand meaning of die-on-term = true? Yeah! That means please stop stack of all processes on SIGTERM.

uwsgi --stop /tmp/MyProject.pid

This command should stop all processes properly. Here is no information in your question to decide what problem that may be...

I have three guesses:

  1. web application source problem: exit operation is not handled properly
  2. die-on-term = true inverts the meanings of SIGTERM and SIGQUIT to uWSGI, so maybe stop working like reload in this case? not sure.
  3. some kind of misunderstanding

Update 1: how to control number of child processes dynamically

In addition, you may check this helpful article to understand how to scale-in and scale-out child process dynamically to be able run all 4th processes only when it needed and run only 1 process when service is idle.

Update 2: CHeck if behaviour reproducible

I created simple uWSGI application to check behaviour:

opt
`-- wsgi_example
    |-- app.py
    `-- wsgi.ini
  • wsgi.ini
[uwsgi]
chdir            = /opt/wsgi_example
module           = app
master           = true
pidfile          = /opt/wsgi_example/app.pid
single-interpreter = true
die-on-term     = true
processes       = 2
socket          = /opt/wsgi_example/app.sock
chmod-socket    = 666
vacuum          = true
  • app.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

I running this application by wsgi wsgi.ini
I stopping this application by uwsgi --stop app.pid

Output is

spawned uWSGI master process (pid: 15797)
spawned uWSGI worker 1 (pid: 15798, cores: 1)
spawned uWSGI worker 2 (pid: 15799, cores: 1)
SIGINT/SIGQUIT received...killing workers...
worker 1 buried after 1 seconds
worker 2 buried after 1 seconds
goodbye to uWSGI.
VACUUM: pidfile removed.
VACUUM: unix socket /opt/wsgi_example/app.sock removed.

All working properly. All processes stopped.
Your question is not reproducible.

Search for the problem inside application code or your individual infrastructure configuration.

※ checked with uWSGI 2.0.8 and 2.0.19.1 and python 3.6.7

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • First, I really appreciate your answer!! As you mentioned regarding "die-on-term=true", when I stop application by using "uwsgi --stop /tmp/MyProject.pid" command, the child process killed and new child process respawn right away. I share uwsgi log as below – Jo Jay Sep 30 '21 at 08:17
  • `SIGINT/SIGQUIT received...killing workers... worker 1 buried after 1 seconds worker 2 buried after 1 seconds worker 3 buried after 1 seconds worker 4 buried after 1 seconds goodbye to uWSGI. pp) spawned uWSGI master process (pid: 18825) spawned uWSGI worker 1 (pid: 18830, cores: 1) spawned uWSGI worker 2 (pid: 18831, cores: 1) spawned uWSGI worker 3 (pid: 18832, cores: 1) spawned uWSGI worker 4 (pid: 18833, cores: 1)` – Jo Jay Sep 30 '21 at 08:21
  • I want to block workers(another child processes) from being respawned after previous child processes killed – Jo Jay Sep 30 '21 at 08:26
  • @JoJay 1. what is result of using `SIGTERM` (`kill -15 $PID`) instead of `SIGKILL`? 2. what is result of using `uwsgi --stop /tmp/MyProject.pid` if you set `die-on-term=false`? – rzlvmp Sep 30 '21 at 09:22
  • 2. what is result of using uwsgi --stop /tmp/MyProject.pid if you set die-on-term=false? -> result is same with die-on-term=true. The child processes was killed and new child processes respawn right away – Jo Jay Oct 01 '21 at 00:27
  • @JoJay I updated my answer. I can't reproduce your problem so can't help more over – rzlvmp Oct 01 '21 at 10:20