Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clients/shared/Network/EventStreamClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ public final class EventStreamClient {
sseTask = Task { @MainActor [weak self] in
guard let self else { return }

// A back-to-back call to `startSSEStream()` on the MainActor can
// cancel this task and invalidate `session` before it runs its
// first instruction. Calling `session.bytes(for:)` on an already
// invalidated session throws an ObjC NSGenericException from
// `-[__NSURLSessionLocal taskForClassInfo:]`, which is
// uncatchable in Swift and crashes the process with SIGABRT.
// Bail out before touching the session if we've been superseded.
guard !Task.isCancelled, self.sseSession === session else { return }

do {
let (bytes, response) = try await GatewayHTTPClient.stream(
path: "assistants/{assistantId}/events",
Expand Down