# Buggy code
def daemonize_if_required(self):
    if self.options.daemon:
        # Late import so logging works correctly
        import salt.utils

        salt.utils.daemonize()


# Traceback (AssertionError)
root@nitin-salt-develop:~/salt$ service salt-master stop
Stopping salt-master daemon:                               [  OK  ]
root@nitin-salt-develop:~/salt$ service salt-master start
Starting salt-master daemon: Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib64/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib64/python2.6/multiprocessing/util.py", line 269, in _exit_function
p.join()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 117, in join
assert self._parent_pid == os.getpid(), 'can only join a child process'
AssertionError: can only join a child process
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib64/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib64/python2.6/multiprocessing/util.py", line 269, in _exit_function
p.join()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 117, in join
assert self._parent_pid == os.getpid(), 'can only join a child process'
AssertionError: can only join a child process
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib64/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib64/python2.6/multiprocessing/util.py", line 269, in _exit_function
p.join()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 119, in join
res = self._popen.wait(timeout)
File "/usr/lib64/python2.6/multiprocessing/forking.py", line 122, in wait
return self.poll(0)
File "/usr/lib64/python2.6/multiprocessing/forking.py", line 107, in poll
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
Error in sys.exitfunc:
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/log/setup.py", line 906, in __global_logging_exception_handler
shutdown_multiprocessing_logging_listener()
File "/usr/lib/python2.6/site-packages/salt/log/setup.py", line 800, in shutdown_multiprocessing_logging_listener
__MP_LOGGING_QUEUE_PROCESS.terminate()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 111, in terminate
self._popen.terminate()
File "/usr/lib64/python2.6/multiprocessing/forking.py", line 139, in terminate
os.kill(self.pid, signal.SIGTERM)
OSError: [Errno 3] No such process

Original exception was:
Traceback (most recent call last):
File "/usr/lib64/python2.6/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib64/python2.6/multiprocessing/util.py", line 269, in _exit_function
p.join()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 119, in join
res = self._popen.wait(timeout)
File "/usr/lib64/python2.6/multiprocessing/forking.py", line 122, in wait
return self.poll(0)
File "/usr/lib64/python2.6/multiprocessing/forking.py", line 107, in poll
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
[  OK  ]

# Fixed code
def daemonize_if_required(self):
    if self.options.daemon:
        if self._setup_mp_logging_listener_ is True:
            # Stop the logging queue listener for the current process
            # We'll restart it once forked
            log.shutdown_multiprocessing_logging_listener()

        # Late import so logging works correctly
        import salt.utils

        salt.utils.daemonize()

    # Setup the multiprocessing log queue listener if enabled
    self._setup_mp_logging_listener()