[Django]-Os.setsid operation not permitted

3👍

Python’s os.setsid() probably calls the underlying library call setsid(3).

The full ERRORS section in man 3 setsid is:

ERRORS
   EPERM  The  process group ID of any process equals the PID of the call-
          ing process.  Thus, in particular, setsid() fails if the calling
          process is already a process group leader.

IOW: the only cause for a setsid() failure, is when the calling process is already a process group leader. Ergo: you may ignore the failure. To verify that this is the case, compare what you get back from getpid() and getpgid() when os.setsid() fails:

#!/usr/bin/env python

import os
import errno

if __name__ == '__main__':
    try:
        os.setsid()
    except OSError, (err_no, err_message):
        print "os.setsid failed: errno=%d: %s" % (err_no, err_message)
        print "pid=%d  pgid=%d" % (os.getpid(), os.getpgid(0))

When I run the above I get:

os.setsid failed: errno=1: Operation not permitted
pid=17025  pgid=17025

Note that the process-id (pid) is equal to the process-group-id (pgid), meaning that this process is indeed already a process group leader.

P.S: Yes, it is a baffling feature of python to raise exceptions where a simple error return code would suffice to distinguish success from failure (just like the familiar Un*x libc APIs behave). This is unfortunately how the python system call interface is implemented, so you need to wrap many system calls with try: except ...: constructs to prevent python from aborting your code.

👤arielf

Leave a comment