Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The request's session was deleted before the request completed. The user may have logged out in a concurrent request, for example

Tags:

python

django

I have a Python/Django app which sometimes has more than 100 users logged in. One day, I found this in Django error log:

The request's session was deleted before the request completed. 
The user may have logged out in a concurrent request, for example.

Although the message is written in quite understandable English, I have no idea

  • what actually happened
  • why it happened
  • whether I need to worry about it
  • if yes, how can I prevent this from happening again

I found a question with almost same title but the difference is that I do not have anything about caching in my settings.

If you need any pieces of code, just please let me know in the comments.

Thanks for your time!

like image 586
karlosss Avatar asked Oct 27 '17 20:10

karlosss


3 Answers

What actually happened: A user's session was destroyed (i.e., they logged out, or the session expired) while the same user made a request with the same session key.

Why it happened: For example, it could happen if the user had two tabs open, and logged out in one of the tabs, while a request was also made from another tab. If both happened in quick succession then one would hit this error.

Do you need to worry about it?: Not unless you see lots of events like this in the logs, in which case there is something wrong. If you found the error just once, then it's nothing to worry about.

like image 188
solarissmoke Avatar answered Oct 21 '22 13:10

solarissmoke


This can also happen because you store session in a dummy cache Backend.

eg :

If You have configured "DummyCache" as your default Cache system or if you have SESSION_CACHE_ALIAS points to dummy cache, There is a chance that, these sesion data can get deleted even in between your request response cycle. That is, Your Request reached Djagno server and is actively under processing.

PROBLEM

Your settings seems to be in any of this possible configuration.

Case A:

# Possible Current Configuration 
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        'LOCATION': 'unique-snowflake',
     }
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"

Or

# Another Possible Current Configuration 
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
              "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    'cache_backend_for_user_session': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        'LOCATION': 'unique-snowflake',
    }
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "cache_backend_for_user_session"

SOLUTION

I hope, now you got the solution, If the Session Engine Depends On Cache, It is better not to point them to DummyCache.

You can use SESSION_ENGINE with cache with cobination of any other cache

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"  # or comfortabley anything else
CACHES = {
    'default': {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
              "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
     }
}

(Redis is my preferred configuration; you can use django.core.cache.backends.locmem.LocMemCache or django.core.cache.backends.memcached.MemcachedCache or any other option. Or even you can change Session engine from cache to something else like these if you still want to use DummyCache:

# File Based
SESSION_ENGINE = "django.contrib.sessions.backends.file"

# Works In Combination With  Current Cache and Database, fairly persistant
SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"   

# Cookie Based, Browser Clearing Will lose it. 
SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"  
like image 13
jerinisready Avatar answered Oct 21 '22 11:10

jerinisready


This error can also occur if an user is trying to login when in 'inactive' state.

like image 3
Shivanshu Bhardwaj Avatar answered Oct 21 '22 11:10

Shivanshu Bhardwaj