I'm unable to use the withTaskCancellaionHandler without a static analysis error

Hi,

We have an API that I'm trying to prototype some async await extensions for. Most of our asynchronous methods are cancelable by returning an object that implements our AGSCancelable protocol that includes a cancel() method.

Here's the code I have to try to extend a geocoding method on our AGSLocatorTask class:

extension AGSLocatorTask {
    func geocode(withSearchText text: String) async throws -> [AGSGeocodeResult] {
        typealias GeocodeContinuation = CheckedContinuation<[AGSGeocodeResult], Error>
        
        var cancelable: AGSCancelable?
        
        return try await withTaskCancellationHandler(handler: {
            cancelable?.cancel()
        }, operation: {
            return try await withCheckedThrowingContinuation({ (continuation: GeocodeContinuation) in
                
                cancelable = self.geocode(withSearchText: text) { results, error in
                    if let error = error {
                        continuation.resume(throwing: error)
                    } else {
                        continuation.resume(returning: results!)
                    }
                }
            })
        })
    }
}

This works great, but I have to comment out the cancelable?.cancel() call or else I get a Reference to captured var 'cancelable' in concurrently-executing code static analysis error (see attached screenshot).

Could someone explain how to fix this, or is this a bug in the Xcode beta?

Many thanks, and thanks for a great WWDC!!

Oops. I missed the screenshot. Apologies. title😒 wift-static-analysis-error.png;width=1768;height=114

Getting the same error.

You can try by implementing a wrapper.

You could try this workaround:

var cancelable: AGSCancelable?
let onCancel = { cancelable?.cancel() }
return try await withTaskCancellationHandler {
      onCancel()
    } operation: {
I'm unable to use the withTaskCancellaionHandler without a static analysis error
 
 
Q