#1476·lldap

Postgres connection leak: /auth/simple/login never returns its DB connection to the pool

Author: escooterclinicCreated Jul 31, 2026Updated Jul 31, 2026

Every call to /auth/simple/login leaks a Postgres connection. We're on 0.6.3, Postgres 17, sea-orm 1.1.20, 5 instances behind HAProxy in TCP mode sharing one PG.

The transaction itself is fine, COMMIT goes out and gets acked, but the connection never comes back to the pool. It just sits there idle on Client/ClientRead until something kills it server side. Give it a few days and you're out of max_connections and the whole directory goes down with "FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute". Two outages so far, about 17k of those in the logs. We bumped 100 to 200, which only bought time.

Every stuck connection looks the same in pg_stat_activity: state=idle, wait_event=Client/ClientRead, and last_query is one of

COMMIT
SELECT "users"."password_hash" FROM "users" WHERE "users"."user_id" = $1 LIMIT $2
SELECT "user_attributes"."user_attribute_user_id", "user_attributes"."user_attribute_name" ...
SELECT "groups"."group_id" ... / SELECT "group_attributes"."group_attribute_group_id" ...
INSERT INTO "jwt_storage" ("jwt_hash","user_id","expiry_date","blacklisted") VALUES ($1...)
INSERT INTO "jwt_refresh_storage" ("refresh_token_hash","user_id","expiry_date") VALUES (...)

which is just the login path: check the password, resolve groups and attributes, insert the JWT and refresh token, commit. Saw it climb 34 to 63 in 13 minutes once and 24 to 61 another time, no batch job running either time, just ordinary login traffic (roughly one a minute across 6-10 of our consumers).

The weird part is that killing lldap doesn't free them. We SIGKILLed an instance that had been up two days and the connection count didn't move, only pg_terminate_backend cleared it. SIGKILL closes every fd the process holds, so the app had already lost track of that socket before we killed it. The guard is getting dropped at runtime, not at shutdown.

We read the login chain at v0.6.3 (48a0a8d9): simple_login in auth_service.rs calls LoginHandler::bind -> get_password_file_for_user for the SELECT, then get_login_successful_response does get_user_groups (two SELECTs), create_refresh_token and create_jwt -> register_jwt for the two INSERTs. Grepped for .begin(, .acquire(, .transaction(, TransactionTrait, PoolConnection and DatabaseTransaction and got nothing anywhere in that path. It's all independent sea_orm statements on the shared DatabaseConnection (one sqlx::PgPool per instance, max_connections(5)), no explicit transaction anywhere. sea-orm 1.1.20's executor/insert.rs doesn't explain the COMMIT either, insert() is a single INSERT ... RETURNING through db.execute().

So we can't pin it on a line in lldap. Feels like it's in the sea-orm/sqlx-postgres pool release path, but we haven't read those crates properly, so that's a guess.

We've also started caching JWTs client side since they're good for an hour and there's no reason to re-auth every minute. That slowed it right down but every real login still leaks one.

Couldn't find an existing issue for this. Closest is #1269/#1270 but that's shutdown-time pool cleanup, already in 0.6.3, different thing. Happy to throw more pg_stat_activity snapshots at it or test a patch.