Repository: benoitc/gunicorn

Issue:
Error handling all requests on master - attempt to read attribute of NoneType
I'm trying to run Gunicorn on the master branch (latest commit is 4c601ce447fafbeed27f0f0a238e0e48c928b6f9). But every incoming request generates this error:

```
[2014-10-27 20:36:55 +0000] [22663] [ERROR] Error handling request
Traceback (most recent call last):
  File "/mnt/runscope/.virtualenvs/embedcurl/src/gunicorn/gunicorn/workers/async.py", line 41, in handle
    proxy_protocol_info = req.proxy_protocol_info
AttributeError: 'NoneType' object has no attribute 'proxy_protocol_info'
```

This is my gunicorn command line:

```
/usr/local/runscope/.virtualenvs/embedcurl/bin/gunicorn \
    --name embedcurl -k gevent --workers=2 --bind 0.0.0.0:3002 \
    --error-logfile /var/log/runscope/embedcurl.error.log \
    --access-logfile /var/log/runscope/embedcurl.access.log \
    --access-logformat '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s %(T)s %(D)s "%(f)s" "%(a)s"' \
    --max-requests 50000 \
    --max-requests-jitter 500 \
    --statsd-prefix service.embedcurl.test004. \
    -D "embedcurl:app" --pid /var/run/embedcurl.pid
```

I think the problem comes from commit adf353f213d12994cc36ecbbcb6a084baf1fda12. See the file https://github.com/benoitc/gunicorn/blob/adf353f213d12994cc36ecbbcb6a084baf1fda12/gunicorn/workers/async.py -- line 41 reads from `req.proxy_protocol_info` but `req` is always None when that line runs. I'm not familiar with `proxy_protocol_info` myself so I'm not quite sure what the fix is.

Error handling all requests on master - attempt to read attribute of NoneType
I'm trying to run Gunicorn on the master branch (latest commit is 4c601ce447fafbeed27f0f0a238e0e48c928b6f9). But every incoming request generates this error:

```
[2014-10-27 20:36:55 +0000] [22663] [ERROR] Error handling request
Traceback (most recent call last):
  File "/mnt/runscope/.virtualenvs/embedcurl/src/gunicorn/gunicorn/workers/async.py", line 41, in handle
    proxy_protocol_info = req.proxy_protocol_info
AttributeError: 'NoneType' object has no attribute 'proxy_protocol_info'
```

This is my gunicorn command line:

```
/usr/local/runscope/.virtualenvs/embedcurl/bin/gunicorn \
    --name embedcurl -k gevent --workers=2 --bind 0.0.0.0:3002 \
    --error-logfile /var/log/runscope/embedcurl.error.log \
    --access-logfile /var/log/runscope/embedcurl.access.log \
    --access-logformat '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s %(T)s %(D)s "%(f)s" "%(a)s"' \
    --max-requests 50000 \
    --max-requests-jitter 500 \
    --statsd-prefix service.embedcurl.test004. \
    -D "embedcurl:app" --pid /var/run/embedcurl.pid
```

I think the problem comes from commit adf353f213d12994cc36ecbbcb6a084baf1fda12. See the file https://github.com/benoitc/gunicorn/blob/adf353f213d12994cc36ecbbcb6a084baf1fda12/gunicorn/workers/async.py -- line 41 reads from `req.proxy_protocol_info` but `req` is always None when that line runs. I'm not familiar with `proxy_protocol_info` myself so I'm not quite sure what the fix is.



Discussion:
Looks like a real bug. Marking it for R19.2. Thanks for the report and the diagnosis.

Looks like a real bug. Marking it for R19.2. Thanks for the report and the diagnosis.


Resolving patch:
diff --git a/gunicorn/workers/async.py b/gunicorn/workers/async.py
--- a/gunicorn/workers/async.py
+++ b/gunicorn/workers/async.py
@@ -38,14 +38,17 @@ def handle(self, listener, client, addr):
                     self.handle_request(listener_name, req, client, addr)
                 else:
                     # keepalive loop
-                    proxy_protocol_info = req.proxy_protocol_info
+                    proxy_protocol_info = {}
                     while True:
                         req = None
                         with self.timeout_ctx():
                             req = six.next(parser)
                         if not req:
                             break
-                        req.proxy_protocol_info = proxy_protocol_info
+                        if req.proxy_protocol_info:
+                            proxy_protocol_info = req.proxy_protocol_info
+                        else:
+                            req.proxy_protocol_info = proxy_protocol_info
                         self.handle_request(listener_name, req, client, addr)
             except http.errors.NoMoreData as e:
                 self.log.debug("Ignored premature client disconnection. %s", e)


Test patch: