Skip to content

fix: Handle user insert races and add test timeout#1618

Open
edwinjosechittilappilly wants to merge 3 commits into
mainfrom
fix-integration-tests-ci
Open

fix: Handle user insert races and add test timeout#1618
edwinjosechittilappilly wants to merge 3 commits into
mainfrom
fix-integration-tests-ci

Conversation

@edwinjosechittilappilly
Copy link
Copy Markdown
Collaborator

@edwinjosechittilappilly edwinjosechittilappilly commented May 15, 2026

Increase TypeScript integration test timeout to 120s to reduce flakiness during slow CI runs.

Enhance user_service.ensure_user_row IntegrityError handling to explicitly handle concurrent-insert races: detect a (oauth_provider, oauth_subject) race and return the existing row, detect an email_lookup_hash race by looking up the email and returning the concurrent identity when it matches, and handle PK collisions by retrying the insert with a new UUID. Add explanatory comments about which collisions are recoverable and when errors should propagate to the caller.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of account creation and sign-in by better recovering from race conditions between OAuth and email-based identities.
  • Tests
    • Increased integration test timeout to reduce false failures in slower environments.
  • Chores
    • Updated internal type annotations for clearer code maintenance.

Review Change Stack

Copilot AI review requested due to automatic review settings May 15, 2026 22:32
@github-actions github-actions Bot added the backend 🔷 Issues related to backend services (OpenSearch, Langflow, APIs) label May 15, 2026
Increase TypeScript integration test timeout to 120s to reduce flakiness during slow CI runs.

Enhance user_service.ensure_user_row IntegrityError handling to explicitly handle concurrent-insert races: detect a (oauth_provider, oauth_subject) race and return the existing row, detect an email_lookup_hash race by looking up the email and returning the concurrent identity when it matches, and handle PK collisions by retrying the insert with a new UUID. Add explanatory comments about which collisions are recoverable and when errors should propagate to the caller.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9338e5a1-b8b8-4724-a6a3-fce026cdc601

📥 Commits

Reviewing files that changed from the base of the PR and between 942f047 and fe5e2f6.

📒 Files selected for processing (1)
  • src/services/user_service.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/services/user_service.py

Walkthrough

The PR increases an integration test timeout to 120 seconds and refactors ensure_user_row to explicitly handle three IntegrityError recovery cases: oauth identity re-fetch/update, email-collision conditional re-fetch, and UUID-based retry for remaining collisions; a return annotation was updated to PEP 604 union syntax.

Changes

User Service Reliability and Testing

Layer / File(s) Summary
User service concurrent-creation race recovery
src/services/user_service.py
ensure_user_row's IntegrityError handler now distinguishes three race cases: re-fetch by (oauth_provider, oauth_subject) and update last_login if found; re-fetch by email and return only if provider+subject match; otherwise retry insert with a new UUID. Removed Optional import and updated get_effective_agent_config annotation to `dict
Integration test timeout increase
sdks/typescript/tests/integration.test.ts
The ingest-with-wait integration test now specifies a 120,000ms (120s) Vitest timeout to allow longer polling/completion waits.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

bug, tests

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: handling user insert race conditions and adding a test timeout.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-integration-tests-ci

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@edwinjosechittilappilly edwinjosechittilappilly changed the title fixHandle user insert races and add test timeout fix: Handle user insert races and add test timeout May 15, 2026
@github-actions github-actions Bot added bug 🔴 Something isn't working. and removed bug 🔴 Something isn't working. labels May 15, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/services/user_service.py (1)

230-230: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix the pipeline failure: use modern union syntax for Optional type.

The Ruff linter (UP045) is failing on this line. While not part of your direct changes, the pipeline must pass for the PR to merge.

Proposed fix
-async def get_effective_agent_config(session: AsyncSession, user_id: str) -> Optional[dict]:
+async def get_effective_agent_config(session: AsyncSession, user_id: str) -> dict | None:

Also remove the Optional import from line 17 if no longer needed:

-from typing import Optional
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/services/user_service.py` at line 230, The function signature for
get_effective_agent_config should use modern union syntax instead of
typing.Optional; change the return annotation from Optional[dict] to dict | None
and, if Optional is no longer used elsewhere in the file, remove the Optional
import (check the import on line with Optional) to fix the UP045 lint error.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/services/user_service.py`:
- Line 230: The function signature for get_effective_agent_config should use
modern union syntax instead of typing.Optional; change the return annotation
from Optional[dict] to dict | None and, if Optional is no longer used elsewhere
in the file, remove the Optional import (check the import on line with Optional)
to fix the UP045 lint error.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4a9b8225-2728-4d7e-89b9-aab7b0acadb4

📥 Commits

Reviewing files that changed from the base of the PR and between 2869a11 and 942f047.

📒 Files selected for processing (2)
  • sdks/typescript/tests/integration.test.ts
  • src/services/user_service.py

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to reduce CI flakiness in TypeScript integration tests and make ensure_user_row more resilient to concurrent user insert races.

Changes:

  • Adds explicit IntegrityError handling comments and recovery paths for OAuth, email hash, and PK collisions.
  • Adds a 120s timeout override to one TypeScript document ingestion integration test.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/services/user_service.py Extends concurrent insert recovery logic in ensure_user_row.
sdks/typescript/tests/integration.test.ts Adds a per-test timeout to one ingestion integration test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +120 to +127
# Case 2: email_lookup_hash race — same email already inserted (most
# common for the synthetic anonymous@localhost user in no-auth mode when
# two concurrent requests both observe an empty table). Look up by email;
# if the row's (provider, subject) matches ours it's a concurrent insert
# of the same identity and we can safely return it.
if user.email:
by_email = await user_repo.get_by_email(user.email)
if (
expect(result.status).toBeDefined();
expect((result as any).successful_files).toBeGreaterThanOrEqual(0);
});
}, 120_000);
Replace typing.Optional[dict] with PEP 604 union syntax (dict | None) for get_effective_agent_config and remove the now-unused Optional import. This is a pure type-annotation cleanup (no runtime behavior changes); note it requires Python 3.10+ for the `|` union syntax.
@github-actions github-actions Bot added bug 🔴 Something isn't working. and removed bug 🔴 Something isn't working. labels May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend 🔷 Issues related to backend services (OpenSearch, Langflow, APIs) bug 🔴 Something isn't working.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants