pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.25k stars 1.42k forks source link

Crash on background queue `.publisher` effect subscription #3400

Open ivopintodasilva opened 12 hours ago

ivopintodasilva commented 12 hours ago

Description

A crash is happening in the CurrentValueRelay.request function when I have a .publisher effect that is subscribed to on a background queue:

    var recordingsEffect: Effect<Action> {
        .publisher {
            recordingsService.recordings
                .removeDuplicates()
                .subscribe(on: backgroundQueue)
                .map { _ in
                    // ...
                    [:]
                }
                .receive(on: mainQueue)
                .map(Action.recordingsUpdated)
        }
    }

If I remove/comment the backgroundQueue subscription, this crash no longer happens.

⚠️ Also worth noting that it only started occurring in Xcode 16 and TCA v1.15.0 ⚠️

I also tried to isolate the issue in a sample project by simply having a .publisher effect that does some work in a background queue but there the crash was not reproducible either...

Will keep investigating and provide a barebones sample project with the crash replication as soon as i can.

Also worth sharing that the recordingsService.recordings is a TCA store publisher (store.publisher.recordings.eraseToAnyPublisher()).

Checklist

Expected behavior

The effect's inner publisher does some work in the background queue and sends an action back to the reducer (in the main queue) without crashing.

Actual behavior

image

Here's a stacktrace that i recovered from our crash reporter.

Reproducing project

Will append one as soon as I'm able to reproduce the issue in an isolated project.

The Composable Architecture version information

1.15.0

Destination operating system

iOS 18

Xcode version information

Xcode 16.0

Swift Compiler version information

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0
ivopintodasilva commented 11 hours ago

After going through the commits in the v1.15.0 release, I was able to pin the issue down to commit 7c6fb2670019d61e9040fab448dfce885b56c7fd

// doesn't crash
.package(
    url: "https://github.com/pointfreeco/swift-composable-architecture",
    revision: "83ed1230412aa30cd84cbe1b5b8a4f298bce23aa"
),

// crashes
.package(
    url: "https://github.com/pointfreeco/swift-composable-architecture",
    revision: "7c6fb2670019d61e9040fab448dfce885b56c7fd"
),
mbrandonw commented 7 hours ago

Hi @ivopintodasilva, thank you very much for investigating this and providing so much information. The stack trace you provided seems quite similar to the problem described here in which an isolation invariant is broken. Would you have time to cook up a repro for us to investigate more deeply?