Search code examples
pythonflaskredisazure-aksflask-session

Flask Application Error: AttributeError: 'NoneType' object has no attribute 'sign'


I have deployed my Flask Application on AKS cluster and configured flask session backend with Redis. But the application is not able to access, it shows Internal Server Error. When I checked the pod logs for more information, I see,

INFO:waitress:Serving on http://0.0.0.0:5000
ERROR:app:Exception on /login [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1826, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'
ERROR:app:Request finalizing failed with an error while handling an error
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1826, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'
ERROR:app:Exception on /login [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1826, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'
ERROR:app:Request finalizing failed with an error while handling an error
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1826, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'
ERROR:app:Exception on /login [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1826, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'
ERROR:app:Request finalizing failed with an error while handling an error
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1826, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1847, in finalize_request
    response = self.process_response(response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2344, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/usr/local/lib/python3.9/site-packages/flask_session/sessions.py", line 171, in save_session
    session_id = self._get_signer(app).sign(want_bytes(session.sid))
AttributeError: 'NoneType' object has no attribute 'sign'

app_config.py

SESSION_TYPE = "redis"
SESSION_PERMANENT = False
SESSION_USE_SIGNER = True
SESSION_REDIS = redis.Redis(host='redis-master', port=6379, password='XXXXX')

How do I fix this issue?

because it was working fine in local environment, with the configuration of

SESSION_TYPE = "redis"
SESSION_PERMANENT = False
SESSION_USE_SIGNER = True
SESSION_REDIS = redis.from_url('redis://localhost:6379')

running the ‘redis-server’ command will start the redis server and my application is able to access it from ‘http://localhost:5000/login’ without any issues.


Solution

  • This maybe caused by the SESSION_USE_SIGNER configuration being set to True. When this is set, Flask-Session will try to sign the session ID before saving it to Redis. _get_signer() method is trying to get a signer from the application, but the application is NoneType.

    This is likely because your Flask application is not configured to use a secret key. The secret key is used to sign the session ID, so if it is not set, the _get_signer() method will return NoneType.

    SECRET_KEY = 'your-secret-key'