fix: share WallpaperChangeLock singleton between service and worker

WallpaperChangeService and WallpaperChangeWorker each had their own
private companion-object Mutex, so simultaneous runs (manual change +
scheduled worker) were not mutually excluded.  Extract a @Singleton
WallpaperChangeLock and inject it into both so the same Mutex is used
across all wallpaper-change entry points.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Anthonyy232andClaude Sonnet 4.6 committed 2026-03-28 11:23:41 -07:00
1 parent bc26d3c75f
commit c93e2f71f1
3 files changed
+29 -21

No files matched your search

@@ -0,0 +1,18 @@
package com.anthonyla.paperize.service
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton
/**
* Shared mutex for wallpaper change operations.
*
* Both [WallpaperChangeService] and [WallpaperChangeWorker] can trigger wallpaper
* changes simultaneously (e.g. a manual change fires the service while a scheduled
* WorkManager job fires the worker). This singleton ensures only one change runs
* at a time across both entry points.
*/
@Singleton
class WallpaperChangeLock @Inject constructor() {
val mutex = Mutex()
}
@@ -20,13 +20,13 @@ import com.anthonyla.paperize.domain.repository.WallpaperRepository
import com.anthonyla.paperize.domain.usecase.ChangeWallpaperUseCase
import com.anthonyla.paperize.domain.usecase.ReapplyEffectsUseCase
import com.anthonyla.paperize.presentation.MainActivity
import com.anthonyla.paperize.service.WallpaperChangeLock
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import javax.inject.Inject
@@ -51,6 +51,9 @@ class WallpaperChangeService : Service() {
@Inject
lateinit var settingsRepository: SettingsRepository
@Inject
lateinit var wallpaperChangeLock: WallpaperChangeLock
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private lateinit var wallpaperManager: WallpaperManager
@@ -62,14 +65,6 @@ class WallpaperChangeService : Service() {
const val ACTION_REAPPLY_EFFECTS = Constants.ACTION_REAPPLY_EFFECTS
const val EXTRA_SCREEN_TYPE = Constants.EXTRA_SCREEN_TYPE
private const val ERROR_NOTIFICATION_ID = Constants.NOTIFICATION_ID + 1
/**
* Mutex to prevent concurrent wallpaper changes
* Multiple service instances can be started simultaneously from different triggers
* (alarms, manual changes, etc.). This mutex ensures only one wallpaper change
* operation runs at a time, preventing race conditions.
*/
private val wallpaperChangeMutex = Mutex()
}
override fun onCreate() {
@@ -112,7 +107,7 @@ class WallpaperChangeService : Service() {
private fun handleChangeWallpaper(screenType: ScreenType, startId: Int) {
serviceScope.launch {
// Use mutex to prevent concurrent wallpaper changes
wallpaperChangeMutex.withLock {
wallpaperChangeLock.mutex.withLock {
try {
// Get schedule settings to determine which albums to use
val settings = settingsRepository.getScheduleSettings()
@@ -308,7 +303,7 @@ class WallpaperChangeService : Service() {
*/
private fun handleReapplyEffects(screenType: ScreenType, startId: Int) {
serviceScope.launch {
wallpaperChangeMutex.withLock {
wallpaperChangeLock.mutex.withLock {
try {
val settings = settingsRepository.getScheduleSettings()
when (screenType) {
@@ -12,9 +12,9 @@ import com.anthonyla.paperize.core.constants.Constants
import com.anthonyla.paperize.domain.repository.SettingsRepository
import com.anthonyla.paperize.domain.repository.WallpaperRepository
import com.anthonyla.paperize.domain.usecase.ChangeWallpaperUseCase
import com.anthonyla.paperize.service.WallpaperChangeLock
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
/**
@@ -29,17 +29,12 @@ class WallpaperChangeWorker @AssistedInject constructor(
@Assisted workerParams: WorkerParameters,
private val changeWallpaperUseCase: ChangeWallpaperUseCase,
private val settingsRepository: SettingsRepository,
private val wallpaperRepository: WallpaperRepository
private val wallpaperRepository: WallpaperRepository,
private val wallpaperChangeLock: WallpaperChangeLock
) : CoroutineWorker(context, workerParams) {
companion object {
private const val TAG = "WallpaperChangeWorker"
/**
* Mutex to prevent concurrent wallpaper changes
* Ensures only one wallpaper change operation runs at a time
*/
private val wallpaperChangeMutex = Mutex()
}
override suspend fun doWork(): Result {
@@ -49,8 +44,8 @@ class WallpaperChangeWorker @AssistedInject constructor(
Log.d(TAG, "Starting wallpaper change for $screenType")
// Use mutex to prevent concurrent wallpaper changes
wallpaperChangeMutex.withLock {
// Use shared lock to prevent concurrent wallpaper changes across service and worker
wallpaperChangeLock.mutex.withLock {
changeWallpaper(screenType)
}