mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
move close_on_exec in util
This commit is contained in:
parent
2f959f9251
commit
354a91ad35
@ -15,8 +15,6 @@ import time
|
|||||||
|
|
||||||
from gunicorn.worker import Worker
|
from gunicorn.worker import Worker
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Arbiter(object):
|
class Arbiter(object):
|
||||||
|
|
||||||
LISTENER = None
|
LISTENER = None
|
||||||
|
|||||||
@ -4,6 +4,8 @@
|
|||||||
# See the NOTICE for more information.
|
# See the NOTICE for more information.
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
|
import fcntl
|
||||||
|
import os
|
||||||
import select
|
import select
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
@ -21,6 +23,11 @@ monthname = [None,
|
|||||||
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
||||||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
||||||
|
|
||||||
|
|
||||||
|
def close_on_exec(fd):
|
||||||
|
flags = fcntl.fcntl(fd, fcntl.F_GETFD) | fcntl.FD_CLOEXEC
|
||||||
|
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
|
||||||
|
|
||||||
def close(sock):
|
def close(sock):
|
||||||
""" socket.close() doesn't *really* close if
|
""" socket.close() doesn't *really* close if
|
||||||
there's another reference to it in the TCP/IP stack.
|
there's another reference to it in the TCP/IP stack.
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
import fcntl
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import select
|
import select
|
||||||
@ -35,25 +34,20 @@ class Worker(object):
|
|||||||
self.tmpname = tmpname
|
self.tmpname = tmpname
|
||||||
|
|
||||||
# prevent inherientence
|
# prevent inherientence
|
||||||
self.close_on_exec(socket)
|
|
||||||
self.close_on_exec(fd)
|
|
||||||
|
|
||||||
|
|
||||||
# Set blocking to 0 back since we
|
|
||||||
# prevented inheritence of the socket
|
|
||||||
# the socket.
|
|
||||||
socket.setblocking(0)
|
|
||||||
|
|
||||||
self.socket = socket
|
self.socket = socket
|
||||||
self.address = socket.getsockname()
|
util.close_on_exec(self.socket)
|
||||||
|
self.socket.setblocking(0)
|
||||||
|
|
||||||
|
|
||||||
|
util.close_on_exec(fd)
|
||||||
|
|
||||||
|
|
||||||
|
self.address = self.socket.getsockname()
|
||||||
|
|
||||||
self.app = app
|
self.app = app
|
||||||
self.alive = True
|
self.alive = True
|
||||||
self.log = logging.getLogger(__name__)
|
self.log = logging.getLogger(__name__)
|
||||||
|
|
||||||
def close_on_exec(self, fd):
|
|
||||||
flags = fcntl.fcntl(fd, fcntl.F_GETFD) | fcntl.FD_CLOEXEC
|
|
||||||
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
|
|
||||||
|
|
||||||
def init_signals(self):
|
def init_signals(self):
|
||||||
map(lambda s: signal.signal(s, signal.SIG_DFL), self.SIGNALS)
|
map(lambda s: signal.signal(s, signal.SIG_DFL), self.SIGNALS)
|
||||||
@ -97,9 +91,17 @@ class Worker(object):
|
|||||||
self._fchmod(spinner)
|
self._fchmod(spinner)
|
||||||
nr += 1
|
nr += 1
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
if e[0] in [errno.EAGAIN, errno.ECONNABORTED,
|
if e[0] in (errno.EAGAIN, errno.EWOULDBLOCK,
|
||||||
errno.EWOULDBLOCK]:
|
errno.ECONNABORTED):
|
||||||
break # Uh oh!
|
break # Uh oh!
|
||||||
|
elif e[0] in (errno.EMFILE, errno.ENOBUFS, errno.ENFILE,
|
||||||
|
errno.ENOMEM):
|
||||||
|
"""
|
||||||
|
linux gave EMFILE when a process is not allowed to
|
||||||
|
allocate more fs or ENOMEM when there is not enough
|
||||||
|
memory to allocate. BSD return ENOBUFS.
|
||||||
|
"""
|
||||||
|
log.info("Could not accept new connection (%s)" % str(e))
|
||||||
raise
|
raise
|
||||||
if nr == 0: break
|
if nr == 0: break
|
||||||
|
|
||||||
@ -122,7 +124,7 @@ class Worker(object):
|
|||||||
self._fchmod(spinner)
|
self._fchmod(spinner)
|
||||||
|
|
||||||
def handle(self, client, addr):
|
def handle(self, client, addr):
|
||||||
self.close_on_exec(client)
|
#util.close_on_exec(client)
|
||||||
try:
|
try:
|
||||||
req = http.HttpRequest(client, addr, self.address)
|
req = http.HttpRequest(client, addr, self.address)
|
||||||
response = self.app(req.read(), req.start_response)
|
response = self.app(req.read(), req.start_response)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user