7

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?

Rufus
  • 5,111
  • 4
  • 28
  • 45
  • remove this damn `shell=True` that you probably don't need and it'll work... or follow the dupe link. – Jean-François Fabre Sep 24 '18 at 09:29
  • But that is exactly how I got this warning, by following the dupe link – Rufus Sep 24 '18 at 09:32
  • oh, right. Lemme check. Have you tried this solution: https://stackoverflow.com/a/25134985/6451573 probably better (I've tested it myself) – Jean-François Fabre Sep 24 '18 at 09:34
  • I still get the same warning. I don't think this is a duplicate, since my question is asking why I'm getting this warning and what's happening underneath. (though knowing how to solve it would be a bonus) – Rufus Sep 25 '18 at 00:48
  • have you tried to remove `shell=True` ? I think it's useless. That would make things easier without a shell process in between – Jean-François Fabre Sep 25 '18 at 06:08
  • This question is a follow up to the question linked which specifically deals with the case of shell=True... the dupe link clearly does not explain the cause behind `ResourceWarning`. I don't understand why this is still marked as a duplicate. I'm not asking how to terminate a python subprocess with shell=True, I'm asking why, when terminating a python subprocess with shell=True, I get a `ResourceWarning` – Rufus Sep 25 '18 at 06:20
  • Is `subprocess XXX` pid the same as `process.pid`? – JohanL Oct 04 '18 at 18:23

1 Answers1

9

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505