Skip to content

Commit d913407

Browse files
committed
Don't use withTaskCancellationHandler to avoid concurrency issues with actors.
1 parent 81c8323 commit d913407

File tree

1 file changed

+39
-55
lines changed

1 file changed

+39
-55
lines changed

Sources/ComposableArchitecturePattern/Server.swift

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -236,34 +236,27 @@ public extension Server {
236236
logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Start]")
237237
}
238238

239-
return try await withTaskCancellationHandler(
240-
operation: { [weak self] in
241-
let (data, response) = try await URLSession.shared.data(for: request)
242-
243-
self?.requestsBeingProcessed.remove(requestUID)
244-
245-
if self?.logActivity == .all {
246-
self?.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Finish]")
247-
}
248-
249-
guard try response.analyzeAsHTTPResponse() else {
250-
self?.logger.error("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Failed }")
251-
throw ServerAPIError.unknown(description: "Unable to complete server response.")
252-
}
253-
254-
if self?.logActivity == .all {
255-
self?.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Success }")
256-
}
257-
258-
guard let data: T = try self?._decode(data: data, dateDecodingStrategy: dateDecodingStrategy, keyDecodingStrategy: keyDecodingStrategy) else {
259-
throw ServerAPIError.unableToDecode(description: NSLocalizedString("Unable to decode object", comment: ""), error: nil)
260-
}
261-
return data
262-
},
263-
onCancel: { [weak self] in
264-
self?.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Cancelled]")
265-
}
266-
)
239+
let (data, response) = try await URLSession.shared.data(for: request)
240+
241+
self.requestsBeingProcessed.remove(requestUID)
242+
243+
if self.logActivity == .all {
244+
self.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Finish]")
245+
}
246+
247+
guard try response.analyzeAsHTTPResponse() else {
248+
self.logger.error("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Failed }")
249+
throw ServerAPIError.unknown(description: "Unable to complete server response.")
250+
}
251+
252+
if self.logActivity == .all {
253+
self.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Success }")
254+
}
255+
256+
guard let data: T = try self._decode(data: data, dateDecodingStrategy: dateDecodingStrategy, keyDecodingStrategy: keyDecodingStrategy) else {
257+
throw ServerAPIError.unableToDecode(description: NSLocalizedString("Unable to decode object", comment: ""), error: nil)
258+
}
259+
return data
267260
}
268261

269262
/// Send the given request to the server and return the result.
@@ -273,33 +266,24 @@ public extension Server {
273266
logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Start]")
274267
}
275268

276-
return try await withTaskCancellationHandler(
277-
operation: {
278-
let (_, response) = try await URLSession.shared.data(for: request)
279-
280-
self.requestsBeingProcessed.remove(requestUID)
281-
282-
if self.logActivity == .all {
283-
logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Finish]")
284-
}
285-
286-
guard try response.analyzeAsHTTPResponse() else {
287-
logger.error("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Failed }")
288-
throw ServerAPIError.unknown(description: "Unable to complete server response.")
289-
}
290-
291-
if self.logActivity == .all {
292-
logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Success }")
293-
}
294-
295-
return true
296-
},
297-
onCancel: {
298-
if self.logActivity == .all {
299-
logger.error("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Cancelled }")
300-
}
301-
}
302-
)
269+
let (_, response) = try await URLSession.shared.data(for: request)
270+
271+
self.requestsBeingProcessed.remove(requestUID)
272+
273+
if self.logActivity == .all {
274+
logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Finish]")
275+
}
276+
277+
guard try response.analyzeAsHTTPResponse() else {
278+
logger.error("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Failed }")
279+
throw ServerAPIError.unknown(description: "Unable to complete server response.")
280+
}
281+
282+
if self.logActivity == .all {
283+
logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) { Success }")
284+
}
285+
286+
return true
303287
}
304288

305289
/// Checks if the `apis` contains the given api.

0 commit comments

Comments
 (0)