Skip to content

gunicorn.pidfile.validate crashes gunicorn when PID exists but is from a different user #1091

Closed

Description

@ski-csis
Traceback (most recent call last):
  File "/opt/python2.7/bin/gunicorn", line 11, in <module>
    sys.exit(run())
  File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/app/base.py", line 189, in run
    super(Application, self).run()
  File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/arbiter.py", line 171, in run
    self.start()
  File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/arbiter.py", line 125, in start
    self.pidfile.create(self.pid)
  File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/pidfile.py", line 23, in create
    oldpid = self.validate()
  File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/pidfile.py", line 75, in validate
    os.kill(wpid, 0)
OSError: [Errno 1] Operation not permitted

This happens because the process identified by the pid-file exists, but belongs to a different user than the one starting gunicorn.

(This is with gunicorn 19.3.0)

Activity

  1. benoitc commented on Jul 31, 2015

    @benoitc
    Owner

    Thanks for the report :) How did you start gunicorn? Is the process still associated to the arbiter? does the user starting gunicorn have the rights to kill its children?

  2. ski-csis commented on Jul 31, 2015

    @ski-csis
    Author

    We run gunicorn with supervisord, nothing really fancy.

    I believe what happened was that the server was rebooted, and a few gunicorns did not shutdown in time, so when the server came up again their old pid-files were still there, but now the PIDs belonged to gunicorns from other users (the server runs a bunch of projects as different users.)

  3. benoitc commented on Aug 4, 2015

    @benoitc
    Owner

    @ski-csis but each pidfile is created per projetcs right ? So if the same suer is launching the project each time it shouldn't happen. Can you share your supervisor conf?

  4. ski-csis commented on Aug 4, 2015

    @ski-csis
    Author

    There is a pid-file per gunicorn, yes. That's not the problem.

    The problem is that the pid in the pid-file no longer belongs to the user running the gunicorn, because gunicorn was not shutdown cleanly before a reboot.

    If you want to simulate similar conditions, just pust 1 in a pid-file, and see how you get the same exception, when you try to start gunicorn.

    vagrant@vagrant:~$ echo 1 > some.pid
    vagrant@vagrant:~$ gunicorn --pid some.pid app:app
    [2015-08-04 07:28:27 +0000] [2698] [INFO] Starting gunicorn 19.3.0
    Traceback (most recent call last):
      File "/opt/python2.7/bin/gunicorn", line 11, in <module>
        sys.exit(run())
      File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
        WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
      File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/app/base.py", line 189, in run
        super(Application, self).run()
      File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
        Arbiter(self).run()
      File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/arbiter.py", line 171, in run
        self.start()
      File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/arbiter.py", line 125, in start
        self.pidfile.create(self.pid)
      File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/pidfile.py", line 23, in create
        oldpid = self.validate()
      File "/opt/python2.7/lib/python2.7/site-packages/gunicorn/pidfile.py", line 75, in validate
        os.kill(wpid, 0)
    OSError: [Errno 1] Operation not permitted
    vagrant@vagrant:~$ gunicorn --version
    gunicorn (version 19.3.0)
    
  5. benoitc commented on Aug 4, 2015

    @benoitc
    Owner

    Such error is expected but looking at that issue I think we can have a better answer to it. Ie instead of just crashing we could log the error and have an option to explicitly overwrite the pidfile if it exists. Another option would be not touching on that pidfile and instead create a new pidfile incrementing the name:

     /tmp/gunicorn.pid --> /tmp/gunicorn.pid.1
    

    What do others think about it?

  6. benoitc commented on Aug 14, 2015

    @benoitc
    Owner

    bump.

  7. tilgovi commented on Aug 14, 2015

    @tilgovi
    Collaborator

    I'd be fine to just catch errors and print an error.

  8. tilgovi commented on Aug 14, 2015

    @tilgovi
    Collaborator

    What do other daemons do?

  9. added this to the milestone on Aug 25, 2015
  10. benoitc commented on Aug 28, 2015

    @benoitc
    Owner

    Both exists. Maybe it's safer to just print catch/display error and exist indeed.

  11. tilgovi commented on Aug 31, 2015

    @tilgovi
    Collaborator

    Here's another idea that I think mixes robustness and safety:

    • If there's a pid file, check if there's a process with that PID.
    • If not, remove the pid file and continue.
    • If so, abort and log.

    This way, the common case of a dead pidfile will be cleaned up. It's possible that a new process has that pid, but less likely. If there's a pidfile that can't be remove, we act paranoid.

    I think this is better than creating a suffixed pidfile because that might trick an init system that expects gunicorn to write its pidfile at the configured location only.

  12. removed this from the milestone on Nov 23, 2015
  13. tilgovi commented on Mar 12, 2016

    @tilgovi
    Collaborator

    Any feedback on my proposal?

  14. 3 remaining items

  15. self-assigned this
    on Mar 20, 2016
  16. benoitc commented on Oct 16, 2016

    @benoitc
    Owner

    bump

  17. removed this from the milestone on Oct 16, 2016
  18. ChinnannGitHub commented on Oct 31, 2016

    @ChinnannGitHub

    Can any one suggest some workaround or the configuration that should be used for gunicorn with supervisord

  19. tilgovi commented on Dec 21, 2016

    @tilgovi
    Collaborator

    I think this is all we need to do here:

    diff --git a/gunicorn/pidfile.py b/gunicorn/pidfile.py
    index 7f7f844..6831512 100644
    --- a/gunicorn/pidfile.py
    +++ b/gunicorn/pidfile.py
    @@ -73,8 +73,9 @@ class Pidfile(object):
     
                     try:
                         os.kill(wpid, 0)
    -                    return wpid
                     except OSError as e:
    +                    if e.args[0] == errno.EPERM:
    +                        return wpid
                         if e.args[0] == errno.ESRCH:
                             return
                         raise
  20. tilgovi commented on Dec 21, 2016

    @tilgovi
    Collaborator

    Oops, I accidentally deleted the first return wpid. Here's the proper diff:

    diff --git a/gunicorn/pidfile.py b/gunicorn/pidfile.py
    index 7f7f844..a6e085f 100644
    --- a/gunicorn/pidfile.py
    +++ b/gunicorn/pidfile.py
    @@ -75,6 +75,8 @@ class Pidfile(object):
                         os.kill(wpid, 0)
                         return wpid
                     except OSError as e:
    +                    if e.args[0] == errno.EPERM:
    +                        return wpid
                         if e.args[0] == errno.ESRCH:
                             return
                         raise
  21. berkerpeksag commented on Dec 22, 2016

    @berkerpeksag
    Collaborator

    Randall's inline patch looks good to me. I think we can easily unit test the new behavior by using a mock with a side effect.

  22. added 4 commits that reference this issue on Dec 22, 2016
    2a7ec86
    92bbfbe
    bd2d2d0
    3b842c5
  23. added a commit that references this issue on Jan 12, 2017
    d18a9cd
  24. added a commit that references this issue on Feb 6, 2017
    c017f4b
  25. added a commit that references this issue on Mar 16, 2018
    2dd85f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions