question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enabling Flask Security causes all requests to load significantly slower

See original GitHub issue

We have recently started using flask-security-too in our codebase, and we integrated it similar to the example given here. However, upon integrating flask-security-too, we noticed that general requests (route, static files, etc.) in development have become significantly slower (1000% slower with images). We have not experimented with any authentication yet.

Loading time with Flask-Security-Too image

Loading time without Flask-Security-Too image

Here are some relevant codebits: Register Function:

def register_security(app):
    """Register Flask-Security-Too"""
    user_datastore = SQLAlchemyUserDatastore(db, dashboard.models.User, dashboard.models.Role)
    security.init_app(app, user_datastore)
    return None

Models:

class RolesUsers(SurrogatePK, Model):
    __tablename__ = 'roles_users'
    user_id = Column('user_id', db.Integer(), db.ForeignKey('users.id'))
    role_id = Column('role_id', db.Integer(), db.ForeignKey('roles.id'))


class Role(RoleMixin, SurrogatePK, Model):
    __tablename__ = 'roles'
    name = Column(db.String(80), unique=True)
    description = Column(db.String(255))


class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = "users"
    email = Column(db.String(255), unique=True, nullable=False)
    #: Randomly generated uuid
    uuid = Column(db.String(32), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.LargeBinary(255), nullable=False)
    last_login_at = Column(db.DateTime())
    current_login_at = Column(db.DateTime())
    last_login_ip = Column(db.String(100))
    current_login_ip = Column(db.String(100))
    login_count = Column(db.Integer())
    active = Column(db.Boolean(), nullable=False, default=False)
    fs_uniquifier = Column(db.String(255))
    confirmed_at = Column(db.DateTime())
    roles = relationship('Role', secondary='roles_users',
                         backref=backref('users', lazy='dynamic'))
    created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    first_name = Column(db.String(30), nullable=True)
    last_name = Column(db.String(30), nullable=True)
    verified = Column(db.Boolean(), nullable=False, default=False)

This is all we have done with the library so far, and we were able to narrow it down to this library by commenting out the register_security() function and testing. We would love to use flask-security-too, is there a reason we are facing this issue?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
jwag956commented, Apr 14, 2020

Thanks for the update.Not really clear what was going on - Flask-Login generates the session cookie when necessary. I don’t know if there was an issue that 2 different user_loaders were being called or different user id in the session cookie. But glad things are working now.

0reactions
rushilsrivastavacommented, Apr 15, 2020

Appreciate your help! Thanks for the prompt replies and looking into this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Slow Flask response when using Flask-security - Stack Overflow
It seems that the hashing algorithm, which is slow, is running for every request. However, it might not be necessary to do it...
Read more >
Flask-Security Documentation
Flask -Security allows you to quickly add common security mechanisms to your Flask application. They include: 1. Session based authentication.
Read more >
Long time responding · Issue #147 · corydolphin/flask-cors
Flask -CORS doesn't do much work, so I would be surprised if it was slowing down requests substantially. Are they eventually being returned ......
Read more >
NGINX + uWSGI + Flask. Some requests very slow
I presume the issue is related to lazy-loading modules and slow requests are when a new worker needs to start-up, or on reload....
Read more >
python-flask-security(1) - Arch manual pages
(This enables easily invalidating all existing sessions for a given user without having to ... Flask-Security supports JSON/Ajax requests where appropriate.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found