[Fixed]-How can I prevent RuntimeError("Unable to create a new session key.")?

3👍

Looking at the python-memcached code on launchpad, you should be able to tweak dead_retry and retry_timeout. Another option may be to run a low memory, low connections instance of memcached on one or both of the app servers as a fallback when the primary memcached server is unreachable.

6👍

Just solved same problem with apt-get install memcached. May be it’s your case too.

Ou, sorry, this is not your case. I’vr just read question with more attention. But I will left my answer – cause it’s about this runtime error.

3👍

https://github.com/django/django/blob/master/django/contrib/sessions/backends/cache.py

def create(self):
    # Because a cache can fail silently (e.g. memcache), we don't know if
    # we are failing to create a new session because of a key collision or
    # because the cache is missing. So we try for a (large) number of times
    # and then raise an exception. That's the risk you shoulder if using
    # cache backing.
    for i in xrange(10000):
        self._session_key = self._get_new_session_key()
        try:
            self.save(must_create=True)
        except CreateError:
            continue
        self.modified = True
        return
    raise RuntimeError("Unable to create a new session key.")
  • you can monkey-patch django.contrib.sessions.backends.base.SessionBase._get_new_session_key to do a time.sleep(0.001).
  • you could also check your entropy:

here’s the command:

cat /proc/sys/kernel/random/entropy_avail
👤dnozay

2👍

I was getting this error running a local, development version of a Django project, because it was periodically having trouble connecting to a non-local cache. I realized that I could change my session backend to a file-based session to address the issue .

In the settings file for this local, development version of Django, I simply set the following value:

SESSION_ENGINE = 'django.contrib.sessions.backends.file'

This is not the solution I would use in a production environment, and not the solution I would suggest to the original poster, but it took me a few minutes to figure out what the issue was and this is one of the only results that appeared when I Googled, so I figured I’d post here possibly to help out others with a similar issue.

👤erewok

2👍

In the memcache config file (e.g., /etc/sysconfig/memcached or /etc/memcached.conf), you may need to change:

-l 127.0.0.1

to

-l 0.0.0.0

to allow the web server to connect to the memcache server (if they are on different hosts).

-1👍

Same issue I faced, Check ports configured in django and memcached.May be both are different.

you can change memcached port vim /etc/memcached.conf find ‘Default connection port is’ changed according to your need restart memcached services

-1👍

This is exactly CPU shortage. when the request goes to be completed watching /var/log/apache/error.log could be the best place for monitoring. also take htop tp monitor your cpu while you are working with web console.
you should increase CPU cores

Leave a comment