Skip to content

Commit f6314a6

Browse files
fix: Improve subscriptionTask cleanup
1 parent 5c03184 commit f6314a6

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Sources/GraphQLWS/Server.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public class Server<
101101
}
102102
}
103103

104+
deinit {
105+
subscriptionTasks.values.forEach { $0.cancel() }
106+
}
107+
104108
/// Define a custom callback run during `connection_init` resolution that allows authorization using the `payload`.
105109
/// Throw from this closure to indicate that authorization has failed.
106110
/// - Parameter callback: The callback to assign
@@ -176,18 +180,15 @@ public class Server<
176180
let stream = try await onSubscribe(graphQLRequest)
177181
for try await event in stream {
178182
try Task.checkCancellation()
179-
do {
180-
try await self.sendData(event, id: id)
181-
} catch {
182-
try await self.sendError(error, id: id)
183-
throw error
184-
}
183+
try await self.sendData(event, id: id)
185184
}
186185
} catch {
187186
try await sendError(error, id: id)
187+
subscriptionTasks.removeValue(forKey: id)
188188
throw error
189189
}
190190
try await self.sendComplete(id: id)
191+
subscriptionTasks.removeValue(forKey: id)
191192
}
192193
} else {
193194
do {

0 commit comments

Comments
 (0)