Skip to content

Add async streams and awaitable perform to SyncSession - #15

Merged
ryanashcraft merged 6 commits into
mainfrom
session-async-api
Nov 14, 2026
Merged

ryanashcraft merged 6 commits into
mainfrom
session-async-api

Conversation

@ryanashcraft

@ryanashcraft ryanashcraft commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Adds an async surface to SyncSession in SyncSessionCore, beside the Combine subjects. Nothing existing changes or goes away. CloudSyncSession and SupabaseSyncSession both inherit it.

What is new

public extension SyncSession {
    var events: AsyncStream<Event> { get }
    var fetchCompletions: AsyncStream<(SessionFetchOperation<Backend>, SessionFetchOperation<Backend>.Response)> { get }
    var modifyCompletions: AsyncStream<(SessionModifyOperation<Backend>, SessionModifyOperation<Backend>.Response)> { get }
    var halts: AsyncStream<Error?> { get }
    var states: AsyncStream<State> { get }

    func perform(_ operation: SessionFetchOperation<Backend>) async throws -> SessionFetchOperation<Backend>.Response
    func perform(_ operation: SessionModifyOperation<Backend>) async throws -> SessionModifyOperation<Backend>.Response
}

public enum SessionInterruption: Error { case halted(Error), stopped, reset }
public struct DuplicateFetchError: Error {}
  • events and the two completion streams drop nothing from the moment they are made. halts starts with the current value. states starts with the current state and keeps only the newest unread one.
  • A perform call owns its operation. When it throws on a halt, a stop, a reset or task cancellation, the part of the operation that has not been sent leaves the queue. Work queued with modify(_:) and fetch(_:) behaves as before.
  • A perform for a modify returns one response for all of its pieces. A perform for a fetch returns its own page.

The README section "Async Streams and perform" has the full contract.

How it works

  • Operations carry an internal perform ID through the size split, a server split, a retry and a conflict resend. The state stamps it on whatever replaces a piece, so a backend's failure resolver cannot lose it.
  • Stream subscribers and waiting calls live in SessionObservers, behind the session's existing lock, and are told about each change inside update.
  • A waiting call is woken only after the lock is released. Its cancellation handler takes the lock while the Swift runtime holds the task's own lock, so waking it under the lock could deadlock. A test that races cancellation against completion found this.
  • workInFlight is cleared by the same change that records how the request ended, so a request ending and what follows from it are one step.

Things a reviewer should know

  • The stored perform closure is renamed performWork (internal). The private free function perform in CloudSyncSession.swift is renamed carryOut, because the new methods shadow it inside the subclass. A subclass elsewhere with a free function of that name needs the same rename.
  • SyncBackend's associated types now require Sendable, and the operations, responses, events and state conform, so the Unchecked wrapper is deleted, from the run loop as well. This is the one change here that is not additive: a backend with a non-Sendable record type stops compiling, userInfo values are any Sendable, and the package minimums rise to iOS 17, macOS 14 and watchOS 10, where CKRecord becomes Sendable.
  • A failure resolver that answers a modify's failure with a fetch to retry (or the reverse) drops the work, as it did before. A perform waiting on that work would then wait for good. No backend in either library does this.

Tests

  • 37 new tests in SyncSessionCoreTests: each stream, perform completing through split, retry and resend, and perform throwing on each of halt, stop and reset, both queued and with its request in flight. No sleeps.
  • swift test: 99 and 69 pass. swift test --sanitize=thread: clean.
  • The new suite was looped 30 times after the last change with no failure.

Foodnoms does not call any of this yet. Its clients move over in later stages of "Logistics off Combine".

🤖 Generated with Claude Code

ryanashcraft and others added 6 commits November 14, 2026 08:39
An internal ID that a perform call will put on its operation. Every piece
the operation is split into carries it, and the state stamps it on whatever
takes a piece's place in the queue, so a retry a backend builds cannot lose
it. The state can report whether any of a call's work is still queued and
take that work out.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Streams for events, fetch completions, modify completions, halts and state,
and perform for a fetch and for a modify. The Combine subjects are unchanged.

A perform call owns its operation: when the call throws on a halt, a stop, a
reset or cancellation, the part of the operation that has not been sent
leaves the queue. A modify returns one response for all of its pieces.

Subscribers and waiting calls live behind the session's lock and are told
about each change inside update. A waiting call is woken only after the lock
is released, because its cancellation handler takes the same lock while the
runtime holds the task's own.

The stored perform closure is renamed performWork, and CloudSyncSession's
private perform function carryOut, now that the session has methods by
that name.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…withdrawn work

A fetch(_:) for a change token that a perform call already has queued is
served by that one queued fetch. It now stays queued, without the call, when
the call throws. Before, it left the queue with the call and the fetch the
app asked for never ran.

Taking a call's work out of the queue while the run loop is waiting to send
or retry it now ends that wait, so the work behind it is not held up.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
SyncBackend's associated types now require Sendable, and the session's
operations, responses, events and state conform. The compiler checks
what the Unchecked wrapper used to promise, in the run loop as well as
the async streams and perform.

CKRecord is Sendable from iOS 17, macOS 14 and watchOS 10, so those are
the package's new minimums. A modify operation's userInfo values are
any Sendable.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The helper that waits for one CloudKit operation resumed its continuation
with a value the compiler could not prove safe to hand over. Every value
it carries is Sendable now that the backend's types are, so it says so.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ryanashcraft
ryanashcraft merged commit 22563c3 into main Nov 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant