Skip to content

Commit

Permalink
we don't want to block accepted socket. Fix a bug I got with
Browse files Browse the repository at this point in the history
friendpaste. also make worker select timeout related to arbiter.
  • Loading branch information
Benoit Chesneau committed Jan 18, 2010
1 parent 43b9209 commit 4608672
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ def spawn_workers(self):
if i in workers:
continue

worker = Worker(i, self.pid, self.LISTENER, self.modname)
worker = Worker(i, self.pid, self.LISTENER, self.modname,
self.timeout / 2.0)
pid = os.fork()
if pid != 0:
self.WORKERS[pid] = worker
Expand Down
4 changes: 2 additions & 2 deletions gunicorn/http/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def __init__(self, sock, response, req):
def send(self):
# send headers
resp_head = []
resp_head.append("HTTP/1.0 %s\r\n" % (self.status))
resp_head.append("HTTP/1.1 %s\r\n" % (self.status))

resp_head.append("Server: %s\r\n" % self.SERVER_VERSION)
resp_head.append("Date: %s\r\n" % http_date())
# broken clients
resp_head.append("Status: %s\r\n" % str(self.status))
# always close the conenction
#resp_head.append("Connection: close\r\n")
resp_head.append("Connection: close\r\n")
for name, value in self.headers.items():
resp_head.append("%s: %s\r\n" % (name, value))

Expand Down
2 changes: 1 addition & 1 deletion gunicorn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def write(sock, data):
if e[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
break
elif e[0] in (errno.EPIPE,):
break
continue
raise

def write_nonblock(sock, data):
Expand Down
14 changes: 9 additions & 5 deletions gunicorn/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class Worker(object):
"HUP QUIT INT TERM TTIN TTOU USR1".split()
)

def __init__(self, workerid, ppid, socket, app):
def __init__(self, workerid, ppid, socket, app, timeout):
self.id = workerid
self.ppid = ppid
self.timeout = timeout
fd, tmpname = tempfile.mkstemp()
self.tmp = os.fdopen(fd, "r+b")
self.tmpname = tmpname
Expand Down Expand Up @@ -76,7 +77,8 @@ def run(self):
spinner = (spinner+1) % 2
self._fchmod(spinner)
try:
ret = select.select([self.socket], [], [], 2.0)
ret = select.select([self.socket], [], [],
self.timeout)
if ret[0]:
break
except select.error, e:
Expand All @@ -96,7 +98,8 @@ def run(self):
while self.alive:
try:
client, addr = self.socket.accept()

client.setblocking(0)

# handle connection
self.handle(client, addr)

Expand All @@ -105,7 +108,8 @@ def run(self):
spinner = (spinner+1) % 2
self._fchmod(spinner)
except socket.error, e:
if e[0] in [errno.EAGAIN, errno.ECONNABORTED]:
if e[0] in [errno.EAGAIN, errno.ECONNABORTED,
errno.EWOULDBLOCK]:
break # Uh oh!
raise

Expand All @@ -118,6 +122,6 @@ def handle(self, client, addr):
except Exception, e:
self.log.exception("Error processing request. [%s]" % str(e))
# try to send something if an error happend
msg = "HTTP/1.0 500 Internal Server Error\r\n\r\n"
msg = "HTTP/1.1 500 Internal Server Error\r\n\r\n"
util.write_nonblock(client, msg)
util.close(client)

0 comments on commit 4608672

Please sign in to comment.