From b76598ea41e2eaccb70b6981a726607f68bcf6aa Mon Sep 17 00:00:00 2001 From: Shroominic Date: Mon, 1 Dec 2025 17:27:58 +0800 Subject: [PATCH] fix reserved balance race condition bug --- routstr/auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routstr/auth.py b/routstr/auth.py index d5b8ed87..5b5debdc 100644 --- a/routstr/auth.py +++ b/routstr/auth.py @@ -337,7 +337,7 @@ async def pay_for_request( stmt = ( update(ApiKey) .where(col(ApiKey.hashed_key) == key.hashed_key) - .where(col(ApiKey.balance) >= cost_per_request) + .where(col(ApiKey.balance) - col(ApiKey.reserved_balance) >= cost_per_request) .values( reserved_balance=col(ApiKey.reserved_balance) + cost_per_request, total_requests=col(ApiKey.total_requests) + 1,