Skip to content

Commit fa621c6

Browse files
authored
Fix a race condition when setting up ServiceStore message channel (#4826)
This seems to be the cause of the flakiness in `AndroidAllocatorTest` and `AndroidHandleManagerTest` (#4781)
1 parent 78bf4f0 commit fa621c6

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

java/arcs/sdk/android/storage/ServiceStore.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,15 @@ class ServiceStore<Data : CrdtData, Op : CrdtOperation, ConsumerData>(
142142
val service = connection.connectAsync().await()
143143

144144
val messageChannel = ParcelableProxyMessageChannel(coroutineContext)
145-
serviceCallbackToken = withContext(coroutineContext) {
146-
service.registerCallback(messageChannel)
147-
}
148145

146+
// Open subscription before attaching callback to make sure that we capture all messages
147+
val subscription = messageChannel.openSubscription()
149148
scope.launch {
150-
messageChannel.openSubscription().consumeEach { handleMessageAndResultFromService(it) }
149+
subscription.consumeEach { handleMessageAndResultFromService(it) }
150+
}
151+
152+
serviceCallbackToken = withContext(coroutineContext) {
153+
service.registerCallback(messageChannel)
151154
}
152155

153156
this.serviceConnection = connection

0 commit comments

Comments
 (0)