Skip to main content
coreydaley.dev

A StoreKit unlock while the app is still launching

When Purrchiver launches, it has to decide whether you own the one-time unlock before it shows anything that depends on it. That decision takes a few awaits, and StoreKit doesn’t stop delivering transactions while it runs. A refund or an Ask to Buy approval can arrive in the middle. Until recently, the code had paths where that could leave the app wrong: unlocked for someone who’d been refunded, locked for someone who’d paid, or telling someone their unlock was revoked when they’d never had it. None of these was reported or reproduced. I found them reading the code while writing a StoreKit guide.

All three come down to order, not trust. Every transaction involved is a real, verified one; the question is which verified fact arrived last, and whether the app was listening when it did. The fixes are on main and ship in the next Purrchiver release. None of them is in 1.1.1.

The unlock is a trust decision made on the Mac

Purrchiver has no server. StoreKit 2 verifies each transaction on the device, and the app grants the unlock only from a .verified transaction with no revocation; an unverified one is only ever finished, never trusted. The first round of edge cases is in a StoreKit 2 one-time unlock with no server. With no backend to reconcile against, a wrong answer at launch can stick until the next launch.

What launch looks like

The launch sequence is short:

  1. Drain Transaction.unfinished, finishing each one.
  2. Scan Transaction.currentEntitlements for unlock transactions that grant.
  3. Decide isUnlocked, then mark the store resolved.

Meanwhile the Transaction.updates listener starts with the store and runs for the life of the app, through all three steps. Two earlier rules shape what it does. Since 1.1, a verified, non-revoked grant on the listener unlocks at once, because currentEntitlements can lag behind a new purchase. And a transaction drained at launch doesn’t unlock by itself, because the copy StoreKit kept can predate a refund that currentEntitlements already shows.

The mistake under all three problems: I’d treated launch as one step, when the listener is allowed to change the answer through all of it.

A copy from before the refund

The drain had a guard against stale copies. The listener didn’t. If Transaction.updates ever handed over an older copy of a transaction that had since been refunded, the listener would see a verified grant and unlock for the rest of the session. I don’t know that StoreKit ever delivers a copy like that. I couldn’t rule it out, so the listener now asks for the latest copy before trusting one that looks like a grant:

private func latestCopyIsRevoked(id: UInt64) async -> Bool {
    guard case .verified(let latest)? = await Transaction.latest(for: unlockProductID),
          latest.id == id else { return false }
    return latest.revocationDate != nil || latest.revocationReason != nil
}

A brand-new purchase isn’t revoked, so latest(for:) can’t veto it, and a new grant still unlocks immediately even while currentEntitlements lags.

A grant the launch decision could throw away

The launch decision was one line, computed across an await while the listener was live. If the listener delivered a grant during the scan, say an Ask to Buy approval made while the app was closed, and currentEntitlements hadn’t caught up, the decision would overwrite it with false. The owner would stay locked until the next launch, and the app would remember that locked state as what it knew.

Now every delivery records whether that transaction’s newest copy grants (its latest word), and the decision is a pure function over three inputs: what the scan listed, what’s been delivered, and which grants came from the listener.

static func unlockedAtLaunch(granting: [UInt64], delivered: [UInt64: Bool],
                             listenerGrants: Set<UInt64>) -> Bool {
    (granting + listenerGrants).contains { delivered[$0] != false }
}

A listed transaction counts. A listener grant counts even if the scan missed it. Anything whose latest word is “doesn’t grant”, such as a refund that arrived after the grant, counts for neither. A drained grant on its own still doesn’t unlock.

Changing the answer before it was decided

My own doc comment on the store says that until it’s resolved, callers should treat the lock state as provisional and wait. Several places in the app read isUnlocked directly instead. And the listener and the drain both set isUnlocked as they went, before the decision. That opened three paths:

  • A drained purchase could flip the app to unlocked, and the launch decision then flip it back.
  • A grant and its refund both arriving during launch would go unlocked, then locked, which looks exactly like a mid-session refund. The app would show its “Your Purrchiver unlock was revoked” alert for an app that had never been unlocked.
  • A purchase completed before the decision could be overwritten by a scan that ran before it. I’ve only made that happen in a test.

The fix is a guard: until the store is resolved, a delivery is recorded and finished, and nothing else.

guard isResolved else {
    await tx.finish()
    return
}

The purchase path still unlocks at once when you buy; it now also records itself as a grant, so the decision agrees with it. And the decision moved after the last await in the launch path, so the listener has nothing left to deliver that the decision hasn’t seen.

What the tests cover, and what they can’t

The decision is a pure function, so it has plain unit tests: a listed transaction unlocks unless its latest word says no, a listener grant counts though the scan missed it, a drained grant alone doesn’t unlock, a later refund wins, one refund doesn’t cancel a different grant, and nothing at all stays locked.

The rest are SKTestSession tests. I can’t get SKTestSession to stage a real launch with StoreKit delivering during it, so these use two DEBUG-only seams: one hands the store a chosen transaction as the listener would, and one runs a step between the scan and the decision.

After launch has resolved, a chosen copy is delivered:

  • a copy captured before a refund, delivered after it, leaves the app locked;
  • a grant delivered while the scan lists nothing unlocks at once.

Between the scan and the decision:

  • a grant delivered there survives the decision;
  • a drained grant delivered there leaves isUnlocked false at that moment, and locked after;
  • a grant and its refund delivered there raise no alert;
  • a purchase made there stays unlocked after the decision.

One more test drives the real listener: an Ask to Buy purchase sits pending, the test approves it in the session, and the listener unlocks; then a refund re-locks and sets the alert. That runs after launch, not during it.

What that leaves untested:

  • The stale-copy case wasn’t reproduced through real StoreKit delivery, and whether an old copy can reach the listener after a refund is unconfirmed.
  • The launch window was only hit by forcing the timing through the seam.
  • The “lists nothing” test fakes lag by clearing the session, which also empties latest(for:). It doesn’t show a real lag where latest(for:) already returns the new copy.
  • That latest(for:) shows a refund before currentEntitlements does is an assumption in a code comment, not something a test pins in real delivery.
  • No test checks what the direct readers of isUnlocked show on screen during launch.

And like every SKTestSession entitlement test, these only mean anything when they run in the logged-in GUI session. From a plain shell they fail for reasons unrelated to the code.

What I got wrong

I treated launch as one step. In 1.1 I made the listener decide from the delivered transaction and made launch decide from the scan. Each was right on its own. I never asked what happens when both run at once, and they always run at once.

I guarded one path and not its twin. The drain already knew a stored copy can predate a refund. The listener, handling the same kind of transaction, didn’t. The comment explaining the risk was in the same file, one function over.

I documented “wait until resolved” and didn’t enforce it. Several readers skipped the wait. Telling callers to be careful is weaker than making the value not change until it means something.

I’d learned the settle lesson once and didn’t apply it everywhere. In June I added a bounded poll to one refund test because SKTestSession returns before currentEntitlements catches up. Just before these fixes, the store tests failed 4 of 8 runs on unchanged code, a different test each time, for the same reason across the suite. They now wait for the lists to settle after every clear, buy and refund.

What to take away

  • If a listener runs during your launch decision, the decision has to include what it delivered, and it has to come after the last await.
  • Before the decision, record deliveries; don’t act on them.
  • A verified transaction can still be out of date. Check the latest copy before trusting a grant.
  • When a timing bug can’t be reproduced, a seam that chooses the moment is worth having, as long as you say that’s what it is.

What does your app decide at launch while something else is already allowed to change the answer?

Related posts