[expo-secure-store][iOS] Run the async keychain functions on a private serial queue
Vlad Petrenko
Summary
On iOS, expo-secure-store's three async functions (getValueWithKeyAsync, setValueWithKeyAsync, deleteValueWithKeyAsync) run on the default expo.modules.AsyncFunctionQueue. That is one serial queue shared by every module that doesn't call runOnQueue. This proposal gives SecureStore its own serial queue:
// SecureStoreModule.definition()
let queue = DispatchQueue(label: "expo.modules.securestore", qos: .userInitiated)
AsyncFunction("getValueWithKeyAsync") { ... }.runOnQueue(queue)
AsyncFunction("setValueWithKeyAsync") { ... }.runOnQueue(queue)
AsyncFunction("deleteValueWithKeyAsync") { ... }.runOnQueue(queue)
This is not a bug claim. SecureStore follows the framework default correctly. The proposal is about robustness for a module that auth flows depend on.
Motivation
- Head-of-line blocking from other modules. Any module that blocks the shared queue also blocks keychain reads and writes. The concrete case is https://github.com/expo/expo/issues/52216 (fix in progress: https://github.com/expo/expo/pull/53509). There, Notifications.getPermissionsAsync() waited on a semaphore with no timeout. An OS callback that never arrived left SecureStore.setItemAsync pending for the rest of the process, and a refresh-token write, and with it sign-in, never finished. #53509 fixes that one caller. Any future blocking call in any module would cause the same failure, and an app can't defend against it except by switching to the sync API on the JS thread.
- SecureStore blocking others. With requireAuthentication: true, SecItemCopyMatching / SecItemUpdate show the biometric/passcode UI and block the calling thread until the user responds. On the shared queue, every other module's async functions wait for the duration of that prompt. A private queue confines the wait to SecureStore.
- Precedent. Several modules already pick their own queue with runOnQueue (expo-file-system, expo-haptics, expo-image-picker, expo-constants, expo-web-browser, expo-updates, and expo-notifications' PushTokenModule/BadgeModule, all on .main). Expo's iOS review guideline recommends a private queue for unbounded work: https://github.com/expo/expo/blob/e01e8b8dbf492bbafd5f998b2fad9e8d015ec251/.expo-agents/code-review/agents/correctness-ios.md#L178-L182
Semantics
- Ordering within the module is unchanged. A serial queue keeps SecureStore's async calls in call order, so a set followed by a get behaves as today.
- Ordering relative to other modules changes. Today a SecureStore call queued behind, for example, an expo-crypto call runs after it. With a private queue there is no such guarantee. No ordering across modules is documented, and it is unlikely an app relies on one, but it is the one observable difference.
- The sync functions (getValueWithKeySync, setValueWithKeySync) are unaffected. They already run on the JS thread, concurrently with the async queue, so keychain access is already concurrent today. Keychain Services is thread-safe.
Cost and risk
- One extra GCD serial queue per process. A queue holds no thread while idle.
- About six lines, no API change, no JS change.
- QoS .userInitiated matches the current default queue.
- Scope is iOS only. Android was not examined.
Related
- https://github.com/expo/expo/issues/52216 - getPermissionsAsync blocks the shared AsyncFunctionQueue (open, canonical)
- https://github.com/expo/expo/pull/53509 - fix PR for the notifications side (open)