Code Monkey home page Code Monkey logo

Comments (4)

Cito avatar Cito commented on May 24, 2024

Thanks for reporting, I see the problem:

    accessTimeAndKeys = []
    for key in self._memoryStore:
        try:
            accessTimeAndKeys.append(
                (self._memoryStore[key].lastAccessTime(), key))
        except KeyError:
            pass
    return map(itemgetter(1), sorted(accessTimeAndKeys))

If another thread (like the session sweeper) deletes a session from the store while the for loop is running, then this will fail with a "dictionary changed size" Runtime error. Catching KeyError will not help here. With your patch, this will work because a KeyError is raised in this case.

Maybe the following would be a bit more elegant as it's shorter and avoids catching the KeyError?

    return [item[0] for item in sorted((session.lastAccessTime(), key)
        for key, session in self._memoryStore.items())]

Or maybe this is even more elegant:

    return [session.identifier() for session in sorted(
        self._memoryStore.values(), key=lambda v: v.lastAccessTime())]

We could run the loop inside with self._lock as we do in other methods, but that would hurt performance.

from w4py.

benparker avatar benparker commented on May 24, 2024

I like your last option. It's compact and DRY, because the base definition of SessionStore.values() already has the loop with an internal try...except to catch missing keys.

Note that there are other places in SessionDynamicStore which also iterate over self.keys(). I don't think it's a construct that needs to be eliminated entirely, especially within a SessionStore that manages it's own internals.

edit ---> So as soon as I clicked "Update comment" I realized that the base definitions of SessionStore.values() and SessionStore.items() both appear to have the same bug as they iterate over self instead of self.keys()

from w4py.

Cito avatar Cito commented on May 24, 2024

... realized that the base definitions of SessionStore.values() and SessionStore.items() both appear to have the same bug ...

Fixed this in a second commit. There was also another one in SessionMemoryStore. Can you confirm that everything is fixed with the last two commits?

from w4py.

benparker avatar benparker commented on May 24, 2024

Looks good to me and a better overall fix than my initial submission. Thanks!

from w4py.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.