pythondaemonzombie-processpython-daemondefunct

Daemon in python generates defunct/zombie linux process


I'm trying to spawn long processes from a flask application. So I have a main process that handles incoming web requests and from which I start daemons using the python-daemon library. Here is a minimal working example:

import os
import time
import daemon
import lockfile

def daemon_function():
    return True

working_directory = os.path.dirname(os.path.abspath(__file__))
pid_file = os.path.join(working_directory,"my_daemon.pid")

daemon_context = daemon.DaemonContext(
        working_directory=working_directory,
        pidfile=lockfile.FileLock(pid_file),
    )

pid = os.fork()
if pid == 0:
    with daemon_context:
        daemon_function()

time.sleep(10)

When I use the linux command ps -ef while the main process is still running, after the daemon process has terminated, I see this output:

user     2484     1  0 09:38 ?        00:00:01 /lib/systemd/systemd --user
user    11269  6061  0 12:07 pts/2    00:00:00 bash
user    28817 11269  1 15:43 pts/2    00:00:00 python test_daemon.py
user    28818 28817  0 15:43 pts/2    00:00:00 [python] <defunct>

The reason why I use the fork function before the with daemon_context: statement is that I need the main process to continue.

Should I worry about the created defunct process (I might spawn a lot of them) ? How could I avoid this zombie to appear?


Solution

  • If you don't want to create zombie processes you should wait for them to finish i.e. execute wait system call instead of sleep:

    import os
    import time
    import daemon
    import lockfile
    
    def daemon_function():
        return True
    
    working_directory = os.path.dirname(os.path.abspath(__file__))
    pid_file = os.path.join(working_directory,"my_daemon.pid")
    
    daemon_context = daemon.DaemonContext(
            working_directory=working_directory,
            pidfile=lockfile.FileLock(pid_file),
        )
    
    pid = os.fork()
    if pid == 0:
        with daemon_context:
            daemon_function()
    
    os.waitpid(pid, 0)