Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reloading through SIGHUP kills uwsgi, unable to chown socket #1471

Open
AMDmi3 opened this issue Feb 17, 2017 · 15 comments
Open

Reloading through SIGHUP kills uwsgi, unable to chown socket #1471

AMDmi3 opened this issue Feb 17, 2017 · 15 comments

Comments

@AMDmi3
Copy link

AMDmi3 commented Feb 17, 2017

uwsgi running like this:

/usr/local/bin/uwsgi -M --manage-script-name --mount /=repology-app:app --pythonpath=/var/db/repology/repology --pidfile /var/run/uwsgi-repology.pid -s /var/run/repology/uwsgi.socket --chmod-socket=660 --chown-socket=www:www -d /var/log/uwsgi-repology.log --uid repology --gid repology

The config is pretty common - application is running under its own dedicated user repology, and socket is owned by www as it should be accessed by frontend (nginx running as www).

when issuing SIGHUP, it dies with

chown(): Operation not permitted [core/utils.c line 2623]

in the log, likely because it's already running as repology and is thus unable to change socket ownership to www.

  • Likely it should check socket permissions and don't try to change them if they are already correct.
  • Definitely it should not die from SIGHUP, like all well-behaving servers. Such errors should not be considered fatal.
@ghost
Copy link

ghost commented Mar 1, 2017

I'm seeing this as well. With the defaults on FreeBSD now uwsgi is started by root, creates the socket as www:www, and drops privileges to uwsgi user. This gives us nice isolation, but a SIGHUP causes it to close the unix and network sockets and attempt to re-open them. It fails because it no longer has root privileges so it can't recreate a socket owned by www:www, then terminates.

I don't understand why a reload requires destroying the sockets...

@xrmx
Copy link
Collaborator

xrmx commented Mar 1, 2017

@AMDmi3 @feld what version of uwsgi is this?

@AMDmi3
Copy link
Author

AMDmi3 commented Mar 1, 2017

2.0.14

@ghost
Copy link

ghost commented Apr 25, 2017

Any updates on this?

uqs pushed a commit to freebsd/freebsd-ports that referenced this issue Aug 20, 2017
* Changed socket uid/gid and mode, this should fix github issue #1471[1]

[1] unbit/uwsgi#1471

PR:		210170
Submitted by:	David Wahlund
Reviewed by:	matthew (mentor)
Approved by:	matthew (mentor)
Differential Revision:	https://reviews.freebsd.org/D12089


git-svn-id: svn+ssh://svn.freebsd.org/ports/head@448441 35697150-7ecd-e111-bb59-0022644237b5
uqs pushed a commit to freebsd/freebsd-ports that referenced this issue Aug 20, 2017
* Changed socket uid/gid and mode, this should fix github issue #1471[1]

[1] unbit/uwsgi#1471

PR:		210170
Submitted by:	David Wahlund
Reviewed by:	matthew (mentor)
Approved by:	matthew (mentor)
Differential Revision:	https://reviews.freebsd.org/D12089
@AMDmi3
Copy link
Author

AMDmi3 commented Aug 21, 2017

Note that above mentioned FreeBSD ports commits didn't fix this.

@DuncanFairley
Copy link

The socket permissions were screwed for me upon updating to 2.0.15 from 2.0.13 on FreeBSD.

@theory14
Copy link

theory14 commented Nov 3, 2017

I have the same problem when I moved to 2.0.15 on FreeBSD 11.1 RELEASE. I solved this just now by setting uwsgi to run as www and the socket owner to www:
uwsgi_uid="www"
uwsgi_gid="www"
uwsgi_socket_owner="www"

The socket is then owned by www and the SIGHUP reload works as expected.

Setting those to default UID to uwsgi and socket_owner to uwsgi results in failure. The socket is owned by uwsgi and the uwsgi processes are running as uwsgi.

Something just doesn't make sense here... Why does one unprivileged user work and another doesn't?

@tehfink
Copy link

tehfink commented Feb 15, 2018

This is to note that @theory14's solution did not work for me on FreeBSD 11.1 in a jail with uwsgi-py36-2.0.15_3…

@mozumder
Copy link

I've faced this issue for over a year on FreeBSD. I still don't have a solution to this. I have to kill -9 the emperor processes to stop UWSGI.

@AMDmi3
Copy link
Author

AMDmi3 commented Jun 28, 2018

I have to kill -9 the emperor processes to stop UWSGI

service uwsgi restart should be quite enough (however not service uwsgi reload which is broken because of the mentioned problem).

@citrin
Copy link
Contributor

citrin commented Sep 25, 2018

I have the same problem with uwsgi 2.0.17.1 on FreeBSD 11.2 - for some reason Unix socket is not inherited after reload (SIGHUP).
Log look like:

uwsgi_setup_inherited_sockets() is called (debug log added by me)
error removing unix socket, unlink(): Permission denied [core/socket.c line 198]                                                                                                                                                              
bind(): Address already in use [core/socket.c line 230]

@citrin
Copy link
Contributor

citrin commented Sep 25, 2018

As I can see problem is in function uwsgi_add_socket_from_fd(). Adding memset(&usa, 0, sizeof(usa)); fixes problem for me, but may be other changes to uwsgi_add_socket_from_fd() is required.

citrin added a commit to citrin/uwsgi that referenced this issue Sep 25, 2018
On FreeBSD unix socket path returned by getsockname() in
uwsgi_add_socket_from_fd() can contain garbage trailing character(s).

Clear structure before passing it to getsockname() as a workaround.

Problem with reloading through SIGHUP on FreeBSD was also reported in unbit#1471
xrmx pushed a commit that referenced this issue Sep 26, 2018
On FreeBSD unix socket path returned by getsockname() in
uwsgi_add_socket_from_fd() can contain garbage trailing character(s).

Clear structure before passing it to getsockname() as a workaround.

Problem with reloading through SIGHUP on FreeBSD was also reported in #1471
@karolyi
Copy link
Contributor

karolyi commented Feb 23, 2019

I can still reproduce the problem with using the latest dev, 2.1-dev-f17bdbd1.

@karolyi
Copy link
Contributor

karolyi commented Feb 23, 2019

Also with 2.0.18.

@karolyi
Copy link
Contributor

karolyi commented Feb 23, 2019

It seems that the two offending lines in /usr/local/etc/rc.d/uwsgi are:

               rc_flags=${rc_flags}" --vassals-set uid=${uwsgi_uid} --vassals-set gid=${uwsgi_gid}"
               rc_flags=${rc_flags}" --vassals-set chmod-socket=${uwsgi_socket_mode} --vassals-set chown-socket=${uwsgi_socket_owner}"

Line 130-131. Commenting these two lines out will help uwsgi start/reload normally. I use the emperor-tyrant option, and with that the passed parameters to uwsgi vassals above are not only meaningless/harmful, they hinder startup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants