Skip to content

Data races on global mutable state with Swift 6.2 strict concurrency #1073

@HavenDV

Description

@HavenDV

Description

When building a project that depends on swift-snapshot-testing with Swift 6.2 strict concurrency checking enabled (-strict-concurrency=complete), the compiler reports data races on several global var declarations that can be accessed concurrently from parallel tests.

Affected globals

File Variable Issue
AssertSnapshot.swift __diffTool Bare var with no synchronization
AssertSnapshot.swift __record Bare var with no synchronization
AssertSnapshot.swift CleanCounterBetweenTestCases.registered Non-atomic read-modify-write
Deprecations.swift recordings Bare var dictionary, concurrent access from inline snapshot writes
AssertInlineSnapshot.swift testSourceCache Bare dictionary, concurrent reads/writes
PlistEncoder.swift _plistNullNSString Global let of NSString triggers isolation warning

Reproduction

  1. Add swift-snapshot-testing as a dependency
  2. Build with Swift 6.2 toolchain and -strict-concurrency=complete
  3. Observe data race warnings on the globals listed above

Suggested fix

Wrap mutable globals in a lock-protected container (e.g., NSLock-based wrapper with @unchecked Sendable). A Mutex (SE-0433) would be ideal but requires iOS 18+ / macOS 15+, which is above this library's deployment targets.

See #1072 for a proposed fix.

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