Skip to content

Conversation

swolfand
Copy link
Collaborator

This pull request introduces several improvements to the ConfigurationManager and DeviceIdGenerator classes, focusing on optimizing asynchronous operations, enhancing thread safety, and improving initialization performance. Additionally, it updates the clerk-sdk library version and enables code minification for release builds. Below is a categorized summary of the most important changes.

Build Configuration Updates:

  • Updated clerk-sdk library version from 0.1.1 to 0.1.2 in gradle/libs.versions.toml.
  • Enabled code minification for release builds in source/build.gradle.kts by setting isMinifyEnabled to true.

Asynchronous Initialization Enhancements:

  • Optimized ConfigurationManager initialization by moving storage setup, device ID generation, and lifecycle monitoring to background tasks using async blocks. This avoids blocking the main thread and improves startup performance. [1] [2]
  • Improved token refresh logic to use asynchronous operations (async) for fetching tokens, ensuring the refresh loop remains non-blocking.

Device Attestation Improvements:

  • Refactored device attestation in ConfigurationManager to run independently in background tasks using async for better isolation and concurrency. Added retry logic with exponential backoff for failed attestation attempts. [1] [2] [3]

Device ID Generation Enhancements:

  • Enhanced DeviceIdGenerator to prioritize loading existing device IDs from storage before generating new ones. Improved thread safety with double-checked locking and added asynchronous persistence for new IDs. [1] [2]

Logging and Error Handling:

  • Improved logging throughout ConfigurationManager and DeviceIdGenerator to provide more detailed insights into initialization, attestation, and token refresh processes. Enhanced error handling to ensure background operations fail gracefully without blocking critical workflows. [1] [2]

Copy link

linear bot commented Jul 15, 2025

@swolfand swolfand requested a review from Copilot July 15, 2025 21:00
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the startup and device ID flows to run heavy operations asynchronously, improves thread safety, and updates build configuration.

  • Moves storage setup, device ID generation, and attestation into background async/launch blocks.
  • Updates token refresh logic to use non-blocking coroutines and adds exponential backoff for attestation retries.
  • Bumps clerk-sdk version and enables code minification for release builds.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
DeviceIdGenerator.kt Simplified storage load/save checks and added async persistence logic
ConfigurationManager.kt Converted init, refresh, and attestation flows to background tasks
AppLifecycleListener.kt Ensured lifecycle observer addition happens on the main thread
source/src/test/java/com/clerk/configuration/DeviceIdGeneratorTest.kt Updated tests to use explicit call counts instead of constants
source/build.gradle.kts Enabled isMinifyEnabled for release builds
gradle/libs.versions.toml Upgraded clerk-sdk from 0.1.1 to 0.1.2
Comments suppressed due to low confidence (3)

source/src/main/kotlin/com/clerk/configuration/ConfigurationManager.kt:189

  • [nitpick] The log message fires after awaiting the data refresh job, so it actually indicates background initialization completed, not started. Update the wording for accuracy.
      ClerkLog.d("ConfigurationManager configured successfully - background initialization started")

source/src/main/kotlin/com/clerk/configuration/DeviceIdGenerator.kt:150

  • Current code always overwrites any existing stored device ID. Consider re-adding a check (load first) so you only save when no ID exists to avoid unintentional overrides.
      StorageHelper.saveValue(StorageKey.DEVICE_ID, deviceId)

source/src/main/kotlin/com/clerk/configuration/ConfigurationManager.kt:217

  • Using async without awaiting the returned Deferred can launch untracked coroutines. Use launch or explicitly await the result to ensure proper coroutine lifecycle and error handling.
              async { session.fetchToken(GetTokenOptions(skipCache = false)) }

// Ensure observer is added on the main thread
if (Looper.myLooper() == Looper.getMainLooper()) {
// Already on main thread
ProcessLifecycleOwner.get().lifecycle.addObserver(listener)
Copy link

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Repeated calls to configure can add the same observer multiple times. Consider tracking a flag or removing existing observers to make configure idempotent.

Copilot uses AI. Check for mistakes.

@swolfand swolfand merged commit 79cf28d into main Jul 16, 2025
4 checks passed
@swolfand swolfand deleted the sam/mobile-92-android-investigate-critical-path-being-more-async branch July 16, 2025 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant