-
-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Postgres queries stuck in status:active freezing our apps that use Lucia #1710
Comments
Are you sure this is a Lucia issue? You may need to close your database connections if you're using a lambda etc |
I'm not 100% sure, but we are not using the DB for any other stuff, just Lucia, which means that the only queries that the DB receives are Lucia queries. |
Are you by chance passing an |
Can you try querying the database manually and see if you can recreate the issue? I kinda doubt this is an issue with Lucia and adapters aren't expected to manage connections |
Use a runtime validator such as Zod if your app keeps having connection issue which resulted in some variable not get passed since Typescript only handles error at compiles. On large projects this could happen often. Lucia pg adapter execute whatever is passed from the function as string into an |
Thank you @pilcrowonpaper and @zsp03 for your inputs. I wrote stress tests that try to make these stuck queries happen on my local env, with no success. Sidenote: I did try to reproduce aborted queries on my local machine, but no luck. I guess the roundtrip on the docker setup is much much lower than in prod (usually under 10ms), which makes it impossible to randomly abort the query just at the right time. |
I don't think
Notice in the pg adapter public async getUserSessions(userId: UserId): Promise<DatabaseSession[]> {
const result = await this.controller.getAll<SessionSchema>(
`SELECT * FROM ${this.escapedSessionTableName} WHERE user_id = $1`,
[userId]
);
return result.map((val) => {
return transformIntoDatabaseSession(val);
});
} Based on your log, the query sent is
Lucia is moving away from adapters, i don't think this is possible in Lucia side. It would require you to make custom adapter. What you can do is add some runtime validation to check the session object and use values on the queries instead of args. This would make sure everything is correct before passing it into the queries and the db driver won't need to do extra. You can use the current postgres adapter as a reference. |
Quick update: |
Package
@lucia-auth/adapter-postgresql
Describe the bug
We have a worrying issue on production.
We are using Lucia with a Postgres.js adapter. Queries from pg_stat_activity start to hang on status "active". They can be in that status for hours, and don't go away until the server is restarted / apps redeployed. The queries are the simplest they can be, SELECT by an indexed session id column [session validation]). They pile up quickly, and the app's pool thread limit is quickly reached (it is 10 by default), freezing the app.
The issue:
Here is an example of stuck queries from the pg_stat_activity. As you may see from this example, the query_id is the same, and on is added 9 minutes after the first query.
I usually see queries duplicating like that in the pg_stat_activity query.
The text was updated successfully, but these errors were encountered: