Leaky subprocess timeout
If the process doesn't terminate after `timeout` seconds, a `TimeoutExpired` exception is raised. Because the child process does not end if the timeout expires, to properly clean up you must explicitly end the child process and finish communication.
def subprocess_timeout_noncompliant():
    import subprocess
    process = subprocess.Popen("ls -al",
                               bufsize=-1,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    try:
        # Noncompliant: fails to terminate the child process before
        # the timeout expires.
        outs, errs = process.communicate(timeout=15)
    except subprocess.TimeoutExpired:
        print("Timed out")
def subprocess_timeout_compliant():
    import subprocess
    process = subprocess.Popen("ls -al",
                               bufsize=-1,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    try:
        # Compliant: makes sure to terminate the child process when
        # the timeout expires.
        outs, errs = process.communicate(timeout=15)
    except subprocess.TimeoutExpired:
        process.kill()
        outs, errs = process.communicate()