Skip to content

feat. multi process - #4

Draft
DHR60 wants to merge 1 commit into
masterfrom
multi_process
Draft

feat. multi process#4
DHR60 wants to merge 1 commit into
masterfrom
multi_process

Conversation

@DHR60

@DHR60 DHR60 commented Jun 18, 2026

Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request configures several services to run in a separate ":engine" process and updates the Room database and DataStore instances to support multi-process access. The review feedback highlights critical issues in the new multi-process DataStore implementations, including a compilation error due to referencing the internal "PreferencesFileSerializer", missing "@volatile" annotations on the "INSTANCE" variables for double-checked locking, and unsafe synchronization on a framework class instead of a private lock object.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +9 to +17
private var INSTANCE: DataStore<ConfigItem>? = null

val Context.configDataStore: DataStore<ConfigItem>
get() = INSTANCE ?: synchronized(MultiProcessDataStoreFactory::class.java) {
INSTANCE ?: MultiProcessDataStoreFactory.create(
serializer = ConfigItemSerializer,
produceFile = { this.applicationContext.dataStoreFile("config.json") }
).also { INSTANCE = it }
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Concurrency & Thread Safety Issues

  1. Missing @Volatile: The INSTANCE variable is used in a double-checked locking pattern but is not marked as @Volatile. Without this, writes to INSTANCE might not be immediately visible to other threads, or the compiler/CPU might reorder instructions, leading to another thread reading a partially initialized INSTANCE.
  2. Locking on a Framework Class: Locking on MultiProcessDataStoreFactory::class.java is not recommended. It is a framework class, and locking on it can lead to unexpected contention or deadlocks if other parts of the app or libraries also lock on it. Use a private lock object instead.
Suggested change
private var INSTANCE: DataStore<ConfigItem>? = null
val Context.configDataStore: DataStore<ConfigItem>
get() = INSTANCE ?: synchronized(MultiProcessDataStoreFactory::class.java) {
INSTANCE ?: MultiProcessDataStoreFactory.create(
serializer = ConfigItemSerializer,
produceFile = { this.applicationContext.dataStoreFile("config.json") }
).also { INSTANCE = it }
}
@Volatile
private var INSTANCE: DataStore<ConfigItem>? = null
private val lock = Any()
val Context.configDataStore: DataStore<ConfigItem>
get() = INSTANCE ?: synchronized(lock) {
INSTANCE ?: MultiProcessDataStoreFactory.create(
serializer = ConfigItemSerializer,
produceFile = { this.applicationContext.dataStoreFile("config.json") }
).also { INSTANCE = it }
}

Comment on lines +10 to +20
private var INSTANCE: DataStore<Preferences>? = null

val Context.preferencesDataStore: DataStore<Preferences>
get() = INSTANCE ?: synchronized(MultiProcessDataStoreFactory::class.java) {
INSTANCE ?: MultiProcessDataStoreFactory.create(
serializer = PreferencesFileSerializer,
produceFile = {
this.applicationContext.dataStoreFile("config.preferences_pb")
}
).also { INSTANCE = it }
} No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Compilation & Concurrency Issues

  1. Compilation Error (PreferencesFileSerializer is internal): PreferencesFileSerializer is an internal object in the androidx.datastore.preferences.core package. Referencing it directly here will cause a compilation error. To resolve this, you can copy the implementation of PreferencesFileSerializer into your project or implement a custom serializer for Preferences.
  2. Missing @Volatile: The INSTANCE variable is used in a double-checked locking pattern but is not marked as @Volatile.
  3. Locking on a Framework Class: Locking on MultiProcessDataStoreFactory::class.java is not recommended. Use a private lock object instead.
Suggested change
private var INSTANCE: DataStore<Preferences>? = null
val Context.preferencesDataStore: DataStore<Preferences>
get() = INSTANCE ?: synchronized(MultiProcessDataStoreFactory::class.java) {
INSTANCE ?: MultiProcessDataStoreFactory.create(
serializer = PreferencesFileSerializer,
produceFile = {
this.applicationContext.dataStoreFile("config.preferences_pb")
}
).also { INSTANCE = it }
}
@Volatile
private var INSTANCE: DataStore<Preferences>? = null
private val lock = Any()
val Context.preferencesDataStore: DataStore<Preferences>
get() = INSTANCE ?: synchronized(lock) {
INSTANCE ?: MultiProcessDataStoreFactory.create(
serializer = PreferencesFileSerializer,
produceFile = {
this.applicationContext.dataStoreFile("config.preferences_pb")
}
).also { INSTANCE = it }
}

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