'The request's session was deleted before the request completed. The user may have logged out in a concurrent request, for example
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!
Solution 1:[1]
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.
Solution 2:[2]
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"
Solution 3:[3]
This error can also occur if an user is trying to login when in 'inactive' state.
Solution 4:[4]
I also got this error for a different reason: The database was in read-only mode because of a different issue.
Solution 5:[5]
This error kept me busy for a few hours until finally I found out that my database was locked due to the fact that I had not saved the changes I made to it. Saving the data solved it.
Solution 6:[6]
In my case turned out to be caused by replication delay between two database servers (in Master [RW] - Slave mode [RO]) because I was routing some requests to the slave in order to perform a load distribution amongst the DBs. Same thing happened using 2 Redis servers as cache engine because of the replication delay between them.
We fix it forcing session requests to the main server.
Solution 7:[7]
This can also happen when there is no more space left on the HDD, had a similar issue with an AWS EC2 instance and clearing the space helped
Solution 8:[8]
This problem can occur when the server user (if running by a different user than root) running the django server hasn't enough permissions on the directories. The server therefore has only the permission to read and not to write. You can edit the permission of the project directory using the linux command :
chmod u=rwx,g=rx,o= /project_path
where u refers to users, g to group of users and o to others.
You can check the permissions of a directory with the -d option of ls, like this:
ls -lhd /
ls -lhd /etc
ls -lhd /etc/opt
ls -lhd /etc/opt/$DJANGO_PROJECT
This error can also occur if the user may have logged out in a concurrent request.
Solution 9:[9]
This exception also happen if you call
request.session.clear()
when on session exists (becuase it's not used or it's timed out).
It can be solved by checking and only clear the session if its in use:
if request.session:
request.session.clear()
Solution 10:[10]
In my case I had opened my database file (SQLite) into a DB Browser. So, this made it be closed for subsequent requests. Try to stop other stuff that are connected to your database.
Solution 11:[11]
This error can happen when you are using presistend cache (which makes a duplicate of the cache in the database) (django.contrib.sessions.backends.cached_db
) and you delete the database or flush all the data inside it.
This happend for me in development mode as I was using docker-compose and had python manage.py flush
in the entrypoint. I just removed the flush command as it is not needed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow