So python has a subprocess run timeout issue

john | Oct. 23, 2023, 10:12 p.m. Programming

I had written about having issues killing a process started as a subprocess in python a bit ago.  For some reason django-rq wasn't killing the process either, nor was subprocess.run timeout working.  The program would just hang or keep doing what it was doing.

It is ill advised to use shell=True in python for security reasons.  Unless you know the input that's going in is going to be clean.  Sometimes though, the only way to get some of these commands to work to begin with quickly due to the amount of quotations string replaces and such is just to shove it in a shell=True subprocess command and be done with it.  So you can come back later and refactor it, or just throw some programming debt onto the project. 

Regardless, this started leading to some issues.  Apparently when you use shell=True python this will cause the command to launch a child process, which causes some permission issues and does not get killed.

To mitigate this, some people suggest taking out shell=True.  Surprisingly though if you're on linux based systems like ubuntu, you can simply add the command `exec ` to your subprocess command. 

This will cause the command to inherit the shell process, instead of having the shell launch a child process, so the timeout works.



Read other posts in the Programming category:

Programming posts

Read other posts:


Stay notified of new posts

Get an email once a month if there where posts that month


RSS Feed

Copyright © 2024 Johnathan Nader