Skip to content

Commit 1385073

Browse files
committed
Address review feedback on registrar thread safety and engine keys
- Restore locking around Native Interop registrations, since lookups from background isolates run on worker threads. Registration is still expected on the main thread; this doesn't make the APIs a thread-safe contract. - Treat a zero engine id from FlutterPluginBinding / FlutterPluginRegistrar as unknown and fall back to the messenger key. - Only learn a Dart engine affinity when the isolate has no engine id. - Document that affinity is per generated file and can bind lazily used APIs to another engine.
1 parent 3ffe355 commit 1385073

47 files changed

Lines changed: 40826 additions & 82483 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/pigeon/README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ Pigeon supports two distinct models for communication between Dart and native co
115115
> plugin bindings and registrars, only on Flutter versions that expose it). See
116116
> [Multi-Engine Routing](./native_interop_guide.md#multi-engine-routing) and
117117
> [flutter/flutter#195877](https://github.com/flutter/flutter/issues/195877). Known limitations:
118-
> - Until engine identifiers are available to plugins on stable Flutter, lookups may resolve another
119-
> engine's registration when engines are created back-to-back before their Dart code starts.
118+
> - Until engine identifiers are available to plugins on stable Flutter, each generated Pigeon file
119+
> binds to the engine of its first resolved `@HostApi` handle. If that first lookup happens while
120+
> another engine's registration is the most recent one, for example when engines are created
121+
> back-to-back before their Dart code starts, or when an API is first used lazily while a
122+
> secondary engine is attached, that file's APIs may be bound to the other engine's instance and
123+
> throw `unregistered-instance` after that engine detaches.
120124
> - Background isolates (`Isolate.spawn` / `Isolate.run`) do not have a `PlatformDispatcher.engineId`,
121125
> so in multi-engine apps their lookups may resolve another engine's registration.
122126
> - APIs that hold engine-specific state (such as an Android `Activity`, a `TextureRegistry`, or a

‎packages/pigeon/example/native_interop_app/android/app/src/main/kotlin/dev/flutter/pigeonnativeinteropapp/NativeInteropExample.g.kt‎

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private object NativeInteropExamplePigeonEngineKeys {
7272
private var nextSyntheticKey: Long = -1L
7373

7474
/** Returns the key for the engine that [binding] belongs to. */
75+
@Synchronized
7576
fun keyOf(binding: FlutterPlugin.FlutterPluginBinding): Long {
7677
return bindingKeys.getOrPut(binding) { computeKey(binding) }
7778
}
@@ -80,7 +81,12 @@ private object NativeInteropExamplePigeonEngineKeys {
8081
val method = getEngineIdMethod
8182
if (method != null) {
8283
try {
83-
return method.invoke(binding) as Long
84+
// Real engine ids are never 0, but an engine without one, or a test
85+
// double, reports 0, which must not be shared between engines.
86+
val engineId = method.invoke(binding) as Long
87+
if (engineId != 0L) {
88+
return engineId
89+
}
8490
} catch (e: Exception) {
8591
// Fall back to a messenger-based key.
8692
}
@@ -89,6 +95,7 @@ private object NativeInteropExamplePigeonEngineKeys {
8995
}
9096

9197
/** Returns the key for [engine]. */
98+
@Synchronized
9299
fun keyOf(engine: FlutterEngine): Long {
93100
val key = engine.engineId
94101
// Record the exact key so later lookups through a binding for this engine
@@ -97,6 +104,7 @@ private object NativeInteropExamplePigeonEngineKeys {
97104
return key
98105
}
99106

107+
@Synchronized
100108
private fun keyOfMessenger(messenger: Any): Long {
101109
return messengerKeys.getOrPut(messenger) { nextSyntheticKey-- }
102110
}
@@ -114,7 +122,7 @@ interface NativeInteropExampleApi {
114122
@Keep
115123
class NativeInteropExampleApiRegistrar private constructor(private val engineKey: Long) :
116124
NativeInteropExampleApi {
117-
private var api: NativeInteropExampleApi? = null
125+
@Volatile private var api: NativeInteropExampleApi? = null
118126

119127
constructor() : this(NativeInteropExamplePigeonEngineKeys.NO_KEY)
120128

@@ -187,35 +195,39 @@ class NativeInteropExampleApiRegistrar private constructor(private val engineKey
187195
}
188196

189197
private fun registerWithKey(api: NativeInteropExampleApi, engineKey: Long, name: String) {
190-
val list = NativeInteropExampleApiInstances.getOrPut(name) { mutableListOf() }
191-
val index = list.indexOfFirst { it.api === api && it.engineKey == engineKey }
192-
if (index >= 0) {
193-
list.add(list.removeAt(index))
194-
} else {
195-
val wrapper = NativeInteropExampleApiRegistrar(engineKey)
196-
wrapper.api = api
197-
list.add(wrapper)
198+
kotlin.synchronized(NativeInteropExampleApiInstances) {
199+
val list = NativeInteropExampleApiInstances.getOrPut(name) { mutableListOf() }
200+
val index = list.indexOfFirst { it.api === api && it.engineKey == engineKey }
201+
if (index >= 0) {
202+
list.add(list.removeAt(index))
203+
} else {
204+
val wrapper = NativeInteropExampleApiRegistrar(engineKey)
205+
wrapper.api = api
206+
list.add(wrapper)
207+
}
198208
}
199209
}
200210

201211
private fun unregisterWithKey(api: NativeInteropExampleApi, engineKey: Long, name: String) {
202-
val list = NativeInteropExampleApiInstances[name] ?: return
203-
val removeKey =
204-
if (list.any { it.api === api && it.engineKey == engineKey }) {
205-
engineKey
206-
} else {
207-
list.filter { it.api === api }.map { it.engineKey }.distinct().singleOrNull() ?: return
212+
kotlin.synchronized(NativeInteropExampleApiInstances) {
213+
val list = NativeInteropExampleApiInstances[name] ?: return
214+
val removeKey =
215+
if (list.any { it.api === api && it.engineKey == engineKey }) {
216+
engineKey
217+
} else {
218+
list.filter { it.api === api }.map { it.engineKey }.distinct().singleOrNull() ?: return
219+
}
220+
val iterator = list.iterator()
221+
while (iterator.hasNext()) {
222+
val entry = iterator.next()
223+
if (entry.api === api && entry.engineKey == removeKey) {
224+
entry.api = null
225+
iterator.remove()
208226
}
209-
val iterator = list.iterator()
210-
while (iterator.hasNext()) {
211-
val entry = iterator.next()
212-
if (entry.api === api && entry.engineKey == removeKey) {
213-
entry.api = null
214-
iterator.remove()
215227
}
216-
}
217-
if (list.isEmpty()) {
218-
NativeInteropExampleApiInstances.remove(name)
228+
if (list.isEmpty()) {
229+
NativeInteropExampleApiInstances.remove(name)
230+
}
219231
}
220232
}
221233

@@ -235,16 +247,18 @@ class NativeInteropExampleApiRegistrar private constructor(private val engineKey
235247
affinityKey: Long,
236248
name: String = defaultInstanceName
237249
): NativeInteropExampleApiRegistrar? {
238-
val list = NativeInteropExampleApiInstances[name] ?: return null
239-
val exact =
240-
if (engineId != NativeInteropExamplePigeonEngineKeys.NO_KEY)
241-
list.lastOrNull { it.engineKey == engineId }
242-
else null
243-
val affine =
244-
if (affinityKey != NativeInteropExamplePigeonEngineKeys.NO_KEY)
245-
list.lastOrNull { it.engineKey == affinityKey }
246-
else null
247-
return exact ?: affine ?: list.lastOrNull()
250+
return kotlin.synchronized(NativeInteropExampleApiInstances) {
251+
val list = NativeInteropExampleApiInstances[name] ?: return null
252+
val exact =
253+
if (engineId != NativeInteropExamplePigeonEngineKeys.NO_KEY)
254+
list.lastOrNull { it.engineKey == engineId }
255+
else null
256+
val affine =
257+
if (affinityKey != NativeInteropExamplePigeonEngineKeys.NO_KEY)
258+
list.lastOrNull { it.engineKey == affinityKey }
259+
else null
260+
exact ?: affine ?: list.lastOrNull()
261+
}
248262
}
249263

250264
/**

‎packages/pigeon/example/native_interop_app/ios/Runner/NativeInteropExample.g.swift‎

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
208208
private enum PigeonEngineKeys {
209209
/// The sentinel for an unknown engine.
210210
static let noKey: Int64 = Int64.min
211+
/// Guards the key caches. Recursive because `key(for:)` calls `key(messenger:exactKey:)`.
212+
private static let lock = NSRecursiveLock()
211213
private static let syntheticKeys = NSMapTable<AnyObject, NSNumber>.weakToStrongObjects()
212214
private static var nextSyntheticKey: Int64 = -1
213215

@@ -217,6 +219,8 @@ private enum PigeonEngineKeys {
217219
if let exactKey = exactKey {
218220
return exactKey
219221
}
222+
lock.lock()
223+
defer { lock.unlock() }
220224
if let existing = syntheticKeys.object(forKey: messenger) {
221225
return existing.int64Value
222226
}
@@ -234,13 +238,18 @@ private enum PigeonEngineKeys {
234238
/// Returns the key for the engine that owns `registrar`, or `noKey` if it can't be determined.
235239
static func key(for registrar: FlutterPluginRegistrar) -> Int64 {
236240
let registrarObject = registrar as AnyObject
241+
lock.lock()
242+
defer { lock.unlock() }
237243
if let cached = registrarKeys.object(forKey: registrarObject) {
238244
return cached.int64Value
239245
}
240246
var resolved = noKey
241247
if registrarObject.responds(to: NSSelectorFromString("engineIdentifier")),
242-
let identifier = registrarObject.value(forKey: "engineIdentifier") as? NSNumber
248+
let identifier = registrarObject.value(forKey: "engineIdentifier") as? NSNumber,
249+
identifier.int64Value != 0
243250
{
251+
// Real engine identifiers are never 0. 0 comes from a registrar whose engine is already
252+
// deallocating, or from a test double, and must not be shared between engines.
244253
resolved = identifier.int64Value
245254
} else if let messenger = registrarObject.value(forKey: "messenger") as AnyObject? {
246255
// Read through KVC because the messenger can be nil while the engine is deallocating,
@@ -380,6 +389,8 @@ private class _PigeonFfiCodec {
380389

381390
class NativeInteropExampleApiInstanceTracker {
382391
static var instancesOfNativeInteropExampleApi = [String: [NativeInteropExampleApiSetup]]()
392+
/// Guards the registrations, which Native Interop calls can read from any thread.
393+
static let lock = NSLock()
383394
}
384395
/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
385396
protocol NativeInteropExampleApi: AnyObject {
@@ -391,7 +402,10 @@ protocol NativeInteropExampleApi: AnyObject {
391402
private var api: NativeInteropExampleApi?
392403
override init() {}
393404
private func resolveApi() throws -> NativeInteropExampleApi {
394-
guard let currentApi = api else {
405+
NativeInteropExampleApiInstanceTracker.lock.lock()
406+
let registeredApi = api
407+
NativeInteropExampleApiInstanceTracker.lock.unlock()
408+
guard let currentApi = registeredApi else {
395409
throw PigeonError(
396410
code: "unregistered-instance",
397411
message: "NativeInteropExampleApi has not been registered.",
@@ -400,7 +414,7 @@ protocol NativeInteropExampleApi: AnyObject {
400414
}
401415
return currentApi
402416
}
403-
/// The engine key this wrapper was registered with.
417+
/// The engine key this wrapper was registered with. Set before the wrapper is published.
404418
private var engineKey: Int64 = Int64.min
405419

406420
#if canImport(Flutter) || canImport(FlutterMacOS)
@@ -436,6 +450,8 @@ protocol NativeInteropExampleApi: AnyObject {
436450
#endif
437451

438452
fileprivate static func register(api: NativeInteropExampleApi, engineKey: Int64, name: String) {
453+
NativeInteropExampleApiInstanceTracker.lock.lock()
454+
defer { NativeInteropExampleApiInstanceTracker.lock.unlock() }
439455
var list = NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi[name] ?? []
440456
if let index = list.firstIndex(where: { $0.api === api && $0.engineKey == engineKey }) {
441457
list.append(list.remove(at: index))
@@ -449,36 +465,47 @@ protocol NativeInteropExampleApi: AnyObject {
449465
}
450466

451467
fileprivate static func unregister(api: NativeInteropExampleApi, engineKey: Int64, name: String) {
452-
guard var list = NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi[name]
453-
else {
454-
return
455-
}
456-
let removeKey: Int64
457-
if list.contains(where: { $0.api === api && $0.engineKey == engineKey }) {
458-
removeKey = engineKey
459-
} else {
460-
let keys = Set(list.filter { $0.api === api }.map { $0.engineKey })
461-
guard keys.count == 1, let onlyKey = keys.first else {
468+
// Removed entries are kept alive until after unlocking so their deinit cannot run while the
469+
// non-recursive lock is held.
470+
var displaced: [Any] = []
471+
do {
472+
NativeInteropExampleApiInstanceTracker.lock.lock()
473+
defer { NativeInteropExampleApiInstanceTracker.lock.unlock() }
474+
guard
475+
var list = NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi[name]
476+
else {
462477
return
463478
}
464-
removeKey = onlyKey
465-
}
466-
list.removeAll { entry in
467-
guard let existing = entry.api else {
479+
let removeKey: Int64
480+
if list.contains(where: { $0.api === api && $0.engineKey == engineKey }) {
481+
removeKey = engineKey
482+
} else {
483+
let keys = Set(list.filter { $0.api === api }.map { $0.engineKey })
484+
guard keys.count == 1, let onlyKey = keys.first else {
485+
return
486+
}
487+
removeKey = onlyKey
488+
}
489+
list.removeAll { entry in
490+
guard let existing = entry.api else {
491+
return false
492+
}
493+
if existing === api && entry.engineKey == removeKey {
494+
displaced.append(existing)
495+
displaced.append(entry)
496+
entry.api = nil
497+
return true
498+
}
468499
return false
469500
}
470-
if existing === api && entry.engineKey == removeKey {
471-
entry.api = nil
472-
return true
501+
if list.isEmpty {
502+
NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi.removeValue(
503+
forKey: name)
504+
} else {
505+
NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi[name] = list
473506
}
474-
return false
475-
}
476-
if list.isEmpty {
477-
NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi.removeValue(
478-
forKey: name)
479-
} else {
480-
NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi[name] = list
481507
}
508+
withExtendedLifetime(displaced) {}
482509
}
483510

484511
/// Returns the instance generated Dart code should use for `name` (or the default
@@ -490,6 +517,8 @@ protocol NativeInteropExampleApi: AnyObject {
490517
engineId: Int64, affinityKey: Int64,
491518
name: String = NativeInteropExamplePigeonInternal.defaultInstanceName
492519
) -> NativeInteropExampleApiSetup? {
520+
NativeInteropExampleApiInstanceTracker.lock.lock()
521+
defer { NativeInteropExampleApiInstanceTracker.lock.unlock() }
493522
guard let list = NativeInteropExampleApiInstanceTracker.instancesOfNativeInteropExampleApi[name]
494523
else {
495524
return nil

‎packages/pigeon/example/native_interop_app/lib/src/native_interop_example.native_interop.g.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class _NativeInteropExampleApiForNativeInterop implements NativeInteropExampleAp
436436
if (link == null) {
437437
_throwNoInstanceError(channelName);
438438
}
439-
if (link != null && _pigeonEngineAffinity == null) {
439+
if (link != null && engineId == null && _pigeonEngineAffinity == null) {
440440
final int linkEngineKey = jni_bridge.NativeInteropExampleApiRegistrar.pigeonEngineKeyOf(
441441
link,
442442
);
@@ -456,7 +456,7 @@ class _NativeInteropExampleApiForNativeInterop implements NativeInteropExampleAp
456456
if (link == null) {
457457
_throwNoInstanceError(channelName);
458458
}
459-
if (link != null && _pigeonEngineAffinity == null) {
459+
if (link != null && engineId == null && _pigeonEngineAffinity == null) {
460460
final int linkEngineKey = ffi_bridge.NativeInteropExampleApiSetup.pigeonEngineKeyOf(link);
461461
if (linkEngineKey != _pigeonNoEngineKey) {
462462
_pigeonEngineAffinity = linkEngineKey;

‎packages/pigeon/lib/src/dart/dart_generator.dart‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,17 +2015,24 @@ $dartApiName? ${classMemberNamePrefix}getInstance_${api.name}({
20152015
// Native registrations are matched to this isolate's engine. When the
20162016
// engine id is unavailable, the engine of the first resolved instance is
20172017
// remembered so later lookups stay on the same engine, and Flutter API
2018-
// registrations made before it was known are updated with it.
2018+
// registrations made before it was known are updated with it. Isolates
2019+
// that know their engine id never learn an affinity, because a lookup
2020+
// that falls back to another engine's registration doesn't make that
2021+
// engine theirs.
20192022
void writeRecordAffinity(String engineKeyOfCall) {
2020-
indent.writeScoped('if (link != null && _pigeonEngineAffinity == null) {', '}', () {
2021-
indent.writeln('final int linkEngineKey = $engineKeyOfCall;');
2022-
indent.writeScoped('if (linkEngineKey != _pigeonNoEngineKey) {', '}', () {
2023-
indent.writeln('_pigeonEngineAffinity = linkEngineKey;');
2024-
if (root.apis.any((Api api) => api is AstFlutterApi)) {
2025-
indent.writeln('_pigeonUpdateFlutterApiEngineAffinity();');
2026-
}
2027-
});
2028-
});
2023+
indent.writeScoped(
2024+
'if (link != null && engineId == null && _pigeonEngineAffinity == null) {',
2025+
'}',
2026+
() {
2027+
indent.writeln('final int linkEngineKey = $engineKeyOfCall;');
2028+
indent.writeScoped('if (linkEngineKey != _pigeonNoEngineKey) {', '}', () {
2029+
indent.writeln('_pigeonEngineAffinity = linkEngineKey;');
2030+
if (root.apis.any((Api api) => api is AstFlutterApi)) {
2031+
indent.writeln('_pigeonUpdateFlutterApiEngineAffinity();');
2032+
}
2033+
});
2034+
},
2035+
);
20292036
}
20302037

20312038
indent.writeln(

0 commit comments

Comments
 (0)