Problem with cherrypy.session and serve_download

81 views
Skip to first unread message

Zap

unread,
May 5, 2009, 2:48:05 AM5/5/09
to cherrypy-users
Hello great cherrypy community :)

In the past I ran into the problem that session handling and the
method serve_download() doesn't work well
together. The download of files bigger 100 MB results in an empty file
downloaded by the browser.
I got no error, neither in the cherrypy-log nor on the browser side.
Just this empty file saved on the harddisk.

This small test script should whow this behaviour: (note: you need a
big file to serve)

import os
import cherrypy
from cherrypy.lib.static import serve_download

THIS_DIR = os.path.abspath(os.path.dirname(__file__))

class HelloWorld(object):
_cp_config = {"tools.sessions.on": True}
#_cp_config = {} # uncomment this and download will work fine
def index(self):
try:
cherrypy.session["hello"] = "hello"
except:
pass
return serve_download(os.path.join(THIS_DIR, "bigfile.bin"))
index.exposed = True
cherrypy.config.update({'server.socket_host': '0.0.0.0',
'server.socket_port': 8080,
})
cherrypy.quickstart(HelloWorld())

Is this a known issue or a limitation we have to live with?

My workaround makes a HTTPRedirect to a download method which is not
part of an object with session handling activated. This seems to work
in general but some clients told me they still download files with a
size of zero bytes sometimes. After some retries the download will
work right but it seems to be some kind of unstable.
Do you have any experience with this unstable behaviours, too?

Best regards,
Zap

Dirk Rothe

unread,
May 6, 2009, 1:03:44 AM5/6/09
to cherrypy-users
I was hit by a similar phenomenon on a Windows box as described here:
http://www.cherrypy.org/ticket/909

--dirk

Zap

unread,
May 7, 2009, 12:03:11 AM5/7/09
to cherrypy-users
I've checked dirks example from http://www.cherrypy.org/ticket/909
and with a size of 128 MB the download via string failed (With 127 MB
it still works).
That is reproducable on my system (Windows XP SP3, Python2.5,
CherryPy3.1.2)
The download via file failed after some retries and occured on a size
of 200 MB.

Zap

unread,
Jun 18, 2009, 1:09:54 AM6/18/09
to cherrypy-users
I did a monkey patch in my script for the issue described in ticket
#909

Perhaps you're interested to assume the procedure in the cherrypy
sources.
Fact is, I could reproduce the bug on different pc's running
WindowXP
and this workaround helps so far.

import socket
from cherrypy.wsgiserver import CP_fileobject

CP_fileobject_sendall = CP_fileobject.sendall
def _sendall(self, data):
""" Monkeypatch for cherrypy ticket #909 """
try:
CP_fileobject_sendall(self, data)
except socket.error, e:
if e.args[0] == 10055:
# 10055 -> "No buffer space available"
# try to send the data in smaller parts
block = 1024 ** 2 # 1 MB
for pos in xrange(0, len(data), block):
CP_fileobject_sendall(self, data[pos:pos+block])
else:
raise
CP_fileobject.sendall = _sendall

Zap

On 7 Mai, 06:03, Zap <z...@lancraft.net> wrote:
> I've checked dirks example fromhttp://www.cherrypy.org/ticket/909
Reply all
Reply to author
Forward
0 new messages