You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gunicorn.pidfile.validate crashes gunicorn when PID exists but is from a different user #1091
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.
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?
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.)
@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?
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)
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:
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.
Activity
benoitc commentedon Jul 31, 2015
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?
ski-csis commentedon Jul 31, 2015
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.)
benoitc commentedon Aug 4, 2015
@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?
ski-csis commentedon Aug 4, 2015
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
1in a pid-file, and see how you get the same exception, when you try to start gunicorn.benoitc commentedon Aug 4, 2015
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:
What do others think about it?
benoitc commentedon Aug 14, 2015
bump.
tilgovi commentedon Aug 14, 2015
I'd be fine to just catch errors and print an error.
tilgovi commentedon Aug 14, 2015
What do other daemons do?
benoitc commentedon Aug 28, 2015
Both exists. Maybe it's safer to just print catch/display error and exist indeed.
tilgovi commentedon Aug 31, 2015
Here's another idea that I think mixes robustness and safety:
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.
tilgovi commentedon Mar 12, 2016
Any feedback on my proposal?
3 remaining items
benoitc commentedon Oct 16, 2016
bump
ChinnannGitHub commentedon Oct 31, 2016
Can any one suggest some workaround or the configuration that should be used for gunicorn with supervisord
tilgovi commentedon Dec 21, 2016
I think this is all we need to do here:
tilgovi commentedon Dec 21, 2016
Oops, I accidentally deleted the first
return wpid. Here's the proper diff:berkerpeksag commentedon Dec 22, 2016
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.
catch access denied to pid file
catch access denied to pid file
catch access denied to pid file
catch access denied to pid file
catch access denied to pid file
catch access denied to pid file
catch access denied to pid file