Skip to content

Thread safety issues with SharedPreferences #90

@callebtc

Description

@callebtc

Description:
Multiple SharedPreferences files are accessed from different threads without proper synchronization, creating race conditions.

Affected Files:

  • ThemeManager.kt: "app_prefs"
  • CashuWalletManager.kt: "CashuWalletPrefs"
  • ModernPOSActivity.kt: "NumoPrefs"
  • AmountDisplayManager.kt: preference access

Issues:

  1. Inconsistent preference file naming
  2. No synchronization for concurrent access
  3. Potential data corruption in multi-threaded scenarios

Fix Instructions:
Option 1 - Add synchronization:

@Synchronized
private fun getPrefs(): SharedPreferences {
    return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
}

Option 2 - Migrate to DataStore (recommended):

// Replace SharedPreferences with Jetpack DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0"

Recommendation: Migrate to DataStore for better thread safety and async support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions