Implement press, and hold switch actions#2
Merged
Merged
Conversation
4 tasks
OwenMcGirr
added a commit
that referenced
this pull request
Jan 2, 2026
Fixed race condition in getInstance() where multiple threads could create
multiple database instances due to incomplete double-checked locking.
**Problem:**
The original code only checked INSTANCE before entering synchronized:
```kotlin
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(...).build()
INSTANCE = instance
instance
}
```
**Race Condition Scenario:**
1. Thread A checks INSTANCE == null
2. Thread B checks INSTANCE == null
3. Thread A enters synchronized, creates DB instance #1
4. Thread B waits for lock
5. Thread B enters synchronized, creates DB instance #2 (BUG!)
6. Two database instances exist, causing corruption
**Solution:**
Added second null-check inside synchronized block (double-checked locking):
```kotlin
return INSTANCE ?: synchronized(this) {
// Second check AFTER acquiring lock
INSTANCE ?: Room.databaseBuilder(...).build().also { INSTANCE = it }
}
```
**How It Works:**
1. Thread A checks INSTANCE == null (first check)
2. Thread B checks INSTANCE == null (first check)
3. Thread A enters synchronized, checks again (second check), creates instance
4. Thread B waits for lock
5. Thread B enters synchronized, checks again (second check), sees INSTANCE != null
6. Thread B returns existing instance - no duplicate!
**Thread Safety:**
- @volatile ensures INSTANCE writes are visible across threads
- synchronized prevents concurrent creation
- Double-check minimizes synchronization overhead (only on first access)
This is the standard double-checked locking pattern for thread-safe
singleton initialization.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.