[Python] How can I kill child processes spawned by the subprocess.Popen() process?

Discussão em 'Python' iniciado por Stack, Setembro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I use subprocess.Popen to spawn a new child process - in this case git clone.

    The issue is that git clone itself spawns subprocesses, and when I try to kill them using Popen.kill() only the parent (git clone) gets killed, but not its children.

    An example of a child is:

    79191 /usr/lib/git-core/git fetch-pack --stateless-rpc --stdin --lock-pack --include-tag --thin --cloning --depth=1 https://example.com/scm/adoha/adoha_digit_interpretation.git/


    How can I kill all of the processes - git clone and its children?

    NB: I've thought about placing the processes in their own process group, but then the main process gets killed as well.

    # execute a child process using os.execvp()
    p = subprocess.Popen(shlex.split(f'git clone --bare --depth=1 -v \'{url}\' \'{temp_dir}\''),
    stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    try:
    ret_code = p.wait(timeout)
    except subprocess.TimeoutExpired as exc:
    p.kill()
    shutil.rmtree(temp_dir)
    raise common.exc.WatchdogException(f'Failed to clone repository: Timeout.'
    f'\n{timeout=}\n{url=}') from exc

    Continue reading...

Compartilhe esta Página