Skip to content

Commit

Permalink
Fix a corner case of CancellableQueryHandle not resuming a continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
andersio committed Aug 11, 2024
1 parent 30998f6 commit bc25d48
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Sources/VitalHealthKit/HealthKit/Abstractions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,27 @@ final class CancellableQueryHandle<Result>: @unchecked Sendable {
let doWork = { store.stop(query) }
return (doWork, continuation)

default:
case
let (.cancelled, .running(_, _, continuation)),
let (.completed, .running(_, _, continuation)):

// The handle is cancelled before it started running.
// Don't start the query, but make sure the continuation is called.
let doWork = { continuation.resume(throwing: CancellationError()) }
return (doWork, nil)

case (.completed, .cancelled), (.cancelled, .cancelled), (.idle, .cancelled):
// Not illegal, can gracefully ignore
return (nil, nil)

case (.cancelled, .completed):
// Not illgealm can gracefully ignore
// Any registered continuation would have been called backed already
// during running -> cancelled|completed.
return (nil, nil)

case (.completed, .completed), (.running, .running), (.idle, .completed), (_, .idle):
fatalError("Illegal CancellableQueryHandle state transition")
}
}

Expand Down

0 comments on commit bc25d48

Please sign in to comment.