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

File descriptors not inherited by child processes in python 3.4 #787

Closed
thunderk opened this issue May 28, 2014 · 7 comments
Closed

File descriptors not inherited by child processes in python 3.4 #787

thunderk opened this issue May 28, 2014 · 7 comments

Comments

@thunderk
Copy link
Contributor

There was an important change in python 3.4 that seems to break the usage of use_sockets.

Python 3.4 does not allow inheritance of newly created file descriptors to child processes by default : https://docs.python.org/3/whatsnew/3.4.html#whatsnew-pep-446

On linux, when using file descriptors for sockets, only fds 0 through 7 seems to be made available in child process.

I have a circus conf with 4 daemon watchers with use_sockets=True. The first 2 daemons get fd=6 and fd=7 and can open their socket and work with it. The last 2 daemons get fd=8 and fd=9 and get an exception on opening them :

OSError: [Errno 9] Bad file descriptor
@thunderk
Copy link
Contributor Author

thunderk commented Jun 2, 2014

I created a branch to show a potential fix for the problem. This is a quick-and-dirty solution, I want to add unit tests, and check if this is the correct place to change fd inheritance. I'm also don't sure if the 'hasattr' method I use is the best way to find if we need to do this.
https://github.com/thunderk/circus/compare/787-fd-inheritance-py34

@k4nar
Copy link
Contributor

k4nar commented Jun 2, 2014

Shouldn't we do it only if "close_fds" is False ?

@tarekziade
Copy link
Member

@thunderk I suspect we want to place this code in the sockets module, in the CircusSocket class immediatly after the fd is linked - where we set another flag already

see https://github.com/mozilla-services/circus/blob/master/circus/sockets.py#L71

hasattr is fine I guess

@thunderk
Copy link
Contributor Author

thunderk commented Jun 3, 2014

@tarekziade Thanks, I'll move the code there and open a pull request

@k4nar I won't have access to "close_fds" after the move to sockets module. But I don't think it's a problem, inheritance was the default prior to 3.4 and close_fds should work regardless

tarekziade added a commit that referenced this issue Jun 6, 2014
Fix file descriptor inheritance problem with python3.4 (#787)
@thunderk
Copy link
Contributor Author

thunderk commented Jun 6, 2014

Fix has been merged to master

@thunderk thunderk closed this as completed Jun 6, 2014
@iDevPy
Copy link

iDevPy commented Sep 2, 2014

Hi everyone,

I'm trying to run circus with python 3.4.1 and I'm getting this same error.
This is the content of my circus.ini

[circus]
endpoint = tcp://127.0.0.1:5555
pubsub_endpoint = tcp://127.0.0.1:5556
stats_endpoint = tcp://127.0.0.1:5557

[watcher:settings]
cmd = /root/.virtualenvs/myenv/bin/chaussette settings.wsgi.application
args = --backend tornado --fd $(circus.sockets.settings)
use_sockets = True
numprocesses = 3
copy_env = True
virtualenv = /root/.virtualenvs/myenv/

[socket:settings]
host = 0.0.0.0
port = 8080

This is the output of "circusd circus.ini":

(myenv)[root@server django]# circusd circus.ini
2014-09-02 05:13:50 circus[2595] [INFO] Starting master on pid 2595
2014-09-02 05:13:50 circus[2595] [INFO] sockets started
2014-09-02 05:13:50 circus[2595] [INFO] Arbiter now waiting for commands
2014-09-02 05:13:50 circus[2595] [INFO] settings started
2014-09-02 05:13:50 circus[2595] [INFO] circusd-stats started
2014-09-02 05:13:51 circus[2602] [INFO] Starting the stats streamer
2014-09-02 05:13:51 [2599] [INFO] Application is <django.core.handlers.wsgi.WSGIHandler object at 0x7f21f762b208>
2014-09-02 05:13:51 [2599] [INFO] Serving on fd://7
2014-09-02 05:13:51 [2599] [INFO] Using <class 'chaussette.backend._tornado.Server'> as a backend
Traceback (most recent call last):
File "/root/.virtualenvs/myenv/bin/chaussette", line 9, in
load_entry_point('chaussette==1.2', 'console_scripts', 'chaussette')()
File "/root/.virtualenvs/myenv/lib/python3.4/site-packages/chaussette/server.py", line 203, in main
inner()
File "/root/.virtualenvs/myenv/lib/python3.4/site-packages/chaussette/server.py", line 184, in inner
socket_type=_SOCKET_TYPE[args.socket_type])
File "/root/.virtualenvs/myenv/lib/python3.4/site-packages/chaussette/server.py", line 35, in make_server
server = server_class((host, port), app, **server_class_kwargs)
File "/root/.virtualenvs/myenv/lib/python3.4/site-packages/chaussette/backend/_tornado.py", line 32, in init
set_close_exec(fd)
File "/root/.virtualenvs/myenv/lib/python3.4/site-packages/tornado/platform/posix.py", line 28, in set_close_exec
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
OSError: [Errno 9] Bad file descriptor

And the same error occurs without --backend tornado

@iDevPy
Copy link

iDevPy commented Sep 2, 2014

I fix it adding the code in this page: https://github.com/mozilla-services/circus/pull/800/files

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

4 participants