Django

Code

Ticket #6063 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

BaseHttp server crash since last SVN update

Reported by: Martín Conte Assigned to: mtredinnick
Milestone: Component: django-admin.py runserver
Version: SVN Keywords:
Cc: jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com, chris@moffitts.net, janusz.harkot@gmail.com, gajon@gajon.org Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 0

Description

I'm getting

Traceback (most recent call last):

  File "/usr/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 279, in run
    self.finish_response()

  File "/usr/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 318, in finish_response
    self.write(data)

  File "/usr/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 389, in write
    assert type(data) is StringType,"write() argument must be string"

AssertionError: write() argument must be string

It is happens sice last SVN update. I think this is related to new SafeString? patch. There is my patch.

Attachments

basehttp_safestring.patch (0.8 kB) - added by Martin Conte on 11/30/07 12:00:51.
Safestring is also an string ;)
basehttp_safestring_instance.patch (0.5 kB) - added by Gasper Zejn <zejn@kiberpipa.org> on 11/30/07 12:23:43.
Another patch, oneliner
httpresponse_only_str.diff (0.7 kB) - added by zgoda on 12/02/07 15:43:34.
Naive patch on HttpResponse to convert content to str (just to visualize the idea)
SafeString_remove.diff (476 bytes) - added by janusz.harkot@gmail.com on 12/04/07 13:48:38.
patch that is working for me

Change History

11/30/07 12:00:51 changed by Martin Conte

  • attachment basehttp_safestring.patch added.

Safestring is also an string ;)

11/30/07 12:23:43 changed by Gasper Zejn <zejn@kiberpipa.org>

  • attachment basehttp_safestring_instance.patch added.

Another patch, oneliner

11/30/07 12:26:09 changed by Gasper Zejn <zejn@kiberpipa.org>

  • needs_better_patch changed.
  • has_patch set to 1.
  • needs_tests changed.
  • needs_docs changed.

11/30/07 12:28:19 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [6780]) Fixed some type checks in the development server. Fixed #6063.

11/30/07 13:10:08 changed by aeby

  • status changed from closed to reopened.
  • resolution deleted.
  • needs_tests set to 1.
  • stage changed from Unreviewed to Design decision needed.

This fix only works for the BaseHTTPServer. It is not possible to use FastCGI with flup because there is also an assert inside the BaseFCGIServer write() method:

assert type(data) is str, 'write() argument must be string'

The fix should ensure that only str types are sent to the write() method.

11/30/07 13:45:53 changed by mtredinnick

No, the fix really shouldn't do that. SafeString? is a subclass of str and therefore perfectly safe to pass in. I'll make the same change to the fcgi server as I did to base server.

(follow-up: ↓ 6 ) 11/30/07 13:50:44 changed by mtredinnick

