Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 578
Rufus Avatar asked Sep 24 '18 09:09

Rufus


People also ask

How do I stop subprocess running?

The kill() is a built-in method used for terminating a single subprocess. The kill() command keeps running in the background.

Should I use shell true in subprocess?

We should avoid using 'shell=true' in subprocess call to avoid shell injection vulnerabilities. In this call you have to pass a string as a command to the shell. If call_method is user controlled then it can be used to execute any arbitrary command which can affect system.

Why are shells true in subprocess?

Setting the shell argument to a true value causes subprocess to spawn an intermediate shell process, and tell it to run the command. In other words, using an intermediate shell means that variables, glob patterns, and other special shell features in the command string are processed before the command is run.

What shell does Python subprocess use?

By default, running subprocess. Popen with shell=True uses /bin/sh as the shell. If you want to change the shell to /bin/bash , set the executable keyword argument to /bin/bash .


1 Answers

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.

like image 110
user2357112 supports Monica Avatar answered Oct 24 '22 19:10

user2357112 supports Monica