Skip to content

Systematic FK constraint race conditions exposed by async statistics optimization #25

@jas88

Description

@jas88

Problem

The LocalDB optimization (AUTO_UPDATE_STATISTICS_ASYNC ON) has exposed 34+ latent race conditions where database objects with FK dependencies are created without intermediate commits.

Root Cause

With async statistics enabled, INSERT operations may not be immediately visible to subsequent FK constraint checks. When parent-child objects are created in rapid succession:

// RISKY
var parent = new TableInfo(repo, "Table");
var child = new ColumnInfo(repo, "Column", "int", parent); // FK check may fail

Impact

  • Tests: Intermittent failures ("FK constraint violation")
  • Production: At least one production code path affected
  • CI: Timing-dependent failures hard to debug

Affected FK Relationships

  1. TableInfo → ColumnInfo
  2. Catalogue → CatalogueItem
  3. CatalogueItem → ExtractionInformation
  4. ColumnInfo → ExtractionInformation

Locations (34+ instances)

Priority 1 (Highest Impact)

  • Tests.Common/UnitTests.cs - Helper methods used by all tests
  • Rdmp.Core/CommandExecution/ExecuteCommandCreateNewCohortFromTable.cs - PRODUCTION CODE
  • AggregateBuilderTestsBase.cs - Affects many test classes

Priority 2 (Individual Tests)

  • 30+ test files with SetUp methods or test code

Solution Pattern

// SAFE
var parent = new TableInfo(repo, "Table");
parent.SaveToDatabase(); // Explicit commit
var child = new ColumnInfo(repo, "Column", "int", parent);

Action Plan

  • Fix Tests.Common/UnitTests.cs helper methods
  • Fix production code in ExecuteCommandCreateNewCohortFromTable.cs
  • Fix test base classes (AggregateBuilderTestsBase.cs, etc.)
  • Systematic cleanup of remaining test files
  • Add coding guideline to prevent recurrence
  • Consider static analysis rule

Already Fixed

  • Rdmp.Core.Tests/CohortCommitting/CreateNewCohortDatabaseWizardTests.cs

Related

  • Exposed by commit: a1ee79e (LocalDB optimization with async statistics)
  • Branch: fix/bundle-build-workflow

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions