Search code examples
pythonpython-3.xsubprocessexit-code

Python3: Exception or return code when resource limit exceeded?


I am running python 3.4.3. I have a subprocess that may run a long time, or generate files that are too large. I have this code.

def setlimits():
    if DEBUG:
        print("Setting resource limit in child (pid {0})".format(os.getpid()))
    resource.setrlimit(resource.RLIMIT_CPU, (.05, .05))  # CPU time in secs
    resource.setrlimit(resource.RLIMIT_FSIZE, (1000000, 1000000)) # file size
    resource.setrlimit(resource.RLIMIT_NOFILE, (20, 20)) # Number open files

Then I call the subprocess (inside a larger routine) with this code.

        rc = None
        try:
            rc = subprocess.call(["potentially long-running command"],shell=True, preexec_fn=setlimits) 
        except Exception as err:
            print("Exception happened")
            rc = -1
        print("rc = {0}".format(str(rc)))

When I feed it a process that runs long it does not give me an exception. (I expected OSError based on the resource documentation.) It gives me an rc=137.

Any idea where this is documented? I'd of course like to know I've covered all the cases; do I need a if rc>128-type check?


Solution

  • 137 is a reserved exit code indicating that the process was killed - it's equal to 128 + 9 (where the 9 stands for signal 9 or SIGKILL). This most likely happened when the process hit its hard CPU limit and was killed by the kernel.

    subprocess.call() does not raise an exception if the called process returns with a nonzero exit status. If you want that behaviour, you're better off with either subprocess.check_call() or subprocess.run(..., check=True), which will raise CalledProcessError on nonzero exit status.