Why does my AsyncSequence with no non-isolated next() function crash on iOS 18.1?

I recently rewrote an AsyncSequence, whose decl now looks roughly like the following:

public struct AnyAsyncSequence<Element, Failure>: AsyncSequence, Sendable, SendableMetatype where Failure: Error {

    public struct Iterator: AsyncIteratorProtocol, SendableMetatype {

        public mutating func next(
            isolation actor: isolated (any Actor)?
        ) async throws(Failure) -> Element? {
            ...
        }

    }

}

Originally, it had a public func next() async -> Element? impl (and no Failure case).

However, the removal of the next() function with that decl causes the app to crash at launch on the iOS 18.1 simulator in dyld. According to the person who reported this issue, an LLM informed them that the binary now wants to load the symbol sScIsE4next7ElementQzSgyYa7FailureQzYKF, aka (extension in Swift):Swift.AsyncIteratorProtocol.next() async throws(A.Failure) -> A.Element?, which is not present in the iOS 18 simulator.

This came as a surprise to me; firstly that it's not present, and secondly, that it's looking for a version that throws a Failure case, when the non-isolated next() decl does not include Failure even after Failure was added to the stdlib.

I've fixed this by adding this decl to my AsyncSequence: public mutating func next() async throws(Failure) -> Element? but I'm wondering if I should also be adding a next that throws or something to fully comply with the protocol requirement?

One other interesting detail is that it seems like some code in my codebase was, according to Xcode's index, directly calling this non-existent non-isolated-typed-throws variant.

1 Like

Your iterator now only implements next(isolation:). That means the next() requirement of AsyncIteratorProtocol is satisfied by the standard library's default implementation in a protocol extension. That's the symbol dyld can't find: the typed-throws default lives in the Swift runtime that ships with the OS, so it doesn't exist on older OS versions like iOS 18.1. Ideally the compiler would flag the conformance as unavailable on your deployment target instead of letting it fail at load time, so it may be worth filing an issue.

The fix is to implement next() yourself so the conformance never relies on the runtime-provided default:

public mutating func next() async throws(Failure) -> Element? {
    try await next(isolation: nil)
}

Your own code is compiled into your binary, so it works on every OS version. Callers that support isolation still get next(isolation:). Before you ship, it's worth running the launch test on the oldest simulator runtime you support, since that's the only place this kind of missing-symbol problem shows up.

This is `@available(SwiftStdlib 6.0, *)` on default `AsyncIteratorProtocol.next()` does not match actual runtime availability (only available in 6.1) · Issue #87849 · swiftlang/swift · GitHub which was caused by incorrect availability annotations on the default implementation of next(). It was just fixed recently.

3 Likes