Skip to content

Compiler race-condition crash capturing weak #67361

Description

@NachoSoto

Description
I have this code to ensure that an object doesn't leak at the end of tests:

self.addTeardownBlock { [weak instance = Singleton.shared] in
    expect { instance == nil }.toEventually(beTrue(), description: "Instance has leaked")
}

That .toEventually comes from Nimble, it basically polls after some interval, until a certain timeout, verifying the condition.

On CI I noticed sometimes we get a crash:

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib            	       0x10bc64589 swift::runtime::AccessSet::insert(swift::runtime::Access*, void*, void*, swift::ExclusivityFlags) + 73
1   libswiftCore.dylib            	       0x10bc647e2 swift_beginAccess + 66
2   BackendIntegrationTests       	       0x13ab37c76 default argument 1 of Expectation.toEventually(_:timeout:pollInterval:description:) + 54
3   BackendIntegrationTests       	       0x13ab37b44 closure #1 in BaseBackendIntegrationTests.verifyPurchasesDoesNotLeak() + 276 (BaseBackendIntegrationTests.swift:167)

Which I believe is due to some race-condition accessing that weak var.

I believe changing the code to this works around it:

weak var instance = Singleton.shared
self.addTeardownBlock {
    expect { instance == nil }.toEventually(beTrue(), description: "Instance has leaked")
}

Environment

  • Swift:
swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
Target: arm64-apple-macosx13.0
  • Xcode:
Xcode 14.3
Build version 14E222b
  • Deployment target: iOS 16.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.runtimeThe Swift Runtime

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions