EOFError when loading empty session files #83

Open
leres opened this Issue Jul 7, 2011 · 10 comments

Comments

Projects
None yet
5 participants
@leres

leres commented Jul 7, 2011

It appears for whatever reason I occasionally end up empty web.py session files:

File "/usr/local/lib/python2.7/site-packages/web/session.py", line 199, in decode, referer: https://onestop.lbl.gov/onestop/
    return pickle.loads(pickled), referer: https://xyz.lbl.gov/webapp/
EOFError, referer: https://xyz.lbl.gov/webapp/

It seems like web.py could catch the EOFError and pretend there was no session file. If that isn't right, should I catch the call to web.session.Session()?

if web.config.get('_session') is None:
    session = web.session.Session(APP,
        web.session.DiskStore(os.path.join(ROOT, 'sessions')), {'user': 'anonymous'})
    web.config._session = session
else:
    session = web.config._session

What's the preferred way to deal with this?

@reustle

This comment has been minimized.

Show comment Hide comment
@reustle

reustle Aug 12, 2011

I continue to have this problem as well, but it seems to only happen when running locally.

reustle commented Aug 12, 2011

I continue to have this problem as well, but it seems to only happen when running locally.

@nadahalli

This comment has been minimized.

Show comment Hide comment
@nadahalli

nadahalli Aug 25, 2011

I have this problem as well. Bumping it up.

I have this problem as well. Bumping it up.

@leres

This comment has been minimized.

Show comment Hide comment
@leres

leres Nov 5, 2011

I still see this happening, sometimes as often as several times a week, is there a workaround?

def decode(self, session_data):
    """decodes the data to get back the session dict """
    pickled = base64.decodestring(session_data)
    return pickle.loads(pickled)

leres commented Nov 5, 2011

I still see this happening, sometimes as often as several times a week, is there a workaround?

def decode(self, session_data):
    """decodes the data to get back the session dict """
    pickled = base64.decodestring(session_data)
    return pickle.loads(pickled)
@reustle

This comment has been minimized.

Show comment Hide comment
@reustle

reustle Nov 5, 2011

When running locally, I pipe /static/ through webpy. This causes this error to happen at least every other request (since it is pushing 20~ resources through webpy). That should help whoever is digging deeper into this reproduce it easily.

reustle commented Nov 5, 2011

When running locally, I pipe /static/ through webpy. This causes this error to happen at least every other request (since it is pushing 20~ resources through webpy). That should help whoever is digging deeper into this reproduce it easily.

@leres

This comment has been minimized.

Show comment Hide comment
@leres

leres Feb 8, 2012

I'm still getting burned by this problem.

leres commented Feb 8, 2012

I'm still getting burned by this problem.

@reustle

This comment has been minimized.

Show comment Hide comment
@reustle

reustle Feb 9, 2012

This got too annoying. We ripped out WebPy completely.

reustle commented Feb 9, 2012

This got too annoying. We ripped out WebPy completely.

@heshiming

This comment has been minimized.

Show comment Hide comment
@heshiming

heshiming Mar 5, 2012

I've got this problem too. It appears when there are simultaneous access to the session file. I guess it's a thread-safe issue.

I've got this problem too. It appears when there are simultaneous access to the session file. I guess it's a thread-safe issue.

@heshiming

This comment has been minimized.

Show comment Hide comment
@heshiming

heshiming Mar 5, 2012

webpy disk session file should not be an empty file, it's a pickle serialized object. So whenever EOF is encountered, it should be always because another thread is trying to write, thus truncating the size to zero.

I've got a simple fix in session.py's DiskSession, change:

def __getitem__(self, key):
    path = self._get_path(key)
    if os.path.exists(path): 
        pickled = open(path).read()
        return self.decode(pickled)
    else:
        raise KeyError, key

to:

def __getitem__(self, key):
    path = self._get_path(key)
    if os.path.exists(path): 
        while True:
            try:
                pickled = open(path).read()
                return self.decode(pickled)
            except EOFError:
                time.sleep(0.1)
    else:
        raise KeyError, key

It's a bit ugly. But since we are dealing with files with no concurrency support, this shall be as good as it gets. It helps to keep stuff simple without tapping into a full-fledged db like mysql too early, just for sessions.

webpy disk session file should not be an empty file, it's a pickle serialized object. So whenever EOF is encountered, it should be always because another thread is trying to write, thus truncating the size to zero.

I've got a simple fix in session.py's DiskSession, change:

def __getitem__(self, key):
    path = self._get_path(key)
    if os.path.exists(path): 
        pickled = open(path).read()
        return self.decode(pickled)
    else:
        raise KeyError, key

to:

def __getitem__(self, key):
    path = self._get_path(key)
    if os.path.exists(path): 
        while True:
            try:
                pickled = open(path).read()
                return self.decode(pickled)
            except EOFError:
                time.sleep(0.1)
    else:
        raise KeyError, key

It's a bit ugly. But since we are dealing with files with no concurrency support, this shall be as good as it gets. It helps to keep stuff simple without tapping into a full-fledged db like mysql too early, just for sessions.

@eduardohenriquearnold

This comment has been minimized.

Show comment Hide comment
@eduardohenriquearnold

eduardohenriquearnold Apr 8, 2013

Is there any solution for this problem (besides changing the source code) ?

It does seems to be a problem with concurrency, so why don't they change the source to fix this?

Is there any solution for this problem (besides changing the source code) ?

It does seems to be a problem with concurrency, so why don't they change the source to fix this?

@eduardohenriquearnold

This comment has been minimized.

Show comment Hide comment
@eduardohenriquearnold

eduardohenriquearnold Apr 10, 2013

Same as issue #191
Check there for a better solution.

Same as issue #191
Check there for a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment