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

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"  

3👍

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

3👍

I also got this error for a different reason: The database was in read-only mode because of a different issue.

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.

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.

👤Erasmo

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

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.

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.

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()
👤Drewes

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.

Leave a comment