From 0d28529cb050189af702c92557e6fcd3fa9e3895 Mon Sep 17 00:00:00 2001 From: Lee Burnette Date: Wed, 20 May 2020 12:11:14 -0400 Subject: [PATCH] Fixed two bugs related to gevent + gunicorn + statsd. --- gunicorn/instrument/statsd.py | 2 ++ gunicorn/workers/ggevent.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/gunicorn/instrument/statsd.py b/gunicorn/instrument/statsd.py index afbfd7b4..81619703 100644 --- a/gunicorn/instrument/statsd.py +++ b/gunicorn/instrument/statsd.py @@ -95,6 +95,8 @@ class Statsd(Logger): Logger.access(self, resp, req, environ, request_time) duration_in_ms = request_time.seconds * 1000 + float(request_time.microseconds) / 10 ** 3 status = resp.status + if isinstance(status, bytes): + status = status.decode('utf-8') if isinstance(status, str): status = int(status.split(None, 1)[0]) self.histogram("gunicorn.request.duration", duration_in_ms) diff --git a/gunicorn/workers/ggevent.py b/gunicorn/workers/ggevent.py index 3941814f..385a35e7 100644 --- a/gunicorn/workers/ggevent.py +++ b/gunicorn/workers/ggevent.py @@ -165,7 +165,11 @@ class PyWSGIHandler(pywsgi.WSGIHandler): finish = datetime.fromtimestamp(self.time_finish) response_time = finish - start resp_headers = getattr(self, 'response_headers', {}) - resp = GeventResponse(self.status, resp_headers, self.response_length) + + # Status is expected to be a string but is encoded to bytes in gevent for PY3 + # Except when it isn't because gevent uses hardcoded strings for network errors. + status = self.status.decode() if isinstance(self.status, bytes) else self.status + resp = GeventResponse(status, resp_headers, self.response_length) if hasattr(self, 'headers'): req_headers = self.headers.items() else: