#6094 closed (fixed)
core handlers do not catch middleware exceptions
Reported by: | Gary Wilson | Owned by: | Gary Wilson |
---|---|---|---|
Component: | HTTP handling | Version: | dev |
Severity: | Keywords: | ||
Cc: | jdunck@…, cmawebsite@…, gav@…, Trevor Caira, mpjung@…, me@…, Maniac@…, sdeasey@…, Carl Meyer, Gonzalo Saavedra, Gert Van Gool, Andrey Golovizin | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This causes bare exceptions to be returned. To reproduce, just put a raise Exception
statement in the process_request
method of a middleware you are using. You will get a traceback like this in your browser:
Traceback (most recent call last): File "/home/gdub/bzr/django/upstream/django/core/servers/basehttp.py", line 277, in run self.result = application(self.environ, self.start_response) File "/home/gdub/bzr/django/upstream/django/core/servers/basehttp.py", line 619, in __call__ return self.application(environ, start_response) File "/home/gdub/bzr/django/upstream/django/core/handlers/wsgi.py", line 205, in __call__ response = self.get_response(request) File "/home/gdub/bzr/django/upstream/django/core/handlers/base.py", line 63, in get_response response = middleware_method(request) File "/home/gdub/bzr/django/upstream/django/middleware/common.py", line 29, in process_request raise Exception Exception
If you put a raise Exception
statement in the process_response
method of a middleware you are using, then you will get a traceback like this in your browser:
Traceback (most recent call last): File "/home/gdub/bzr/django/upstream/django/core/servers/basehttp.py", line 277, in run self.result = application(self.environ, self.start_response) File "/home/gdub/bzr/django/upstream/django/core/servers/basehttp.py", line 619, in __call__ return self.application(environ, start_response) File "/home/gdub/bzr/django/upstream/django/core/handlers/wsgi.py", line 209, in __call__ response = middleware_method(request, response) File "/home/gdub/bzr/django/upstream/django/middleware/common.py", line 62, in process_response raise Exception Exception
WSGI and ModPython handlers are both affected.
Discussion: http://groups.google.com/group/django-developers/browse_thread/thread/255b42846c7e49e9
I would think that we want these exceptions to be handled by the standard exception handling. Should they also be passed through process_exception
middlewares? Currently only exceptions raised in views get passed through process_exception
middlewares.
Attachments (7)
Change History (43)
comment:1 by , 17 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:2 by , 17 years ago
Owner: | changed from | to
---|
by , 17 years ago
comment:3 by , 17 years ago
comment:6 by , 17 years ago
Has patch: | set |
---|
by , 17 years ago
Attachment: | 6094-2.diff added |
---|
follow-up: 10 comment:7 by , 17 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Design decision needed → Accepted |
Gary,
I've reviewed this fairly carefully (I was hoping to check it in) and it's fine. The only minor change I'd like to make is move the resolve() method out of the class and into the handlers.base namespace. It might be useful externally (and it fits better here than in urlresolvers.py, since that is request-agnostic). So just remove the "self" and dedent, etc.
The major problem is the tests don't pass (in either version of the patch). The template tests show failures in all the "url" template tags. I've poked around a bit, but I can't see what's going on there. That needs to be fixed, but I can't keep working on this right now.
So, if you can work out the test problem (which probably suggests somebody bigger going wrong and move resolve() out of the class it's in, feel free to commit.
It's probably worth making a note in "backwards incompatible changes" about the fact that get_response() has changed. It may turn out that somebody has created their own handler subclass and are calling that directly. Just a quick pointer to use handle_request() should be sufficient; it's hardly a major issue.
follow-up: 12 comment:8 by , 17 years ago
(Oh, and if middleware exceptions went through the exception middleware path that would be cool, too. Every successful response goes through the response middleware path, regardless of whether it's generated by a view or a request middleware, so let's be consistent on the exception path, too.)
comment:9 by , 17 years ago
Cc: | added |
---|
comment:10 by , 17 years ago
Patch needs improvement: | unset |
---|
Replying to mtredinnick:
I've reviewed this fairly carefully (I was hoping to check it in) and it's fine. The only minor change I'd like to make is move the resolve() method out of the class and into the handlers.base namespace. It might be useful externally (and it fits better here than in urlresolvers.py, since that is request-agnostic). So just remove the "self" and dedent, etc.
Sounds good, done.
The major problem is the tests don't pass (in either version of the patch). The template tests show failures in all the "url" template tags. I've poked around a bit, but I can't see what's going on there. That needs to be fixed, but I can't keep working on this right now.
So, if you can work out the test problem (which probably suggests somebody bigger going wrong and move resolve() out of the class it's in, feel free to commit.
I'm not seeing any failing tests here. Can someone please confirm this.
It's probably worth making a note in "backwards incompatible changes" about the fact that get_response() has changed. It may turn out that somebody has created their own handler subclass and are calling that directly. Just a quick pointer to use handle_request() should be sufficient; it's hardly a major issue.
Agreed.
by , 17 years ago
Attachment: | 6094.3.diff added |
---|
comment:11 by , 17 years ago
updated patch, 6094.3.diff:
- resolve method moved to a function of handles.base.
- Added a couple docstrings, including ascii chart of Jacob's request-handling image from OSCON 2007.
comment:12 by , 17 years ago
Replying to mtredinnick:
(Oh, and if middleware exceptions went through the exception middleware path that would be cool, too. Every successful response goes through the response middleware path, regardless of whether it's generated by a view or a request middleware, so let's be consistent on the exception path, too.)
Yeah, I was wondering if this should happen or not. Probably should be its own ticket and would need some thought about if or how it would affect existing middleware, such as TransactionMiddleware.
comment:13 by , 17 years ago
My only other concern with this is that I haven't tested in a ModPython setup. I don't think there would be problems, but just would like to have some confirmation.
comment:14 by , 17 years ago
Cc: | added |
---|
by , 17 years ago
Attachment: | http_refactor_r7320.patch added |
---|
Patch cleanup to work with r7320, no other changes made from previous.
comment:15 by , 17 years ago
I just tested the updated patch with mod_wsgi and mod_python, both did what was expected (threw the proper 500 built-in page).
comment:16 by , 16 years ago
milestone: | → 1.0 |
---|---|
Status: | new → assigned |
marked #2481 as a dup, but there was an interesting idea for a configurable exception handler in the comments there.
comment:17 by , 16 years ago
Cc: | added |
---|
comment:18 by , 16 years ago
Cc: | added |
---|
follow-up: 23 comment:19 by , 16 years ago
milestone: | 1.0 → post-1.0 |
---|
Pushing post-1.0: at this point this will destabilize the core handler a bit too much for my liking. We've lived with this annoyance for a while; we can deal with it for a bit longer still.
comment:20 by , 16 years ago
Cc: | added |
---|
comment:21 by , 16 years ago
Cc: | added |
---|
comment:23 by , 16 years ago
Cc: | added |
---|
Replying to jacob:
Pushing post-1.0: at this point this will destabilize the core handler a bit too much for my liking. We've lived with this annoyance for a while; we can deal with it for a bit longer still.
It's now post 1.0 (and post 1.1, I guess). Any chance you can take another look at this?
comment:24 by , 16 years ago
Cc: | added |
---|
comment:25 by , 16 years ago
Cc: | added |
---|
comment:26 by , 16 years ago
Cc: | added |
---|
comment:27 by , 15 years ago
Cc: | added |
---|
comment:28 by , 15 years ago
I've made an alternative patch that is much less involving than the previous one. Basically it just shoves request middleware handling under try
statements in BaseHandler.
Refactoring of the request part is a good thing by itself but I think it holds this actual bug from fixing. It can be done after it's closed.
comment:29 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:30 by , 15 years ago
comment:31 by , 15 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
After [12186] it is broken again - reopening.
comment:32 by , 15 years ago
Cc: | added |
---|
comment:33 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Please open a new ticket describing the current problem. It's hard to determine what is meant by "broken again"; a fresh problem that actually describes the current situation is much better than reopening a ticket with 2 years of history to dig through to figure out what you might be trying to report.
comment:34 by , 15 years ago
Do you want me to copy&paste the description of this ticket into a new one? Fine.
comment:35 by , 15 years ago
Sigh. I'd like a description of what made you even go find this ticket. I tried the first thing in the description -- putting an exception in a middleware process_request -- and got a debug page, not a bare exception. So something has been fixed, and it would help if you could explain exactly what you see that has not been fixed.
Patch notes:
handle_response
method toBaseHandler
to factor out duplicated code inModPythonHandler
,WSGIHandler
, andClientHandler
.resolver
temporary variable with aresolver
method to ease refactoring ofget_response
method.get_response
method into a more generic method that will apply the standard exception handling to any passed method.Client
class configurable for reraising request-handling exceptions or not.