The vulnerability is caused by improper database transaction management in the TOTP authentication flow. When a user provides an invalid TOTP code, the apiv1.Login function correctly identifies the failure and calls user.HandleFailedTOTPAuth to log the attempt and lock the account after 10 failures. However, the Login function then proceeds to roll back the database session. The HandleFailedTOTPAuth function, in its vulnerable form, used this same session to update the user's status to 'locked'. Consequently, the database update was always reverted, and the account was never actually locked. An attacker with knowledge of a user's password could therefore perform unlimited attempts to guess the TOTP code. The patch addresses this by making HandleFailedTOTPAuth create and manage its own database session, committing the account lock in a separate transaction that is not affected by the rollback in the parent Login function.