Modern Apps git

← Modern-Apps.git

wearlink: stale generations can no longer displace a newer pending item

Commit
9699062ee6c461edff32ab9d5e842db1da14912b
Author
Vayun Mathur <[email protected]>
When
2026-12-01 09:54 UTC (5 days ago)
Parents
dc3a6fb
DataReassembler rejected only chunks older than the last
applied generation, never older than the pending one. Two
putData calls for one path racing on IO threads interleave:
gen 8 chunk 0 pends; stale gen 7 chunks (not < applied,
since nothing applied yet) replaced the pending gen-8 state
— discarding its chunk 0 — completed, verified, and were
delivered; gen 8's remaining chunks then pended forever
incomplete. The stale document was delivered after the newer
one started, and the newer was silently dropped, against the
class contract ('partial or stale generations change
nothing'). The strict < also let a replay of the
already-applied generation reassemble and deliver twice.

Staleness is now judged against both generations: a chunk
stale against the applied generation (<=) or older than the
pending one changes nothing; newer-than-pending still
replaces, equal-generation still accumulates. Regression
test covers the displacement sequence and the applied replay
(failed pre-fix at the stale-delivery assertion). Handshake,
frame bounds, stream resume and ack correlation were audited
and cleared. Verified: :library:wearlink:testDebugUnitTest
9 tests pass; lint passes.

Diff

library/wearlink/src/main/java/com/vayunmathur/library/wearlink/DataItems.kt
diff --git a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/DataItems.kt b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/DataItems.ktindex 6719a9c017..84545c7caf 100644--- a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/DataItems.kt+++ b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/DataItems.kt@@ -105,9 +105,10 @@ class DataReassembler {         val chunk = DataChunk.decodeBody(frame.body) ?: return null         if (chunk.count <= 0 || chunk.index !in 0 until chunk.count) return null         if (chunk.totalSize < 0 || chunk.totalSize > WearLinkProtocol.MAX_DATA_ITEM_BYTES) return null-        val last = appliedGeneration[frame.path] ?: Long.MIN_VALUE-        if (chunk.generation < last) return null+        val last = appliedGeneration[frame.path]+        if (last != null && chunk.generation <= last) return null         val current = pending[frame.path]+        if (current != null && chunk.generation < current.generation) return null         val state = if (current == null || current.generation != chunk.generation) {             Pending(chunk.generation, chunk.count, chunk.totalSize, chunk.sha256)                 .also { pending[frame.path] = it }
library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearLinkProtocolTest.kt
diff --git a/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearLinkProtocolTest.kt b/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearLinkProtocolTest.ktindex d7a4816479..5b05fa4435 100644--- a/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearLinkProtocolTest.kt+++ b/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearLinkProtocolTest.kt@@ -111,6 +111,25 @@ class WearLinkProtocolTest {         assertNull(badCompleted)     } +    @Test+    fun `data reassembler ignores stale generation that would displace newer pending`() {+        val newer = ByteArray(40_000) { (it % 251).toByte() }+        val stale = ByteArray(40_000) { ((it + 7) % 251).toByte() }+        val newerFrames = DataItemCodec.chunk("/calendar/snapshot", 8L, newer)+        val staleFrames = DataItemCodec.chunk("/calendar/snapshot", 7L, stale)+        val reassembler = DataReassembler()+        // Newer generation starts first; the stale generation must change nothing.+        assertNull(reassembler.onFrame(newerFrames[0]))+        for (frame in staleFrames) assertNull(reassembler.onFrame(frame))+        var completed: ByteArray? = null+        for (frame in newerFrames.drop(1)) reassembler.onFrame(frame)?.let { completed = it }+        assertNotNull(completed)+        assertTrue(newer.contentEquals(completed))+        // Replaying the already-applied generation must not deliver it a second time.+        val replay = DataItemCodec.chunk("/calendar/snapshot", 8L, newer)+        for (frame in replay) assertNull(reassembler.onFrame(frame))+    }+     @Test     fun `stream receiver verifies hash and sender resumes from ack offset`() {         val data = ByteArray(50_000) { (it % 241).toByte() }