Is using @Published in a Sendable type safe?

Is the following code accurate/safe?

final class ObjectWithPublished: @unchecked Sendable {
    @Published
    var property: Int?
}

I've tried testing this with the following test, which passes:

final class PublishedThreadSafetyTests: XCTestCase {
    func testSettingValueFromMultipleThreads() {
        let object = ObjectWithPublished()

        let iterations = 1000
        let completesIterationExpectation = expectation(description: "Completes iterations")
        completesIterationExpectation.expectedFulfillmentCount = iterations
        let receivesNewValueExpectation = expectation(description: "Received new value")
        receivesNewValueExpectation.expectedFulfillmentCount = iterations

        var cancellables: Set<AnyCancellable> = []

        object
            .$property
            .dropFirst()
            .sink { _ in
                receivesNewValueExpectation.fulfill()
            }
            .store(in: &cancellables)

        DispatchQueue.concurrentPerform(iterations: iterations) { iteration in
            object.property = iteration
            completesIterationExpectation.fulfill()
        }

        waitForExpectations(timeout: 1)
    }
}

There are also no warnings when using the address sanitizer, so it seems like subscriptions and updating @Published values is thread-safe, but the Published type is not marked Sendable so I can't be 100% sure.

If this isn't safe, what do I need to protect to have the Sendable conformance be correct?

No, this isn't safe, but not because of @Published. Your property is mutable, which means that two different threads with references to the same instance could modify the property simultaneously from different threads. That's a potentially catastrophic failure, in general.

In this case, @Published is a code smell, though. It's only used for mutable stored properties of a class, and such properties prevent the class from being safely Sendable.

OTOH, sendability isn't a relevant issue here, because you're not using Swift concurrency (in this code, at least).

@Polyphonic

This is a simplified example to be able to ask the question, rather than asking "here's my architecture, is it safe?" My use case is an object that has a public API that includes private(set) properties (where @Published comes in useful), internally the values may be updated from any thread, and Sendable conformance is required because the object is passed to an actor.

Are you implying "this could create incorrect values" or does the unsafe nature of this extend to "this is unsafe and may crash"? I am aiming for correctness, but it would be interesting to know why if this isn't safe and why my naive test doesn't crash. Is there a scenario that can be created where this would crash?

I've seen others using @Published properties with @MainThread, which I would guess makes this safe, but it doesn't fix the correctness side of things. e.g. reading the value, checking the value, then writing the value is not a single atomic operation. For this I think locks/private queues would be suitable, but that's out of scope for this discussion.

Is using @Published in a Sendable type safe?
 
 
Q