-
Notifications
You must be signed in to change notification settings - Fork 450
Fix: Connection leak in db_update_cluster() to prevent crashes in transactions #825
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller
participant db_update_cluster as Updater
participant DB
rect rgb(240,248,255)
Note over Caller,Updater: New flow (conditional close)
Caller->>Updater: call db_update_cluster(conn=external_conn, own_connection=False)
Updater->>DB: perform update using external_conn
Updater-->>Caller: return result (no commit/close)
Note over Caller: Caller retains responsibility for commit/close
end
rect rgb(245,255,240)
Note over Caller,Updater: Internal-connection flow (own_connection=True)
Caller->>Updater: call db_update_cluster(conn=None, own_connection=True)
Updater->>DB: open connection, perform update
Updater->>DB: commit
Updater->>DB: close connection
Updater-->>Caller: return result
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
backend/app/database/face_clusters.py (1)
209-230: Missing exception handling with rollback logic.The try block lacks an
exceptclause to handle errors and rollback transactions when exceptions occur. This is inconsistent with similar functions in this file (db_delete_all_clusterslines 63-67 anddb_insert_clusters_batchlines 117-120), which explicitly rollback whenown_connectionis True.Without this, externally-managed connections may be left in an inconsistent state if an exception occurs after the UPDATE but before returning.
🔎 Proposed fix
try: # Build the update query dynamically based on provided parameters update_fields = [] update_values = [] if cluster_name is not None: update_fields.append("cluster_name = ?") update_values.append(cluster_name) if not update_fields: return False update_values.append(cluster_id) cursor.execute( f"UPDATE face_clusters SET {', '.join(update_fields)} WHERE cluster_id = ?", update_values, ) updated = cursor.rowcount > 0 - conn.commit() + if own_connection: + conn.commit() return updated + except Exception: + if own_connection: + conn.rollback() + raise finally: if own_connection: conn.close()
Fixes #824
Changes:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.