# Buggy code
def murder_keepalived(self):
    now = time.time()
    while True:
        try:
            conn = self._keep.popleft()
        except IndexError:
            break

        delta = conn.timeout - now
        if delta > 0:
            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)

            # close the socket
            util.close(conn.sock)


# Traceback (IndexError)
(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

# Fixed code
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 socket from the poller
            self.poller.unregister(conn.sock)

            # close the socket
            util.close(conn.sock)