Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chenlujia888 opened this issue Dec 17, 2022 · 3 comments · May be fixed by #13
Open

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

chenlujia888 opened this issue Dec 17, 2022 · 3 comments · May be fixed by #13

Comments

@chenlujia888
Copy link

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

`

@gmmeyer
Copy link

gmmeyer commented Dec 29, 2022

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

@gmmeyer gmmeyer linked a pull request Dec 29, 2022 that will close this issue
@yfa-vagelis
Copy link

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

@Redevil10
Copy link

Redevil10 commented Jul 4, 2024

I had the same error when I started using it with redis backend, I thought there was a bug.
But then I realised it is probably because I didn't use the correct way to update and read the session.

It works if you try to set the session by following the document:

request.session.update({"some_key": "some_value"})

and access the value by:

value = request.session.get("some_key")

Initially, I was doing it the wrong way, treat it as a dict:

request.session["some_key"] = "some_value"
value = request.session["some_key"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants