Skip to content

Snapshot Git mirror repository IDs under the state lock before starting synchronization workers #5

Open

Description

@LunaStev

Problem

Service.SynchronizeAll ranges over service.repositories without stateMu while launching goroutines. Each worker enters Synchronize, which updates an entry in that same map when setting its status to syncing, error, or ready.

Those writes use stateMu.Lock, but the iterator does not take the corresponding read lock. A worker can therefore write while SynchronizeAll is still advancing the map iterator. Per-repository mutexes do not protect this shared map iteration.

This is reachable during ordinary parallel synchronization even though the configured repository keys themselves are fixed.

Code references:

Evidence and regression scenario

A reduced Go probe preserving the production map-iteration/goroutine-launch pattern and the initial locked status update reports:

WARNING: DATA RACE
Write:         runtime.mapassign_faststr -> Service.Synchronize
Previous read: runtime.mapiternext       -> Service.SynchronizeAll

The probe used three existing keys (wave, vex, and whale), repeated synchronization, and go run -race on Linux amd64. It stopped before external Git and database operations. This confirms the concurrency pattern, not a recorded production-service crash.

Add a service-level regression with controlled synchronization workers or local Git fixtures so the real iteration and status-update paths execute concurrently without contacting the public repositories.

Expected behavior and scope

Copy the repository IDs while holding stateMu.RLock, release it, and launch workers from the independent snapshot. An equivalent design that enumerates an immutable registry is also acceptable if its relationship to the service state is explicit.

Do not hold a read lock while waiting for workers: those workers need the write lock to publish their status. Preserve the per-repository serialization and parallel synchronization of different repositories.

Completion criteria

  • No repository-map iteration overlaps an unsynchronized map write.
  • Add coverage for ordinary synchronization of several repositories and overlapping synchronization requests under the Go race detector.
  • Each selected repository is scheduled once per synchronization pass.
  • The state lock is released before waiting for workers or performing Git/database I/O; tests detect a deadlock with a bounded test deadline.
  • Existing same-repository serialization, status transitions, and cancellation behavior are preserved.
  • Tests use controlled workers or local fixtures rather than network access or timing-sensitive sleeps as their synchronization mechanism.
  • go test -race ./internal/gitmirror exercises the regression on a supported development host.

Difficulty: medium. This issue concerns shared-map access, not Git transport, mirror freshness policy, or repository discovery.

Activity

  1. added
    bug
    help wanted
    on Sep 21, 2026
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

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions