Skip to content

Commit 2dccf71

Browse files
committed
fix(shared_preferences_android): keep APIs registered until the last engine detaches
Resolves flutter/flutter#194920 by adding a reference count for engines attaching and detaching from the SharedPreferences plugins. The process-wide Pigeon APIs are only unregistered when the reference count drops to 0, preventing background tasks from clearing the API registration used by the main isolate.
1 parent 8f4a0de commit 2dccf71

5 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎packages/shared_preferences/shared_preferences_android/android/src/main/kotlin/io/flutter/plugins/sharedpreferences/LegacySharedPreferencesPlugin.kt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ internal constructor(private val listEncoder: SharedPreferencesListEncoder) :
4545

4646
override fun onAttachedToEngine(binding: FlutterPluginBinding) {
4747
setUp(binding.applicationContext)
48+
registeredCount++
4849
}
4950

5051
override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
51-
SharedPreferencesApiRegistrar().register(null)
52+
registeredCount--
53+
if (registeredCount == 0) {
54+
SharedPreferencesApiRegistrar().register(null)
55+
}
5256
}
5357

5458
override suspend fun setBool(key: String, value: Boolean): Boolean =
@@ -183,5 +187,11 @@ internal constructor(private val listEncoder: SharedPreferencesListEncoder) :
183187

184188
companion object {
185189
private const val BIG_INTEGER_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy"
190+
private var registeredCount = 0
191+
192+
@androidx.annotation.VisibleForTesting
193+
internal fun clearRegisteredCount() {
194+
registeredCount = 0
195+
}
186196
}
187197
}

‎packages/shared_preferences/shared_preferences_android/android/src/main/kotlin/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.kt‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ const val DOUBLE_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu"
4242

4343
/// SharedPreferencesPlugin
4444
class SharedPreferencesPlugin() : FlutterPlugin, SharedPreferencesAsyncApi {
45+
companion object {
46+
private var registeredCount = 0
47+
48+
@androidx.annotation.VisibleForTesting
49+
internal fun clearRegisteredCount() {
50+
registeredCount = 0
51+
}
52+
}
53+
4554
private lateinit var context: Context
4655
private var backend: SharedPreferencesBackend? = null
4756
private var legacyPlugin: LegacySharedPreferencesPlugin? = null
@@ -66,12 +75,16 @@ class SharedPreferencesPlugin() : FlutterPlugin, SharedPreferencesAsyncApi {
6675

6776
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
6877
setUp(binding.applicationContext)
78+
registeredCount++
6979
legacyPlugin = LegacySharedPreferencesPlugin().also { it.onAttachedToEngine(binding) }
7080
}
7181

7282
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
73-
SharedPreferencesAsyncApiRegistrar().register(null, "data_store")
74-
backend?.tearDown()
83+
registeredCount--
84+
if (registeredCount == 0) {
85+
SharedPreferencesAsyncApiRegistrar().register(null, "data_store")
86+
SharedPreferencesAsyncApiRegistrar().register(null, "shared_preferences")
87+
}
7588
backend = null
7689
legacyPlugin?.onDetachedFromEngine(binding)
7790
legacyPlugin = null
@@ -295,10 +308,6 @@ class SharedPreferencesBackend(
295308
}
296309
}
297310

298-
fun tearDown() {
299-
SharedPreferencesAsyncApiRegistrar().register(null, "shared_preferences")
300-
}
301-
302311
private fun createSharedPreferences(options: SharedPreferencesPigeonOptions): SharedPreferences {
303312
return if (options.fileName == null) {
304313
PreferenceManager.getDefaultSharedPreferences(context)

‎packages/shared_preferences/shared_preferences_android/android/src/test/kotlin/io/flutter/plugins/sharedpreferences/LegacySharedPreferencesTest.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class LegacySharedPreferencesTest {
3939
plugin.onAttachedToEngine(flutterPluginBinding)
4040
}
4141

42+
@org.junit.After
43+
fun after() {
44+
LegacySharedPreferencesPlugin.clearRegisteredCount()
45+
}
46+
4247
@Test
4348
fun getAll() = runBlocking {
4449
assertEquals(0, plugin.getAll("", null).size)

‎packages/shared_preferences/shared_preferences_android/android/src/test/kotlin/io/flutter/plugins/sharedpreferences/MultipleEngineReproduceTest.kt‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class MultipleEngineReproduceTest {
3030
Mockito.`when`(flutterPluginBinding2.applicationContext).thenReturn(context)
3131
}
3232

33+
@org.junit.After
34+
fun after() {
35+
LegacySharedPreferencesPlugin.clearRegisteredCount()
36+
SharedPreferencesPlugin.clearRegisteredCount()
37+
}
38+
3339
@Test
3440
fun testDetachingSecondEngineClearsProcessWideMap() {
3541
// Start main engine and attach plugin
@@ -55,6 +61,15 @@ class MultipleEngineReproduceTest {
5561
"API should STILL be registered for main engine after background engine is detached",
5662
SharedPreferencesApiRegistrar()
5763
.getInstance("PigeonDefaultClassName32uh4ui3lh445uh4h3l2l455g4y34u"))
64+
65+
// Detach main engine
66+
plugin1.onDetachedFromEngine(flutterPluginBinding1)
67+
68+
// Now it should be cleared
69+
org.junit.Assert.assertNull(
70+
"API should be cleared when last engine detaches",
71+
SharedPreferencesApiRegistrar()
72+
.getInstance("PigeonDefaultClassName32uh4ui3lh445uh4h3l2l455g4y34u"))
5873
}
5974

6075
@Test
@@ -66,12 +81,10 @@ class MultipleEngineReproduceTest {
6681
// Verify main engine registered the APIs
6782
assertNotNull(
6883
"API should be registered for main engine (data_store)",
69-
SharedPreferencesAsyncApiRegistrar()
70-
.getInstance("data_store"))
84+
SharedPreferencesAsyncApiRegistrar().getInstance("data_store"))
7185
assertNotNull(
7286
"API should be registered for main engine (shared_preferences)",
73-
SharedPreferencesAsyncApiRegistrar()
74-
.getInstance("shared_preferences"))
87+
SharedPreferencesAsyncApiRegistrar().getInstance("shared_preferences"))
7588

7689
// Start background engine and attach second plugin instance
7790
val plugin2 = SharedPreferencesPlugin()
@@ -83,11 +96,20 @@ class MultipleEngineReproduceTest {
8396
// The main engine API registration should NOT have been cleared.
8497
assertNotNull(
8598
"API should STILL be registered for main engine after background engine is detached (data_store)",
86-
SharedPreferencesAsyncApiRegistrar()
87-
.getInstance("data_store"))
99+
SharedPreferencesAsyncApiRegistrar().getInstance("data_store"))
88100
assertNotNull(
89101
"API should STILL be registered for main engine after background engine is detached (shared_preferences)",
90-
SharedPreferencesAsyncApiRegistrar()
91-
.getInstance("shared_preferences"))
102+
SharedPreferencesAsyncApiRegistrar().getInstance("shared_preferences"))
103+
104+
// Detach main engine
105+
plugin1.onDetachedFromEngine(flutterPluginBinding1)
106+
107+
// Now it should be cleared
108+
org.junit.Assert.assertNull(
109+
"API should be cleared when last engine detaches (data_store)",
110+
SharedPreferencesAsyncApiRegistrar().getInstance("data_store"))
111+
org.junit.Assert.assertNull(
112+
"API should be cleared when last engine detaches (shared_preferences)",
113+
SharedPreferencesAsyncApiRegistrar().getInstance("shared_preferences"))
92114
}
93115
}

‎packages/shared_preferences/shared_preferences_android/android/src/test/kotlin/io/flutter/plugins/sharedpreferences/SharedPreferencesTest.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ internal class SharedPreferencesTest {
4444

4545
private val testList = JSON_LIST_PREFIX + listOf("foo", "bar").toString()
4646

47+
@org.junit.After
48+
fun after() {
49+
SharedPreferencesPlugin.clearRegisteredCount()
50+
LegacySharedPreferencesPlugin.clearRegisteredCount()
51+
}
52+
4753
private val dataStoreOptions = SharedPreferencesPigeonOptions(useDataStore = true)
4854
private val sharedPreferencesOptions = SharedPreferencesPigeonOptions(useDataStore = false)
4955
private val testContext: Context = ApplicationProvider.getApplicationContext()

0 commit comments

Comments
 (0)