[mod_python] Strange bug in req.sendfile

David Fraser davidf at sjsoft.com
Thu Nov 11 05:51:51 EST 2004

I'm having a wierd bug on Windows XP with sendfile.
I'm trying to send an image and the image gets corrupted. Sending the 
bytes directly through write works fine.
I've simplified my handler down to bare-bones level and the bug still 
Even leaving out content_type and send_http_header doesn't fix it...
I've tried to reproduce this problem on Linux and Windows 2000 to no 
avail ... not sure if it is the platform or a bug on this particular 
machine, but it sure is odd.
The file that is sent has exactly the same length but certain bytes are 
changed in it. They seem to be at fairly regular offsets, so it may be a 
bug in Apache's sendfile routine. The mod_python req.sendfile method 
looks fairly simple so I doubt its that.
In at least one case (animals2_corrupted1.jpg), it was exact 8k chunks 
of files being replaced with zeroes, but it didn't seem to be consistent.
In another case, (animals2_corrupted2.jpg), 4 bytes at n*0x1000+0x10 
where replaced with 0x90008112, for various n, and one 8k block was 
replaced by 0s and sent in place of the next 8k block.
I have tested with Firefox and wget to confirm that the problem is not 
browser-related. I couldn't find any significant difference in the 
headers between the host that worked and the one that didn't (both using 
Transfer-Encoding: chunked).
Using latest Apache 2.0.52 and mod_python 3.1.3 (with some minor mods 
but all python, none to the sendfile method.)

Here's the handler:

def handler(req):
    """return a page that will be sent to the user"""
    req.content_type = "image/jpeg"
    jpegfile = os.path.join(os.path.dirname(__file__), 'animals2.jpg')
    # req.write(open(jpegfile, 'rb').read())
    return apache.OK

The original file, animals2.jpg and some munged versions of it, 
animals2_corrupted{1,2,3,4}.jpg are available here:
There are also hex dumps and diffs between the hex dumps in case they're 

Any ideas appreciated


More information about the Mod_python mailing list