fix: failed to create new record due to index conflicts#88
fix: failed to create new record due to index conflicts#88think-in-universe merged 4 commits intomainfrom
Conversation
Summary of ChangesHello @hanakannzashi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a potential issue in the user ban system where attempting to apply a new ban could lead to unique index conflicts if a previous ban of the same type had expired but was not explicitly marked as revoked. The change introduces a proactive step to automatically revoke such expired bans, ensuring that new bans can be applied without encountering database constraints and maintaining the integrity of the ban records. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request modifies the insert_user_ban function in crates/database/src/repositories/user_repository.rs. It adds a new step to revoke any expired user bans for a specific user and ban type before inserting a new ban. This pre-emptive update sets the revoked_at timestamp for active, expired bans to NOW() to prevent unique index conflicts, as the unique index only considers bans where revoked_at IS NULL. There were no review comments provided for these changes.
Code Review - PR #88: fix: revoke ban
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the user ban creation logic where attempting to create a new ban would fail due to unique index conflicts when expired bans exist but haven't been explicitly revoked.
Key Changes:
- Added automatic revocation of expired bans before inserting a new ban to prevent unique constraint violations
Comments suppressed due to low confidence (1)
crates/database/src/repositories/user_repository.rs:368
- The two separate database operations (UPDATE followed by INSERT) create a potential race condition. Between these operations, another concurrent call to create_user_ban could insert a ban for the same user_id and ban_type, leading to the same unique constraint violation this fix is trying to prevent. Consider wrapping both operations in a database transaction to ensure atomicity.
client
.execute(
"UPDATE user_bans
SET revoked_at = NOW()
WHERE user_id = $1
AND ban_type = $2
AND revoked_at IS NULL
AND expires_at IS NOT NULL
AND expires_at <= NOW()",
&[&user_id, &ban_type.as_str()],
)
.await?;
// Now insert the new ban
client
.execute(
"INSERT INTO user_bans (user_id, reason, ban_type, expires_at)
VALUES ($1, $2, $3, $4)",
&[&user_id, &reason, &ban_type.as_str(), &expires_at],
)
.await?;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@claude review |
Critical Issues Found1. Race Condition in Service Layer (HIGH PRIORITY)Location: crates/services/src/user/service.rs:133-154 The service layer has a TOCTOU (time-of-check to time-of-use) race condition: // Between this check...
if self.user_repository.has_active_ban(user_id).await? {
return Ok(());
}
// ...and this insert, another thread could create a ban
self.user_repository
.create_user_ban(user_id, ban_type, reason, expires_at)
.awaitImpact: In concurrent scenarios (multiple requests for the same user), both could pass the check and attempt to insert, causing one to fail with a duplicate key error despite the new transaction-based fix. Fix: Move the active ban check inside the repository transaction, or handle the unique constraint error gracefully in the service layer. 2. Inconsistent Ban Type Handling (MEDIUM PRIORITY)Location: crates/services/src/user/service.rs:133 vs crates/database/src/repositories/user_repository.rs:344-356
Impact: Inconsistent behavior where:
Fix: Either make 3. Missing Error Context (LOW PRIORITY)Location: crates/database/src/repositories/user_repository.rs:368-369 The transaction operations use Suggestion: transaction
.execute(...)
.await
.context("Failed to revoke expired bans")?;
transaction
.execute(...)
.await
.context("Failed to insert new ban")?;Positive Notes
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
No description provided.