Skip to content

Conversation

@chrisburr
Copy link
Member

Problem

When DIRAC calls DiracX endpoints that write to the database and then immediately queries the same data, intermittent "No sandbox matches the requirements" errors occur due to a race condition.

Root Cause

With FastAPI's default scope="request" for dependencies:

  1. DiracX inserts data into the database
  2. DiracX sends HTTP response back to DIRAC
  3. DIRAC immediately queries the database
  4. DiracX commits the transaction (happens AFTER step 3)

This causes DIRAC to query before the data is committed, resulting in race condition failures.

Solution

Changed all database dependencies to use scope="function" which ensures:

  1. DiracX inserts data into the database
  2. DiracX commits the transaction
  3. DiracX sends HTTP response back to DIRAC
  4. DIRAC queries the database (data is now available)

Changes

  • Introduced DBDepends helper using functools.partial(Depends, scope="function")
  • Applied to all SQL database dependencies: AuthDB, JobDB, JobLoggingDB, PilotAgentsDB, SandboxMetadataDB, TaskQueueDB
  • Applied to OpenSearch database: JobParametersDB

This prevents timing-based race conditions in all DIRAC-DiracX database interactions.

Testing

This fix addresses the production issue where sandbox uploads were intermittently failing with "No sandbox matches the requirements" errors immediately after successful DiracX uploads.

Use scope="function" for database dependencies to prevent race conditions
where DIRAC queries data immediately after DiracX writes it. With the
default scope="request", commits were happening after sending responses,
causing intermittent "No sandbox matches the requirements" errors.

Introduced DBDepends helper using functools.partial to consistently apply
scope="function" to all database dependencies (AuthDB, JobDB, JobLoggingDB,
PilotAgentsDB, SandboxMetadataDB, TaskQueueDB, JobParametersDB).

This ensures transaction commits complete before HTTP responses are sent,
eliminating timing-based race conditions in DIRAC-DiracX interactions.
@chrisburr chrisburr requested a review from chaen December 18, 2025 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant