Updated gunicorn/pidfile.py #1042

Merged
merged 3 commits into from Jul 9, 2015

Projects

None yet

5 participants

@brijeshb42
Contributor

Added a try block to convert the contents of the pidfile.py if it can be converted to integer or not. If not, then wpid is assigned 0 and the validate function returns as usual. If the pidfile has a valid int, the function continues to kill the process as before.

@brijeshb42 brijeshb42 Updated gunicorn/pidfile.py
Added a try block to convert the contents of the `pidfile.py` if it can be converted to integer or not. If not, then `wpid` is assigned 0 and the `validate` function returns as usual. If the pidfile has a valid int, the function continues to kill the process as before.
80e93b9
@benoitc
Owner
benoitc commented Jun 4, 2015

+1

@berkerpeksag
Collaborator

LGTM. Just curious: How did you noticed this? Did you see a traceback or just read the code?

@brijeshb42
Contributor

I was trying to run a Flask app using gunicorn but the pidfile that I was providing was already created but it had a single empty line in it. So it was not starting and giving invalid literal for int() with base 10 ''. Then through the stacktrace, I was able to go to that particular line. So I added an extra check.

@emamirazavi

Why don't i see this problem?!

On Thu, Jun 4, 2015 at 1:23 PM, Brijesh Bittu notifications@github.com
wrote:

I was trying to run a Flask app using gunicorn but the pidfile that I was
providing was already created but its had a single empty line in it. So it
was not starting and giving invalid literal for int() with base 10 ''.
Then through the stacktrace, I was able to go to that particular line. So I
added an extra check.


Reply to this email directly or view it on GitHub
#1042 (comment).

@brijeshb42
Contributor

I was using gunicorn:0.19.3 .
I guess this problem won't appear if the pidfile you are supplying does not exist or contains a valid integer or exists but is completely empty.
Mine had a single empty line.

@benoitc
Owner
benoitc commented Jun 6, 2015

hrm f.read() or 0 should fix the empty case normally. The valid integer case is still valid. Which version of Python are you using?

@brijeshb42
Contributor

I am using sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)

@tilgovi tilgovi and 1 other commented on an outdated diff Jul 7, 2015
gunicorn/pidfile.py
@@ -66,7 +66,10 @@ def validate(self):
return
try:
with open(self.fname, "r") as f:
- wpid = int(f.read() or 0)
+ try:
+ wpid = int(f.read())
+ except ValueError:
+ wpid = 0
@tilgovi
tilgovi Jul 7, 2015 Collaborator

Might be clearer to simply return rather than relying on the subsequent if statement.

@benoitc
benoitc Jul 7, 2015 Owner

also the case where the pid is an empty file should be handled. I would do the following:

try:
    wpid = int(f.read() or 0)
except ValueError:
    raise HaltError("pidfile is invalid")

or any other error message. Thoughts?

@tilgovi
tilgovi Jul 7, 2015 Collaborator

If the zero case fails anyway (we return right after) and int('') is a ValueError just return in the except clause and remove the subsequent if.

@benoitc
benoitc Jul 7, 2015 Owner

right... I forgot we were returning. so indeed let just return.

@benoitc
benoitc Jul 7, 2015 Owner

@brijeshb42 can you do the change on the PR? Will merge it right after :)

@brijeshb42 brijeshb42 Update pidfile.py
Return directly in the `except` instead of checking again for zero (as suggested by @benoitc and @tilgovi)
68a9440
@brijeshb42
Contributor

@benoitc Updated the code.

@tilgovi
Collaborator
tilgovi commented Jul 9, 2015

I think the or 0 just has to be removed. The intention is that if read returns the empty string that int raises ValueError and that is how we can skip the subsequent if.

@brijeshb42 brijeshb42 Removed `or 0`
Removed `or 0` to raise `ValueError` for empty pidfile or pidfile with invalid contents.
db786ce
@brijeshb42
Contributor

@tilgovi added your suggestion.

@tilgovi tilgovi merged commit d971eb4 into benoitc:master Jul 9, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@tilgovi
Collaborator
tilgovi commented Jul 9, 2015

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment