Skip to content

Commit dee92d5

Browse files
committed
fix(remote-connect): isolate host stream readers and recover live sync
- Give every host stream reader its own optional subscription_id lease. A capable host echoes it and removes only that lease on unsubscribe, so a retired reader's late cleanup no longer cancels its replacement. Readers talking to older hosts skip cleanup and let the existing lease expire. - Retry a failed live read on a timer with capped backoff instead of sleeping inside the subscriber loop, also during older-page IO, and report recovery with a live ready event that does not complete an unrelated history page. - Tag desktop session events with a controller-local subscriptionId and mark retryable sync errors so a stale reader cannot fail its successor. - Install hint and reconnect listeners before the opening read in the shared TS and KMP readers so a final hint during that read is not lost. - HarmonyOS stream cleanup keeps the socket it opened, so closing after an account switch cannot touch the new account's connection.
1 parent 2cbaee5 commit dee92d5

26 files changed

Lines changed: 830 additions & 99 deletions

File tree

‎src/apps/desktop/src/api/remote_connect_api.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3947,6 +3947,11 @@ pub async fn account_load_older_session(request: UnsubscribeSessionRequest) -> R
39473947
pub async fn account_subscribe_session(request: SubscribeSessionRequest) -> Result<String, String> {
39483948
let generation = account_context_generation();
39493949
let (session, relay) = read_account_context_for_generation(generation).await?;
3950+
// Events can arrive before this command returns. Give every attempt an
3951+
// identity so a late error/page from a retired reader cannot poison its successor.
3952+
let id = uuid::Uuid::new_v4().to_string();
3953+
let event_subscription_id = id.clone();
3954+
let error_subscription_id = id.clone();
39503955
let source = request.target_device_id.clone();
39513956
let error_source = source.clone();
39523957
let error_session_id = request.session_id.clone();
@@ -3960,21 +3965,23 @@ pub async fn account_subscribe_session(request: SubscribeSessionRequest) -> Resu
39603965
if !account_context_is_current(generation) {
39613966
anyhow::bail!("account subscription retired");
39623967
}
3963-
emit_account_event(&event.event, tag_peer_event_source(event.payload, &source));
3968+
let mut payload = tag_peer_event_source(event.payload, &source);
3969+
payload["subscriptionId"] = event_subscription_id.clone().into();
3970+
emit_account_event(&event.event, payload);
39643971
Ok(())
39653972
}),
39663973
Arc::new(move |error| {
39673974
if account_context_is_current(generation) {
39683975
emit_account_event(
39693976
"account://session-sync-error",
3970-
serde_json::json!({"message":error,"sessionId":error_session_id,"targetDeviceId":error_source}),
3977+
serde_json::json!({"message":error,"sessionId":error_session_id,"targetDeviceId":error_source,
3978+
"subscriptionId":error_subscription_id,"retrying":true}),
39713979
);
39723980
}
39733981
}),
39743982
)
39753983
.await
39763984
.map_err(|e| e.to_string())?;
3977-
let id = uuid::Uuid::new_v4().to_string();
39783985
let mut subscriptions = SESSION_SUBSCRIPTIONS
39793986
.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
39803987
.lock()

‎src/apps/mobile/harmonyos/entry/src/main/ets/model/RemoteModels.ets‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export interface RemoteCommand {
188188
before?: number;
189189
epoch?: number;
190190
subscribe?: boolean;
191+
subscription_id?: string;
191192
}
192193

193194
export interface PairChallengeResponse {

‎src/apps/mobile/harmonyos/entry/src/main/ets/services/CloudAccountClient.ets‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,22 +514,29 @@ export class CloudAccountClient {
514514
subscribeSession(relayUrl: string, session: CloudAccountSession, targetId: string,
515515
streamId: string, callbacks: HostSessionCallbacks): HostSessionStream {
516516
const target = targetId.trim();
517+
const socket = this.connection(relayUrl, session.token);
518+
const subscriptionId = Encoding.randomId('stream');
519+
let scopedSubscription = false;
517520
const source: HostSessionSource = {
518521
target: target,
519-
read: (request: HostStreamReadRequest): Promise<HostStreamPage> => {
520-
const command: RemoteCommand = { cmd: 'read_stream', stream_id: streamId, subscribe: true };
522+
read: async (request: HostStreamReadRequest): Promise<HostStreamPage> => {
523+
if (this.realtime !== socket) throw new Error('Account changed');
524+
const command: RemoteCommand = { cmd: 'read_stream', stream_id: streamId, subscribe: true, subscription_id: subscriptionId };
521525
if (request.after !== undefined) command.after = request.after;
522526
if (request.before !== undefined) command.before = request.before;
523527
if (request.epoch !== undefined) command.epoch = request.epoch;
524-
return this.deviceRpc<HostStreamPage>(relayUrl, session, target, command, 30000);
528+
const page = await this.deviceRpc<HostStreamPage>(relayUrl, session, target, command, 30000);
529+
scopedSubscription = page.subscription_id === subscriptionId;
530+
return page;
525531
},
526532
unsubscribe: async (): Promise<void> => {
533+
if (this.realtime !== socket || !scopedSubscription) return;
527534
await this.deviceRpc<CommandStatusResponse>(relayUrl, session, target,
528-
{ cmd: 'unsubscribe_stream', stream_id: streamId }, 15000);
535+
{ cmd: 'unsubscribe_stream', stream_id: streamId, subscription_id: subscriptionId }, 15000);
529536
},
530537
onHint: (listener: (hint: HostStreamHint) => void): (() => void) =>
531538
this.observeStreamHints(relayUrl, session, target, listener),
532-
onReconnect: (listener: () => void): (() => void) => this.connection(relayUrl, session.token).onReconnect(listener)
539+
onReconnect: (listener: () => void): (() => void) => socket.onReconnect(listener)
533540
};
534541
return new HostSessionStream(streamId, source, callbacks);
535542
}

‎src/apps/mobile/harmonyos/entry/src/main/ets/services/HostSessionStream.ets‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface HostSessionEvent { session_id: string; event: string; payload:
2929
export interface HostStreamEventWire { seq: number; event: string; payload: Object; }
3030
/** `RemoteResponse::StreamPage`, flattened, as answered to `read_stream`. */
3131
export interface HostStreamPage extends CommandStatusResponse {
32+
subscription_id?: string;
3233
stream_id: string;
3334
epoch: number;
3435
events: HostStreamEventWire[];

‎src/apps/mobile/harmonyos/tools/tests/host-stream.test.cjs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,47 @@ test('lazy disk history keeps JS-safe backward cursors separate from live update
304304
assert.equal(c.errors.length, 0);
305305
} finally { stream.close(); }
306306
});
307+
308+
// Use the production account client factory as well as the stream reader: the
309+
// subscription acknowledgement is a wire capability, not a reader-only flag.
310+
test('account stream cleanup negotiates independent leases and leaves old hosts to expiry', async () => {
311+
const clientSource = fs.readFileSync(path.join(__dirname, '../../entry/src/main/ets/services/CloudAccountClient.ets'), 'utf8');
312+
const exported = {}; let nextId = 0;
313+
new Function('require', 'exports', ts.transpileModule(clientSource, { compilerOptions: {
314+
target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS,
315+
} }).outputText)(name => ({
316+
'./HostSessionStream': exportsObject,
317+
'./Encoding': { Encoding: { randomId: () => 'reader-' + ++nextId } },
318+
'./RemoteCrypto': { HarmonyRemoteCryptoCipher: class {} },
319+
}[name] || {}), exported);
320+
for (const scoped of [true, false]) {
321+
const client = new exported.CloudAccountClient(false), commands = [];
322+
const socket = { onReconnect: () => () => {} };
323+
client.realtime = socket;
324+
client.connection = () => { client.realtime = socket; return socket; };
325+
client.observeStreamHints = () => () => {};
326+
client.deviceRpc = async (_url, _session, _target, command) => {
327+
commands.push(command);
328+
return { resp: 'stream_page', stream_id: 's', epoch: 1, cursor: 0, oldest_seq: 1,
329+
events: [], has_more: false, truncated: false,
330+
...(scoped ? { subscription_id: command.subscription_id } : {}) };
331+
};
332+
const first = callbacks(), second = callbacks();
333+
const a = client.subscribeSession('relay', {}, 'desktop', 's', first.hooks);
334+
const b = client.subscribeSession('relay', {}, 'desktop', 's', second.hooks);
335+
try {
336+
await settle(() => first.caught === 1 && second.caught === 1);
337+
assert.notEqual(commands[0].subscription_id, commands[1].subscription_id);
338+
a.close(); await tick();
339+
const cleanup = commands.filter(command => command.cmd === 'unsubscribe_stream');
340+
assert.equal(cleanup.length, scoped ? 1 : 0);
341+
if (scoped) assert.equal(cleanup[0].subscription_id, commands[0].subscription_id);
342+
const newAccountSocket = { onReconnect: () => () => {} };
343+
client.realtime = newAccountSocket;
344+
const count = commands.length;
345+
b.close(); await tick();
346+
assert.equal(commands.length, count, 'retired cleanup cannot invoke on a new account');
347+
assert.equal(client.realtime, newAccountSocket, 'retired cleanup cannot reopen its old account');
348+
} finally { a.close(); b.close(); }
349+
}
350+
});

‎src/apps/mobile/shared/core-protocol/src/commonMain/kotlin/com/openbitfun/mobile/core/protocol/RemoteCommand.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,5 @@ public data class RemoteCommand(
106106
@SerialName("before") val before: Long? = null,
107107
@SerialName("epoch") val epoch: Long? = null,
108108
@SerialName("subscribe") val subscribe: Boolean? = null,
109+
@SerialName("subscription_id") val subscriptionId: String? = null,
109110
)

‎src/apps/mobile/shared/core-transport/src/commonMain/kotlin/com/openbitfun/mobile/core/transport/CloudAccountClient.kt‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import kotlinx.coroutines.withContext
66
import kotlinx.coroutines.CompletableDeferred
77
import kotlinx.coroutines.channels.Channel
88
import kotlinx.coroutines.flow.filter
9+
import kotlinx.coroutines.flow.flow
10+
import kotlinx.coroutines.flow.emitAll
911
import kotlinx.coroutines.flow.map
1012
import kotlinx.coroutines.flow.drop
1113
import kotlinx.coroutines.flow.onCompletion
@@ -341,12 +343,15 @@ public class CloudAccountClient internal constructor(
341343
* encrypted device RPC. The relay forwards ciphertext only, and nothing is
342344
* written on this device: the transcript lives exactly as long as the flow.
343345
*/
346+
@OptIn(ExperimentalUuidApi::class)
344347
public suspend fun subscribeSession(
345348
relayUrl: String, session: CloudAccountSession, targetDeviceId: String, sessionId: String,
346349
onError: (Throwable) -> Unit, onCaughtUp: () -> Unit,
347-
): kotlinx.coroutines.flow.Flow<JsonObject> {
350+
): kotlinx.coroutines.flow.Flow<JsonObject> = flow {
348351
val socket = connection(relayUrl, session.token)
349352
val target = targetDeviceId.trim()
353+
val subscriptionId = Uuid.random().toString()
354+
var scopedSubscription = false
350355
val historyKey = target + ":" + sessionId
351356
val historyRequests = Channel<CompletableDeferred<Unit>>(Channel.RENDEZVOUS)
352357
historyReaders[historyKey] = historyRequests
@@ -356,8 +361,9 @@ public class CloudAccountClient internal constructor(
356361
check(realtime.value?.socket === socket) { "Account changed" }
357362
val startedAt = TimeSource.Monotonic.markNow()
358363
val page = deviceRpc(relayUrl, session, target,
359-
RemoteCommand(cmd = "read_stream", streamId = sessionId, after = after, before = before, epoch = epoch, subscribe = true),
364+
RemoteCommand(cmd = "read_stream", streamId = sessionId, after = after, before = before, epoch = epoch, subscribe = true, subscriptionId = subscriptionId),
360365
StreamPageWire.serializer(), RELAY_DEFAULT_TIMEOUT_MS)
366+
scopedSubscription = page.subscriptionId == subscriptionId
361367
val elapsedMs = startedAt.elapsedNow().inWholeMilliseconds
362368
// The opening page (`after == null`) is the one the user waits for,
363369
// and a slow page is worth naming wherever it happens; a page per
@@ -368,8 +374,8 @@ public class CloudAccountClient internal constructor(
368374
return page
369375
}
370376
override suspend fun unsubscribe() {
371-
if (realtime.value?.socket !== socket) return
372-
deviceRpc(relayUrl, session, target, RemoteCommand(cmd = "unsubscribe_stream", streamId = sessionId),
377+
if (realtime.value?.socket !== socket || !scopedSubscription) return
378+
deviceRpc(relayUrl, session, target, RemoteCommand(cmd = "unsubscribe_stream", streamId = sessionId, subscriptionId = subscriptionId),
373379
CommandStatusResponse.serializer(), RELAY_DEFAULT_TIMEOUT_MS)
374380
}
375381
}
@@ -379,7 +385,7 @@ public class CloudAccountClient internal constructor(
379385
// the gap between the two counts is what the receiving device spends.
380386
val openedAt = TimeSource.Monotonic.markNow()
381387
var recordsSeen = 0
382-
return hostStream(sessionId, target, hints, merge(socket.connections.drop(1), foregroundResumes), reads,
388+
emitAll(hostStream(sessionId, target, hints, merge(socket.connections.drop(1), foregroundResumes), reads,
383389
olderRequests = historyRequests, onError = { error ->
384390
log.warn("host stream read failed stream=${sessionId.take(24)} type=${error::class.simpleName} failure=${(error as? CloudAccountException)?.failure} message=${error.message}")
385391
onError(error)
@@ -390,7 +396,7 @@ public class CloudAccountClient internal constructor(
390396
log.info("host stream ended stream=${sessionId.take(24)} events=$recordsSeen cause=${cause?.let { it::class.simpleName } ?: "none"}")
391397
if (historyReaders[historyKey] === historyRequests) historyReaders.remove(historyKey)
392398
historyRequests.close()
393-
}
399+
})
394400
}
395401

396402
/**

‎src/apps/mobile/shared/core-transport/src/commonMain/kotlin/com/openbitfun/mobile/core/transport/HostStream.kt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.openbitfun.mobile.core.protocol.CommandStatus
44
import com.openbitfun.mobile.core.protocol.isError
55
import kotlinx.coroutines.CancellationException
66
import kotlinx.coroutines.CompletableDeferred
7+
import kotlinx.coroutines.CoroutineStart
78
import kotlinx.coroutines.NonCancellable
89
import kotlinx.coroutines.channels.Channel
910
import kotlinx.coroutines.currentCoroutineContext
@@ -87,6 +88,7 @@ internal data class StreamPageWire(
8788
val cursor: Long = -1,
8889
@SerialName("oldest_seq") val oldestSeq: Long = -1,
8990
val truncated: Boolean = false,
91+
@SerialName("subscription_id") val subscriptionId: String? = null,
9092
) : CommandStatus
9193

9294
/** A decrypted `host-stream-changed` device event. */
@@ -314,6 +316,14 @@ internal fun hostStream(
314316
// Requests the lane took off [wakes] and has not served yet. One sweep can
315317
// hand it several, and every one of them is owed an answer.
316318
val waiting = ArrayDeque<CompletableDeferred<Unit>>()
319+
// Start collection before the opening RPC. SharedFlow does not replay a
320+
// final hint that arrives without a collector while that page is in flight.
321+
launch(start = CoroutineStart.UNDISPATCHED) {
322+
hints.collect { hint -> if (reader.hintIsNew(hint, target)) wakes.trySend(Wake.Dirty(false)) }
323+
}
324+
launch(start = CoroutineStart.UNDISPATCHED) {
325+
reconnects.collect { wakes.trySend(Wake.Dirty(true)) }
326+
}
317327
// Set once a page was read with `subscribe`; before that there is nothing
318328
// on the host to release.
319329
var subscribed = false
@@ -336,8 +346,6 @@ internal fun hostStream(
336346
awaitConsumption()
337347
onCaughtUp()
338348

339-
launch { hints.collect { hint -> if (reader.hintIsNew(hint, target)) wakes.send(Wake.Dirty(false)) } }
340-
launch { reconnects.collect { wakes.send(Wake.Dirty(true)) } }
341349
launch { while (currentCoroutineContext().isActive) { delay(keepaliveMs); wakes.send(Wake.Dirty(false)) } }
342350
launch { for (request in olderRequests) wakes.send(Wake.Older(request)) }
343351

‎src/apps/mobile/shared/core-transport/src/commonTest/kotlin/com/openbitfun/mobile/core/transport/CloudAccountClientTest.kt‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import kotlinx.coroutines.test.advanceUntilIdle
3030
import kotlinx.coroutines.test.runCurrent
3131
import kotlinx.coroutines.test.runTest
3232
import kotlinx.serialization.json.JsonElement
33+
import kotlinx.serialization.json.JsonPrimitive
34+
import kotlin.test.assertNotNull
3335
import kotlinx.serialization.json.JsonObject
3436
import kotlinx.serialization.json.buildJsonObject
3537
import kotlinx.serialization.json.put
@@ -368,6 +370,7 @@ class CloudAccountClientTest {
368370
*/
369371
@Test
370372
fun hostStreamRecoversRepeatsOnlyTheReadAndUnsubscribesOnClose() = runTest {
373+
for (supportsScoped in listOf(true, false)) {
371374
val commands = mutableListOf<RemoteCommand>()
372375
val master = ByteArray(32)
373376
val peerSecret = ByteArray(32) { 11 }
@@ -387,7 +390,11 @@ class CloudAccountClientTest {
387390
"unsubscribe_stream" -> """{"resp":"stream_unsubscribed","stream_id":"session"}"""
388391
else -> error("unexpected ${command.cmd}")
389392
}
390-
val encrypted = CloudAccountCipher.encrypt(response.encodeToByteArray(), key, nonce)
393+
val answer = if (supportsScoped && command.cmd == "read_stream") {
394+
JsonObject(RelayJson.parseToJsonElement(response).jsonObject +
395+
("subscription_id" to JsonPrimitive(command.subscriptionId))).toString()
396+
} else response
397+
val encrypted = CloudAccountCipher.encrypt(answer.encodeToByteArray(), key, nonce)
391398
RelayJson.encodeToJsonElement(EncryptedPayload.serializer(),
392399
EncryptedPayload(Base64.Default.encode(encrypted), Base64.Default.encode(nonce)))
393400
}
@@ -399,11 +406,58 @@ class CloudAccountClientTest {
399406
"desktop-1", "session", {}, {})
400407
.onEach { received += it.getValue("event").jsonPrimitive.content }
401408
.first { it.getValue("event").jsonPrimitive.content == STREAM_EVENT_READY }
402-
assertEquals(listOf("read_stream", "read_stream", "unsubscribe_stream"), commands.map { it.cmd })
403-
assertEquals(RemoteCommand(cmd = "read_stream", streamId = "session", subscribe = true), commands[1])
409+
assertEquals(if (supportsScoped) listOf("read_stream", "read_stream", "unsubscribe_stream")
410+
else listOf("read_stream", "read_stream"), commands.map { it.cmd })
411+
assertNotNull(commands.first().subscriptionId)
412+
assertTrue(commands.all { it.subscriptionId == commands.first().subscriptionId })
413+
assertEquals(RemoteCommand(cmd = "read_stream", streamId = "session", subscribe = true), commands[1].copy(subscriptionId = null))
404414
assertEquals(listOf("session-record", STREAM_EVENT_READY), received)
405415
assertEquals("session", commands.last().streamId)
406416
assertEquals(1, keyReads, "the peer key is fetched once and reused for every read")
417+
}
418+
}
419+
420+
@Test
421+
fun collectingTheSameStreamTwiceOwnsIndependentSubscriptions() = runTest {
422+
val master = ByteArray(32)
423+
val peerSecret = ByteArray(32) { 11 }
424+
val key = DeviceIdentity.messageKey(peerSecret, DeviceIdentity.publicKey(master))
425+
val commands = mutableListOf<RemoteCommand>()
426+
val client = rpcClient(DeviceIdentity.publicKey(peerSecret)) { _, params ->
427+
val envelope = RelayJson.decodeFromJsonElement(EncryptedPayload.serializer(), params)
428+
val plain = CloudAccountCipher.decrypt(Base64.Default.decode(envelope.encryptedData), key,
429+
Base64.Default.decode(envelope.nonce)).decodeToString()
430+
val command = RelayJson.decodeFromString(RemoteCommand.serializer(), plain)
431+
commands += command
432+
val response = if (command.cmd == "read_stream")
433+
"""{"resp":"stream_page","stream_id":"session","subscription_id":"${command.subscriptionId}","epoch":1,"events":[],"has_more":false,"cursor":0,"oldest_seq":1,"truncated":false}"""
434+
else """{"resp":"stream_unsubscribed","stream_id":"session"}"""
435+
val nonce = ByteArray(12) { commands.size.toByte() }
436+
val encrypted = CloudAccountCipher.encrypt(response.encodeToByteArray(), key, nonce)
437+
RelayJson.encodeToJsonElement(EncryptedPayload.serializer(),
438+
EncryptedPayload(Base64.Default.encode(encrypted), Base64.Default.encode(nonce)))
439+
}
440+
val stream = client.subscribeSession("http://192.168.1.2:9700",
441+
CloudAccountSession("token-1", "user-1", master), "desktop-1", "session", {}, {})
442+
val ready = Channel<Unit>(Channel.UNLIMITED)
443+
val first = launch { stream.collect { if (it["event"]?.jsonPrimitive?.content == STREAM_EVENT_READY) ready.send(Unit) } }
444+
ready.receive()
445+
val second = launch { stream.collect { if (it["event"]?.jsonPrimitive?.content == STREAM_EVENT_READY) ready.send(Unit) } }
446+
try {
447+
ready.receive()
448+
val ids = commands.filter { it.cmd == "read_stream" }.map { it.subscriptionId }
449+
assertEquals(2, ids.size)
450+
assertNotNull(ids[0])
451+
assertNotNull(ids[1])
452+
assertTrue(ids[0] != ids[1], "each collector owns its lease and history lane")
453+
first.cancelAndJoin()
454+
assertEquals(ids[0], commands.last().subscriptionId)
455+
assertEquals("unsubscribe_stream", commands.last().cmd)
456+
assertTrue(second.isActive)
457+
} finally {
458+
first.cancelAndJoin()
459+
second.cancelAndJoin()
460+
}
407461
}
408462

409463
@Test

0 commit comments

Comments
 (0)