diff --git a/gunicorn/config.py b/gunicorn/config.py index 292f7275..726840ba 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -315,12 +315,14 @@ def validate_post_request(val): val = validate_callable(-1)(val) largs = len(inspect.getargspec(val)[0]) - if largs == 3: + if largs == 4: return val + elif largs == 3: + return lambda worker, req, env, _r: val(worker, req, env) elif largs == 2: - return lambda worker, req, _: val(worker, req) + return lambda worker, req, _e, _r: val(worker, req) else: - raise TypeError("Value must have an arity of: 3") + raise TypeError("Value must have an arity of: 4") @@ -1022,7 +1024,7 @@ class PostRequest(Setting): section = "Server Hooks" validator = validate_post_request type = "callable" - def post_request(worker, req, environ): + def post_request(worker, req, environ, resp): pass default = staticmethod(post_request) desc = """\ diff --git a/gunicorn/workers/async.py b/gunicorn/workers/async.py index 85ac7854..59bffaca 100644 --- a/gunicorn/workers/async.py +++ b/gunicorn/workers/async.py @@ -74,6 +74,8 @@ class AsyncWorker(base.Worker): def handle_request(self, listener, req, sock, addr): request_start = datetime.now() + environ = {} + resp = None try: self.cfg.pre_request(self, req) resp, environ = wsgi.create(req, sock, addr, @@ -103,7 +105,7 @@ class AsyncWorker(base.Worker): raise StopIteration() finally: try: - self.cfg.post_request(self, req, environ) - except: - pass + self.cfg.post_request(self, req, environ, resp) + except Exception: + self.log.exception("Exception in post_request hook") return True diff --git a/gunicorn/workers/sync.py b/gunicorn/workers/sync.py index 61291d96..a72997c2 100644 --- a/gunicorn/workers/sync.py +++ b/gunicorn/workers/sync.py @@ -107,6 +107,7 @@ class SyncWorker(base.Worker): def handle_request(self, listener, req, client, addr): environ = {} + resp = None try: self.cfg.pre_request(self, req) request_start = datetime.now() @@ -141,6 +142,6 @@ class SyncWorker(base.Worker): return finally: try: - self.cfg.post_request(self, req, environ) - except: - pass + self.cfg.post_request(self, req, environ, resp) + except Exception: + self.log.exception("Exception in post_request hook") diff --git a/tests/test_003-config.py b/tests/test_003-config.py index 50933e8e..b2431a12 100644 --- a/tests/test_003-config.py +++ b/tests/test_003-config.py @@ -192,3 +192,25 @@ def test_cli_overrides_config(): app = NoConfigApp() t.eq(app.cfg.bind, ["blarney"]) t.eq(app.cfg.proc_name, "fooey") + + +def test_post_request(): + c = config.Config() + + def post_request_4(worker, req, environ, resp): + return 4 + + def post_request_3(worker, req, environ): + return 3 + + def post_request_2(worker, req): + return 2 + + c.set("post_request", post_request_4) + t.eq(4, c.post_request(1, 2, 3, 4)) + + c.set("post_request", post_request_3) + t.eq(3, c.post_request(1, 2, 3, 4)) + + c.set("post_request", post_request_2) + t.eq(2, c.post_request(1, 2, 3, 4))