starlette-session icon indicating copy to clipboard operation
starlette-session copied to clipboard

Redis or Memcache backend key not found, request.session will be None

Open chenlujia888 opened this issue 2 years ago • 2 comments

When redis or memcache clear the key/value data, session_backend.get() will return None. as the request.session will be None. None object cannot call update(). This bug happens when the redis server migrate to the new server with no data, but client brower send the original session key, then exception rasied. Looking into the code, on init.py line 80-96: `

    connection = HTTPConnection(scope)
    initial_session_was_empty = True

    if self.cookie_name in connection.cookies:
        data = connection.cookies[self.cookie_name].encode("utf-8")
        try:
            data = self.signer.unsign(data, max_age=self.max_age)
            if self.backend_type == BackendType.cookie or not self.session_backend:
                scope["session"] = json.loads(b64decode(data))
            else:
                session_key = json.loads(b64decode(data)).get(
                    self._cookie_session_id_field
                )
                scope["session"] = await self.session_backend.get(session_key)
                scope["__session_key"] = session_key

######### SHOULD ADD THE CODE HERE

               if scope["session“] == None:
                       scope["session"] = {}

################################

            initial_session_was_empty = False

`

chenlujia888 avatar Dec 17 '22 07:12 chenlujia888

I've hit this too, it's quite serious it essentially makes the library unusable

gmmeyer avatar Dec 29 '22 19:12 gmmeyer

@auredentan Is this project still being maintained? @gmmeyer's pull request looks good, but it's been open for quite a while.

yfa-vagelis avatar Aug 09 '23 13:08 yfa-vagelis