'SWIFT TASK CONTINUATION MISUSE: leaked its continuation - for delegate?

I'm trying to extend my class with async/await capabilities, but at run-time there is an error in the console:

SWIFT TASK CONTINUATION MISUSE: query(_:) leaked its continuation!

Below is the class I'm trying to add the continuation to which uses a delegate:

class LocalSearch: NSObject, MKLocalSearchCompleterDelegate {
    private let completer: MKLocalSearchCompleter
    private var completionContinuation: CheckedContinuation<[MKLocalSearchCompletion], Error>?

    init() {
        completer = MKLocalSearchCompleter()
        super.init()
        completer.delegate = self
    }

    func query(_ value: String) async throws -> [MKLocalSearchCompletion] {
        try await withCheckedThrowingContinuation { continuation in
            completionContinuation = continuation

            guard !value.isEmpty else {
                completionContinuation?.resume(returning: [])
                completionContinuation = nil
                return
            }

            completer.queryFragment = value
        }
    }

    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        completionContinuation?.resume(returning: completer.results)
        completionContinuation = nil
    }

    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        completionContinuation?.resume(throwing: error)
        completionContinuation = nil
    }
}

This is how I use it:

let localSearch = LocalSearch()

do {
    let results = try await localSearch.query("toront")
    print(results)
} catch {
    print(error)
}

What am I doing wrong or is there a better way to achieve this?



Solution 1:[1]

This message appears if a continuation you created via withCheckedContinuation, or withCheckedThrowingContinuation doesn't report success or failure before being discarded. This is will lead to resource leaking:

Resuming from a continuation more than once is undefined behavior. Never resuming leaves the task in a suspended state indefinitely, and leaks any associated resources. CheckedContinuation logs a message if either of these invariants is violated.

Excerpt taken from the documentation for CheckedContinuation (emphasis mine).

Here are possible causes for this to happen:

  1. not all code paths resume the continuation, e.g. there is an if/guard/case that exits the scope without instructing the continuation to report success/failure
class Searcher {

    func search(for query: String) async throws -> [String] {
        await withCheckedContinuation { continuation in
            someFunctionCall(withCompletion: { [weak self] in
                guard let `self` = self else {
                    // if `result` doesn't have the expected value, the continuation
                    // will never report completion
                    return    
                }
                continuation.resume(returning: something)
            })
        }
    }
}
  1. an "old"-style async function doesn't call the completion closure on all paths; this is a less obvious reason, and sometimes a harder to debug one:
class Searcher {
    private let internalSearcher = InternalSearcher()

    func search(for query: String) async throws -> [String] {
        await withCheckedContinuation { continuation in
            internalSearcher.search(query: query) { result in
                // everything fine here
                continuation.resume(returning: result)
            }
        }
    }
}

class InternalSearcher {

   func search(query: String, completion: @escaping ([String]) -> Void {
        guard !query.isEmpty else {
            return
            // legit precondition check, however in this case,
            // the completion is not called, meaning that the
            // upstream function call will imediately discard
            // the continuation, without instructing it to report completion
        }

        // perform the actual search, report the results
    }
}
  1. the continuation is stored as a property when a function is called; this means that if a second function call happens while the first one is in progress, then the first completion will be overwritten, meaning it will never report completion:
class Searcher {
    var continuation: CheckedContinuation<[String], Error>?

    func search(for query: String) async throws -> [String] {
        try await withCheckedTrowingContinuation { continuation in
            // note how a second call to `search` will overwrite the
            // previous continuation, in case the delegate method was
            // not yet called
            self.continuation = continuation
           
            // trigger the searching mechanism
        }
    }

    func delegateMethod(results: [String]) {
        self.continuation.resume(returning: results)
        self.continuation = nil
    }
}

#1 and #2 usually happen when dealing with functions that accept completion callbacks, while #3 usually happens when dealing with delegate methods, since in that case, we need to store the continuation somewhere outside the async function scope, in order to access it from the delegate methods.

Bottom line - try to make sure that a continuation reports completion on all possible code paths, otherwise, the continuation will indefinitely block the async call, leading to the task associated with that async call leaking its associated resources.


In your case, what likely happened is that a second query() call occurred before the first call had a chance to finish.

And in that case, the first continuation got discarded without reporting completion, meaning the first caller never continued the execution after the try await query() call, and this is not ok at all.

The following piece of code needs to be fixed, in order not to overwrite a pending continuation:

func query(_ value: String) async throws -> [MKLocalSearchCompletion] {
    try await withCheckedThrowingContinuation { continuation in
        completionContinuation = continuation

One quick solution would be to store an array of continuations, resume all continuations in the delegate methods, and clear the array afterward. Also, in your specific case, you could simply extract the validation out of the continuation code, as you are allowed to synchronously return/throw, even when in an async function:

func query(_ value: String) async throws -> [MKLocalSearchCompletion] {
    guard !value.isEmpty else {        
        return []
    }

    return try await withCheckedThrowingContinuation { continuation in
        continuations.append(continuation)
        completer.queryFragment = value
    }
}

func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
    continuations.forEach { $0.resume(returning: completer.results) }
    continuations.removeAll() 
}

func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
    continuations.forEach { $0.resume(throwing: error) }
    continuations.removeAll() 
}

I'd also strongly recommend converting your class to an actor, in order to avoid data races, regardless if you store one continuation, like now, or you use an array. The reason is that the continuation property is consumed from multiple threads and at some point you might end up with two threads concurrently accessing/writing the property.

Solution 2:[2]

I think the problem is here -

func query(_ value: String) async throws -> [MKLocalSearchCompletion] {
    try await withCheckedThrowingContinuation { continuation in
        // storing into a variable makes this continuation instance outlive the scope of it
        // In other words, it leaks OR escapes the scope 
        // This is same as why we need to add @escaping attribute for callback functions arguments 
        // those are either stored in variables like this
        // or passed to other functions (escaping scope of current function)
        completionContinuation = continuation

        // Try commenting above line, the warning should go away
        // And your code will stop working as well :)
        // How to design this component is other question.
    }
}

UPDATE

import MapKit

class LocalSearch: NSObject, MKLocalSearchCompleterDelegate {
    typealias Completion = (_ results: [MKLocalSearchCompletion]?, _ error: Error?) -> Void
    
    private let completer: MKLocalSearchCompleter
    private var completion: Completion?
    
    override init() {
        completer = MKLocalSearchCompleter()
        super.init()
        completer.delegate = self
    }
    
    func query(_ value: String, completion: @escaping Completion) {
        self.completion = completion
        completer.queryFragment = value
    }
    
    func query(_ value: String) async throws -> [MKLocalSearchCompletion] {
        try await withCheckedThrowingContinuation { continuation in
            
            guard !value.isEmpty else {
                continuation.resume(returning: [])
                return
            }
            
            self.query(value, completion: { (results, error) in
                if let error = error {
                    continuation.resume(throwing: error)
                } else {
                    continuation.resume(returning: results ?? [])
                }
            })
        }
    }
    
    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        completion?(completer.results, nil)
    }
    
    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        completion?(nil, error)
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2