Skip to content

Bug: routed scope initialization races with concurrent access and cache invalidation #330

Closed

Description

@loveRhythm1990

Problem

DbRouter does not coordinate concurrent cold initialization and cache invalidation for routed users and groups.

  • user_store() and group_store() use a check/initialize/insert sequence. Concurrent first requests can independently perform provisioning/schema initialization and return different Arc<SqlMemoryStore> instances for the same scope. scope_store() dispatches group IDs to the affected group path.
  • invalidate_user() can clear an empty cache while a cold initializer is still running. That initializer can subsequently publish a store and schema-readiness entry, silently overtaking invalidation.
  • user_db_name() also publishes a database-mapping cache entry and needs the same ordering with invalidation.

The global initialization semaphore limits total initialization concurrency; it does not provide exclusion for one user or group. Background access can enter the same cold path as foreground requests.

Evidence

Synthetic database tests reproduce these races without evaluation data or environment-specific configuration:

  • The original same-user diagnostic returned eight distinct stores from eight concurrent calls. This proves duplicate store initialization, not that every migration statement ran eight times.
  • The group regression fails against the original PR fix(storage): coordinate routed scope initialization and invalidation #331 commit: barrier-synchronized scope_store() calls return different store instances.
  • A deterministic invalidation regression pauses a cold initializer before provisioning by holding registry-pool connections. The original implementation lets invalidation finish immediately; the initializer can then publish after it. The corrected regression covers both user and group scopes.
  • A separate regression pauses a mapping lookup and verifies invalidation clears its publication before the next registry lookup.

The user initialization gap predates recent driver fixes. PR #172 replaced Moka async initialization with explicit cache lookup/initialization/publication to preserve Send compatibility. The issue is separate from MatrixOne freshness configuration in #329.

Impact

Redundant initialization can create avoidable schema work, contention, and latency. Lost invalidation can preserve stale schema readiness/store or database-mapping caches after an administrative change. These diagnostics do not establish data corruption.

Fix in #331

Reuse one process-local keyed-gate registry across user/group initialization, user database-mapping lookup, and cache invalidation. Preserve ready-value Moka caches and the hot cache-hit path. Recheck caches after acquiring the gate, and hold it until publication finishes. Invalidation acquires the corresponding gate before clearing database, schema, and store caches. Add explicit group and scope invalidation entry points matching scope_store() dispatch.

Retain the global initialization budget. Weak registry entries are reclaimed when no caller holds a gate, including cancellation, and pointer-checked cleanup cannot remove a replacement gate. No synchronous registry lock is held across an await.

Acceptance criteria

  • Concurrent first requests for one user or group reuse the published store.
  • Group initialization is also coalesced through scope_store().
  • Invalidation cannot be overtaken by an already-running initializer or mapping-cache publisher.
  • Different keys remain independently eligible under the global initialization budget.
  • Failed/cancelled initialization releases its gate and permits retry without publishing false readiness.
  • Gate cleanup reclaims idle entries without removing a replacement gate.
  • Relevant storage/integration tests, formatting, and normal lint checks pass.

Additional correctness and operational requirements

  • User IDs can match the old group:<id> string namespace. Schema/store caches and gates must distinguish users and groups structurally; synthetic regressions must cover both initialization orders and independent invalidation.
  • Bound complete cold initialization and cache-invalidation waits. Timeouts must cancel the local work future/release its gate rather than detach it, and failed invalidation must be reported explicitly.
  • invalidate_user(), invalidate_group(), and invalidate_scope() return Result; migration must propagate failure. A timeout must not bypass a live gate and declare success.
  • Keep the database-mapping cache user-only; group lookup does not consult it, so remove the unused group publication and invalidation.

Boundaries

Coordination is local to one router family/process, not a distributed lock. Cache invalidation does not revoke store handles already returned to callers, undo completed DDL, or serialize the entire database reset/migration operation. No MatrixOne configuration or deployment changes are part of this fix.

Activity

  1. changed the title [-]Bug: concurrent first access does not coalesce routed user initialization[/-] [+]Bug: routed scope initialization races with concurrent access and cache invalidation[/+] on Oct 17, 2026
  2. added a commit that references this issue on Oct 19, 2026
    4b9496f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions