20
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.
18
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"
- [Django]-Django unit tests without a db
- [Django]-Django – Simple custom template tag example
- [Django]-Django create userprofile if does not exist
3
This error can also occur if an user is trying to login when in ‘inactive’ state.
- [Django]-Negating a boolean in Django template
- [Django]-ForeignKey to abstract class (generic relations)
- [Django]-Loading fixtures in django unit tests
3
I also got this error for a different reason: The database was in read-only mode because of a different issue.
- [Django]-Bad request 400: nginx / gunicorn
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
- [Django]-Disable link to edit object in django's admin (display list only)?
2
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.
- [Django]-Django ModelForm for Many-to-Many fields
- [Django]-Best practices for adding .gitignore file for Python projects?
- [Django]-How do I use pagination with Django class based generic ListViews?
1
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.
- [Django]-Authenticate by IP address in Django
- [Django]-Disable a method in a ViewSet, django-rest-framework
- [Django]-How to change status of JsonResponse in Django
1
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
- [Django]-Want to disable signals in Django testing
- [Django]-How do i debug/breakpoint my django app using pycharm?
- [Django]-Iterating through two lists in Django templates
1
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.
- [Django]-Django – FileField check if None
- [Django]-Django, creating a custom 500/404 error page
- [Django]-Django multiple and dynamic databases
0
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.
- [Django]-Invalid command WSGIDaemonProcess Deploy Django application on CentOS 6.7
- [Django]-Uncaught TypeError: Cannot read property 'ownerDocument' of undefined
- [Django]-TransactionManagementError "You can't execute queries until the end of the 'atomic' block" while using signals, but only during Unit Testing
0
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()
- [Django]-Should I be adding the Django migration files in the .gitignore file?
- [Django]-MySQL ERROR 2026 – SSL connection error – Ubuntu 20.04
- [Django]-How to pass django rest framework response to html?
0
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.
- [Django]-ValueError: Related model u'app.model' cannot be resolved
- [Django]-Django: list all reverse relations of a model
- [Django]-Use Django ORM as standalone