Mac hosting guide

Find Race Conditions with Xcode Thread Sanitizer

Run xcodebuild test with -enableThreadSanitizer YES and parallel testing on, then filter the race blocks out of the log with awk. Expect a 2x to 20x slowdown, so keep the run focused.

Figment editorial teamPublished Updated Facts checked November 10, 2026

Figment Mac plans

US East. Pay before use. Hourly and daily too.

Intel Mac VM4 shared vCPUs, 4GB RAM, 80GB SSD$25per monthApple Silicon Mac VM4 shared vCPUs, 4GB RAM, 65GB SSD$39per monthDedicated M4 Pro12-core CPU, 24GB RAM, 512GB SSD$199per month

Minimum first purchase: $25 of account credit, used for your rental.

To chase intermittent crashes caused by shared mutable state, enable Thread Sanitizer (TSan) in your test scheme or with -enableThreadSanitizer YES, run the suite on the hosted Mac, and filter the race blocks out of the log. TSan instruments your binary at compile time, watches every memory access at runtime, and reports the exact pair of conflicting accesses whenever two threads touch the same location without synchronisation. Apple documents the enable flags and the Simulator-only scope for iOS in the Thread Sanitizer section of the Xcode diagnostics guide. The upstream instrumentation is described in the Clang ThreadSanitizer documentation.

What does TSan do to my build, and what does it cost?

When you enable it, Xcode passes -fsanitize=thread to Clang and -sanitize=thread to the Swift compiler. Every load and store in the binary is wrapped with shadow-memory bookkeeping that tracks which thread last wrote each byte.

The runtime cost is real. Apple's diagnostics guide reports a 2x to 20x slowdown and a 5x to 10x memory increase for TSan builds. The upstream Clang docs give a narrower slowdown range of 5x to 15x. A suite that normally finishes in 40 seconds can take several minutes under instrumentation.

Apple restricts TSan to 64-bit macOS binaries and to iOS, iPadOS, tvOS, visionOS and watchOS apps running in Simulator, not on physical devices. A remote Mac absorbs the overhead while you keep working locally, and the Simulator is where TSan produces its reports. Because TSan slows every memory operation, run only the modules or test classes that exercise the shared state you suspect.

How do I enable TSan?

In Xcode (scheme editor): Open Product → Scheme → Edit Scheme, select Diagnostics under the Test action, and check Thread Sanitizer. The setting is stored in the .xcscheme file, so it travels with the repo.

On the command line (CI-friendly): Pass -enableThreadSanitizer YES to xcodebuild:

xcodebuild test \
  -project MyApp.xcodeproj \
  -scheme MyApp \
  -destination 'platform=iOS Simulator,name=iPhone 16,OS=latest' \
  -enableThreadSanitizer YES \
  2>&1 | tee tsan-raw.log

The tee step keeps the full log for the awk filter below. The scheme name is a placeholder, so substitute your project's real scheme.

Run long jobs over SSH inside a tmux or screen session, so the test suite keeps running if your connection drops.

How do I make the test suite trigger races?

TSan reports only races it actually witnesses. A test that never runs two threads against the same memory produces no output. The most reliable way to force contention is parallel test workers:

xcodebuild test \
  -project MyApp.xcodeproj \
  -scheme MyApp \
  -destination 'platform=iOS Simulator,name=iPhone 16,OS=latest' \
  -enableThreadSanitizer YES \
  -parallel-testing-enabled YES \
  -maximum-parallel-testing-workers 4 \
  2>&1 | tee tsan-raw.log

If an async function writes a property from a background task, add a test that calls it from multiple Task {} blocks and awaits them with withTaskGroup. That pattern reliably surfaces races in actor-less reference types.

How do I pull the conflicting pair out of the log?

Use a short awk filter that extracts only the race blocks from tsan-raw.log. Confirm the block terminator against a known TSan log for your toolchain before you rely on it in CI, because some runtimes end the block with a ==<pid>== footer and others emit a blank line:

awk '
  /WARNING: ThreadSanitizer: data race/ { in_race=1 }
  in_race { print }
  in_race && NF==0 { in_race=0; print "---" }
' tsan-raw.log > tsan-races.log

As a sanity check, grep -c "WARNING: ThreadSanitizer: data race" tsan-raw.log should match the number of --- separators in tsan-races.log. If it does not, adjust the terminator pattern (for example, match /^==[0-9]+==\s*$/) before trusting the filtered file.

Each extracted block has this shape:

WARNING: ThreadSanitizer: data race (pid=12345)
  Write of size 8 at 0x... by thread T2:
    #0 MyModel.update() MyModel.swift:47
    #1 ...
  Previous read of size 8 at 0x... by main thread:
    #0 MyModel.value.getter MyModel.swift:22
    ...

The two stacks name the contested property and both call sites. Fix the race by protecting that property with an actor, a lock or a @MainActor annotation, whichever matches how your code accesses it.

Which TSan reports are false positives, and how do I suppress them?

Two Swift patterns are worth double-checking:

  1. DispatchQueue.async with a captured immutable value. If the capture is a value type or a class with no writable state, TSan can still flag the access path. Check whether the flagged property is ever written after initialisation.
  2. @MainActor type tested off the main runloop. If the test runner calls a @MainActor method from a background thread, TSan reports on the wrong thread rather than on a real race. Annotate the test function with @MainActor, or drive it from a live main-runloop test host.

Suppress a confirmed false positive with a .tsan-suppressions file:

race:MyModel.frozenConstant

Load it with TSAN_OPTIONS=suppressions=.tsan-suppressions. Use suppressions sparingly, because they hide reports without changing the access pattern.

Should I combine TSan with Swift strict concurrency checking?

Yes, in that order. Xcode 16 exposes the SWIFT_STRICT_CONCURRENCY build setting with values minimal, targeted and complete. Setting it to complete turns actor-isolation violations into compile-time errors rather than runtime races.

  1. Build with SWIFT_STRICT_CONCURRENCY=complete so the compiler rejects every isolation violation it can prove statically.
  2. Run the same suite with -enableThreadSanitizer YES to catch the residue: races involving Objective-C types, unstructured Task launches or legacy GCD code.

Compiler errors are cheap to fix, so you spend TSan's overhead only on dynamic behaviour the type system cannot verify.

Which Figment Mac plan fits TSan runs?

The Intel Mac VM ($25 a month, $2 a day) runs macOS Sequoia and fits Xcode 26.3 or earlier. Pair it with a Simulator runtime that still ships x86_64 variants (check the Xcode and Simulator row in the Xcode release notes). Xcode 27 is Apple silicon only per Apple's porting guidance, and Xcode 26.4 and later need macOS Tahoe, so use the Apple Silicon Mac VM ($39 a month, $6 a day) when your race reproduces only under arm64 or you need a newer Xcode. For long, heavy instrumented suites, the Dedicated M4 Pro 48 GB gives a whole Mac. VM vCPUs are shared, and we have not measured TSan timings on any plan. See all Mac hosting plans.

Related guides:

Which Figment Mac fits this work?

Intel Mac VM from $25 a month, Apple Silicon Mac VM from $39, or a Dedicated M4 Pro for $199. Hourly and daily plans too. You pay before use, starting with at least $25 of account credit.

Choose a Mac plan