Rufus
Rufus

Reputation: 5566

Killing shell=True process results in ResourceWarning: subprocess is still running

Following the advice from How to terminate a python subprocess launched with shell=True

I have a process that I start with the following

process = subprocess.Popen(my_cmd, shell=True, executable='/bin/bash', preexec_fn=os.setsid)

my_cmd is in the form of some_cmd_that_periodically_outputs_to_stdout | grep "some_fancy_regex" > some_output_file.txt

I kill the process with the following

os.killpg(os.getpgid(process.pid), signal.SIGKILL)

After killing, I get the following warning

ResourceWarning: subprocess XXX is still running

Why am I getting this warning?

Upvotes: 8

Views: 9286

Answers (1)

user2357112
user2357112

Reputation: 280390

You didn't wait for the process to end. Even if you kill the process, you're still supposed to wait for it so you don't have a zombie process hanging around. Python is warning you that you didn't wait.

Add a process.wait() call after killing it.

Upvotes: 11

Related Questions