Repository: benoitc/gunicorn

Issue:
gunicorn 19.0 threaded worker raises exceptions on start
```
(wsgiapps)sontek@aladdin:~/src/wsgiapps/pyramid_hello$ gunicorn --paste development.ini  --log-file=-
2014-07-12 00:10:42 [4752] [INFO] Starting gunicorn 19.0.0
2014-07-12 00:10:42 [4752] [INFO] Listening at: http://0.0.0.0:8000 (4752)
2014-07-12 00:10:42 [4752] [INFO] Using worker: threads
2014-07-12 00:10:42 [4803] [INFO] Booting worker with pid: 4803
2014-07-12 00:10:42 [4806] [INFO] Booting worker with pid: 4806
2014-07-12 00:10:42 [4831] [INFO] Booting worker with pid: 4831
2014-07-12 00:10:42 [4844] [INFO] Booting worker with pid: 4844
2014-07-12 00:10:42 [4885] [INFO] Booting worker with pid: 4885
2014-07-12 00:10:46 [4885] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 502, in spawn_worker
    worker.init_process()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/gthread.py", line 109, in init_process
    super(ThreadWorker, self).init_process()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 120, in init_process
    self.run()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/gthread.py", line 178, in run
    self.murder_keepalived()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/gthread.py", line 147, in murder_keepalived
    conn = self._keep.popleft()
IndexError: pop from an empty deque
Traceback (most recent call last):
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 502, in spawn_worker
    worker.init_process()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/gthread.py", line 109, in init_process
    super(ThreadWorker, self).init_process()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 120, in init_process
    self.run()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/gthread.py", line 178, in run
    self.murder_keepalived()
  File "/home/sontek/venvs/wsgiapps/local/lib/python2.7/site-packages/gunicorn/workers/gthread.py", line 147, in murder_keepalived
    conn = self._keep.popleft()
IndexError: pop from an empty deque

```



Discussion:
@benoitc looks like this slipped in during that refactoring and I missed it. The problem appears to be that we call `popleft()` again even though we already did and stored the result in the variable `conn`.


Resolving patch:
diff --git a/gunicorn/workers/gthread.py b/gunicorn/workers/gthread.py
--- a/gunicorn/workers/gthread.py
+++ b/gunicorn/workers/gthread.py
@@ -134,18 +134,17 @@ def murder_keepalived(self):
         now = time.time()
         while True:
             try:
+                # remove the connection from the queue
                 conn = self._keep.popleft()
             except IndexError:
                 break
 
             delta = conn.timeout - now
             if delta > 0:
+                # add the connection back to the queue
                 self._keep.appendleft(conn)
                 break
             else:
-                # remove the connection from the queue
-                conn = self._keep.popleft()
-
                 # remove the socket from the poller
                 self.poller.unregister(conn.sock)
 


Test patch: