Skip to content

Commit

Permalink
Merge branch 'fix/swift_concurrency' into 'develop'
Browse files Browse the repository at this point in the history
fix: SWIFT TASK CONTINUATION MISUSE: awaitResultWithID(_:) leaked its continuation!

See merge request papers/airgap/airgap-vault!519
  • Loading branch information
isordo committed Sep 26, 2024
2 parents 1441b75 + 7da24df commit 56dc04e
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ios/App/App/Helpers/JS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protocol JSONConvertible {

class JSAsyncResult: NSObject, Identifiable, WKScriptMessageHandler {
private typealias Listener = (Result<Any, Error>) -> ()

private static let defaultName: String = "jsAsyncResult"

private static let fieldID: String = "id"
Expand All @@ -49,34 +49,35 @@ class JSAsyncResult: NSObject, Identifiable, WKScriptMessageHandler {
await selfWeak?.resultManager.setResult(result, forID: id)
}
}

return id
}

func awaitResultWithID(_ id: String) async throws -> Any {
if let result = await resultManager.result[id] {
return try result.get()
}

return try await withCheckedThrowingContinuation { continuation in
Task {
await listenerRegistry.add(forID: id) { result in
if let result = await resultManager.result[id] {
continuation.resume(with: result)
} else {
await listenerRegistry.add(forID: id) { result in
continuation.resume(with: result)
}
}
}
}
}


func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard message.name == id, let body = message.body as? [String: Any] else { return }

Task {
guard let id = body[Self.fieldID] as? String else { return }

do {
let result = body[Self.fieldResult]
let error = body[Self.fieldError]

if let result = result, error == nil {
await listenerRegistry.notifyAllWithID(id, with: .success(result))
} else if let error = error {
Expand Down

0 comments on commit 56dc04e

Please sign in to comment.