Superwall-iOS icon indicating copy to clipboard operation
Superwall-iOS copied to clipboard

[BUG] Swift access race when run with Thread sanitizer

Open tkafka opened this issue 3 years ago • 3 comments

General information

  • Superwall version: 3.2.2
  • iOS version(s): 17.0
  • CocoaPods/Carthage version (if applicable):
  • Xcode version: Version 14.3.1 (14E300c)
  • Devices/Simulators affected: iPhone simulator
  • Reproducible in the demo project? (Yes/No): Yes
  • Related issues:

Describe the bug

When I turn on Thread sanitizer in a scheme, and run the app, the following warning appears:

/Users/.../Library/Developer/Xcode/DerivedData/Weathergraph-eksxezdcznqhaibhzaurdwsgkvku/SourcePackages/checkouts/Superwall-iOS/Sources/SuperwallKit/Dependencies/DependencyContainer.swift:272 Swift access race in SuperwallKit.Superwall.$subscriptionStatus.getter : Combine.Published<SuperwallKit.SubscriptionStatus>.Publisher at 0x1246653c0

image

Steps to reproduce

Run an app with SuperwallKit with thread sanitizer.

tkafka avatar Aug 02 '23 12:08 tkafka

Hey, someone else also mentioned this but I'm yet to reproduce it. You said you managed to reproduce it our example apps? How did you do that? I tried with the thread sanitiser but nothing happened. Any more details of how to reproduce this would help a lot!

yusuftor avatar Aug 02 '23 14:08 yusuftor

I'm closing this because I've been unable to reproduce this. If it still happens, let me know and I'll reopen the issue.

yusuftor avatar Jan 24 '24 15:01 yusuftor

I'm closing this because I've been unable to reproduce this. If it still happens, let me know and I'll reopen the issue.

It still happens. Reproduced on 4.16.1, deterministically — 3/3 runs. Flagging it here as invited rather than opening a duplicate; happy to file fresh if you'd prefer.

It's the same line, not a lookalike

The original report cited DependencyContainer.swift:272 on 3.2.2. That line was:

subscriptionStatus: subscriptionStatus ?? Superwall.shared.$subscriptionStatus.eraseToAnyPublisher(),

The same statement in 4.16.1 is DependencyContainer.swift:405, in the same makePresentationRequest:

subscriptionStatus: Superwall.shared.$subscriptionStatus.eraseToAnyPublisher(),

Same property, same function, same projected-value access — only the line number moved and the optional override parameter went away.

Environment

  • SuperwallKit 4.16.1 (SPM)
  • Xcode 26.4.1 (17E202)
  • iPhone 17 Pro simulator, iOS 26.4.1 (23E254a)
  • Plain SwiftUI app, default SuperwallOptions, no PurchaseController

Steps to reproduce

  1. Superwall.configure(apiKey:) at launch.
  2. Call await Superwall.shared.getPresentationResult(forPlacement: "anything"). The placement doesn't need to exist in a campaign.
  3. Build with -enableThreadSanitizer YES and run.

TSan reports within ~20 seconds of launch. No app-side concurrency, no threading tricks — one public API call.

Trace

WARNING: ThreadSanitizer: Swift access race
  Modifying access of Swift variable at 0x000335e27fc0 by thread T9:
    #0 Superwall.$subscriptionStatus.getter                    Superwall.swift
    #1 DependencyContainer.makePresentationRequest(...)        DependencyContainer.swift:405
    #2 Superwall.internallyGetPresentationResult(forPlacement:requestType:)
                                                              PublicGetPresentationResult.swift:97

  Previous modifying access of Swift variable at 0x000335e27fc0 by main thread:
    #0 Superwall.$subscriptionStatus.getter                    Superwall.swift
    #1 DependencyContainer.makePresentationRequest(...)        DependencyContainer.swift:405
    #2 Superwall.internallyHandleImplicitTrigger(forPlacement:withData:)
                                                              Tracking.swift:112

  Location is heap block of size 112 allocated by main thread:
    #2 Superwall.__allocating_init(...)                        Superwall.swift:467
    #3 static Superwall.configure(...)                         Superwall.swift:695

  Thread T9 (tid=1369566, running) is a GCD worker thread

SUMMARY: ThreadSanitizer: Swift access race Superwall.swift in Superwall.$subscriptionStatus.getter

What's happening

Both sides of the race are inside the SDK, at the same line, reached by two different internal paths:

  • internallyGetPresentationResult → makePresentationRequest on a background executor
  • internallyHandleImplicitTrigger → makePresentationRequest on the main thread

Reading the projected value of a @Published property is a modifying access under Swift's exclusivity rules, so two concurrent reads violate it even though neither side assigns. Since the app never touches $subscriptionStatus itself, an integrator can't avoid this from the outside — which may be why the original reporter couldn't narrow it down beyond "run with Thread Sanitizer".

That the implicit-trigger path fires on its own is probably also why it looked flaky: it depends on the SDK's own event traffic overlapping a presentation-result call, rather than on anything the app does.

I'm happy to share the harness app, or to test a patch — the reproduction is deterministic on my machine.

0xwurdig avatar Aug 14 '26 18:08 0xwurdig