Oh, I see what you're saying: the assert is in flup itself. That's just broken. They should be using isinstance() so that valid subclassing is supported. :-(

This is going to unbelievably hard to fix in Django and we're not actually doing anything wrong. Needs some thought, but filing a bug report with flup would be a help.

(in reply to: ↑ 5 ) 11/30/07 14:02:07 changed by aeby

Replying to mtredinnick:

Oh, I see what you're saying: the assert is in flup itself. That's just broken. They should be using isinstance() so that valid subclassing is supported. :-( This is going to unbelievably hard to fix in Django and we're not actually doing anything wrong. Needs some thought, but filing a bug report with flup would be a help.

Ok, I will send a change request to flup.

11/30/07 18:41:33 changed by aeby

Allan Saddi gave the input that the usage of a string subclass is maybe not spec-compliant:
http://trac.saddi.com/flup/ticket/28

12/01/07 10:34:04 changed by jacob

  • stage changed from Design decision needed to Accepted.

12/01/07 10:55:33 changed by mtredinnick

Can somebody please give me a short example of how to repeat this problem. I can't see how a SafeString instance is leaking out of HttpResponse.

12/01/07 10:55:39 changed by mtredinnick

  • owner changed from nobody to mtredinnick.
  • status changed from reopened to new.

12/02/07 00:43:07 changed by denis <kuzmichyov@gmail.com>

To reproduce the problem it is enough to run trunk copy of django as fastcgi server and setup some flat page for example.

12/02/07 07:20:11 changed by zgoda

  • cc set to jarek.zgoda@gmail.com.

12/02/07 07:30:52 changed by zgoda

The change that causes the problem was introduced in [6778], [6777] works fine.

(follow-up: ↓ 17 ) 12/02/07 14:03:24 changed by mtredinnick

Did [6807] fix this by any chance? I still can't see from reading the code what is causing the problem and I'm not easily able to set up the whole fcgi stuff on my laptop at the moment. So if somebody could test that commit and see, I'd appreciate it.

For anybody wanting to help out here, the thing to work out is where the SafeData is coming from. HttpResponse.content should always have type "str", not SafeString, so where is the bad type leaking from? We aren't about to revert [6778], since it solves another problem, thus we need to plug the leak after we find it.

12/02/07 14:16:23 changed by zgoda

I can confirm this problem still exists in [6844].

12/02/07 14:26:06 changed by aleander@shirk.pl

In newforms-admin, I worked around the problem by replacing source:django/branches/newforms-admin/django/template/__init__.py with the version from [6777].

(in reply to: ↑ 14 ) 12/02/07 14:44:11 changed by zgoda

Replying to mtredinnick:

For anybody wanting to help out here, the thing to work out is where the SafeData is coming from. HttpResponse.content should always have type "str", not SafeString, so where is the bad type leaking from? We aren't about to revert [6778], since it solves another problem, thus we need to plug the leak after we find it.

Well, it looks like Template render() method returns SafeString which in turn is passed to HttpResponse constructor. The most sensible point of ensuring only strings are leaving Django realm seems to me the HttpResponse's constructor (the content should be rendered and escaped anyway, so there's no need to have SafeString here).

12/02/07 15:43:34 changed by zgoda

  • attachment httpresponse_only_str.diff added.

Naive patch on HttpResponse to convert content to str (just to visualize the idea)

12/02/07 22:03:50 changed by Eugene Lazutkin <eugene.lazutkin@gmail.com>

  • cc changed from jarek.zgoda@gmail.com to jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com.

Problem still exists. It is easy to reproduce with flup (manage.py runfcgi) --- any 404 triggers the error.

12/03/07 05:30:44 changed by nesh

  • cc changed from jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com to jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com.

12/04/07 09:18:59 changed by Chris Moffitt <Chris@moffitts.net>

  • cc changed from jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com to jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com, chris@moffitts.net.

12/04/07 13:48:38 changed by janusz.harkot@gmail.com

  • attachment SafeString_remove.diff added.

patch that is working for me

12/04/07 13:50:16 changed by janusz.harkot@gmail.com

  • cc changed from jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com, chris@moffitts.net to jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com, chris@moffitts.net, janusz.harkot@gmail.com.

Hi, I've just posted a very simple patch that is working for me, please check if you get this error message somewhere else.

12/04/07 15:28:41 changed by Gasper Zejn <zejn@kiberpipa.org>

To be honest, I don't really see a problem with isinstance.

The isinstance doc says, that if given a type as a second option, it checks whether type matches:

>>> print isinstance.__doc__
isinstance(object, class-or-type-or-tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).

12/04/07 16:11:11 changed by janusz.harkot@gmail.com

I belive that we do not have any problem with it, but asaddi from http://trac.saddi.com/flup/ticket/28 has :) so there always is a choice... convince him, include modified version of flup withing django tree (painful) or switch to different implementation :) IMHO easiest is to patch django...

12/04/07 19:21:07 changed by mtredinnick

The problem with isinstance() is that it doesn't test purely is something is the type, but also (correctly and in a natural Python fashion) allows subclasses. I will argue that the WSGI spec is completely broken by insisting on a str type in that place, rather than also permitting something that is a subclass of string. Which part of duck-typing don't they understand? However, that is beside the point and we should comply with the spec whenever possible.

12/04/07 19:24:34 changed by Eugene Lazutkin <eugene.lazutkin@gmail.com>

Hear, hear! We can sit and complain about the standard, or we can easily fix it on our side. I agree with Malcolm totally --- let's fix it to comply to the spec. If the spec is updated, we'll update our code as well.

12/04/07 23:59:28 changed by gajon

  • cc changed from jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com, chris@moffitts.net, janusz.harkot@gmail.com to jarek.zgoda@gmail.com, eugene.lazutkin@gmail.com, nesh@studio-quattro.com, chris@moffitts.net, janusz.harkot@gmail.com, gajon@gajon.org.

12/05/07 03:28:56 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [6895]) Fixed #6063 -- Caught one place in HttpResponse that was not returning a str type (violation of WSGI spec). Thanks, janusz.harkot@gmail.com.


Add/Change #6063 (BaseHttp server crash since last SVN update)




Change Properties
Action