fief-dev / fief

Users and authentication management SaaS
https://www.fief.dev
Other
486 stars 42 forks source link

Fief causes numerous idle sessions in PostgreSQL #382

Closed meteoDaniel closed 1 month ago

meteoDaniel commented 1 month ago

Describe the bug

In my database, I can see numerous idle sessions that caused an outage. No more clients were allowed in my PostgreSQL

To Reproduce

Bildschirmfoto vom 2024-05-29 22-08-59

Expected behavior

Sessions will be closed correctly

Configuration

Additional context

I recommend to use a contextmanager to handle database sessions:

@contextmanager
def get_scoped_session(database: str) -> Generator[Session, None, None]:
    """
    Returns a scoped sqlalchemy session for the specified database

    Args:
        database: database name

    Returns:
        scoped session object

    """
    session = sessionmaker(bind=create_engine(database))()

    try:
        yield session
        session.commit()
    except Exception:
        session.rollback()
        raise
    finally:
        session.close()

# Usage: 
with get_scoped_session(engine):
    # do stuff