From e28f79d043a2d7424e5df6ff8d6929a3c1539ed4 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 12 Feb 2026 18:13:12 +0100 Subject: [PATCH] pairing: Add retry logic to get_pairing_data to fix flaky test The test_pairing_session test was flaky due to a race condition in the pairing protocol. The NewDeviceClient generates the device_id locally and sends it via the QR code channel before the PairDevice gRPC request reaches the server. When the AttestationDeviceClient immediately calls GetPairingData with that device_id, the session might not exist yet. This adds retry logic to get_pairing_data that retries for up to 10 seconds with 100ms intervals, handling the inherent race condition in the pairing protocol. --- .../src/pairing/attestation_device.rs | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/libs/gl-client/src/pairing/attestation_device.rs b/libs/gl-client/src/pairing/attestation_device.rs index ffd4ddd90..ab19b6a35 100644 --- a/libs/gl-client/src/pairing/attestation_device.rs +++ b/libs/gl-client/src/pairing/attestation_device.rs @@ -68,15 +68,32 @@ impl Client Client { pub async fn get_pairing_data(&self, device_id: &str) -> Result { - Ok(self - .inner - .0 - .clone() - .get_pairing_data(GetPairingDataRequest { - device_id: device_id.to_string(), - }) - .await? - .into_inner()) + use tokio::time::{sleep, Duration, Instant}; + + // Retry for up to 10 seconds to handle the race condition where the + // attestation device receives the QR code before the new device's + // PairDevice request has been processed by the server. + let deadline = Instant::now() + Duration::from_secs(10); + + loop { + let result = self + .inner + .0 + .clone() + .get_pairing_data(GetPairingDataRequest { + device_id: device_id.to_string(), + }) + .await; + + match result { + Ok(response) => return Ok(response.into_inner()), + Err(_) if Instant::now() < deadline => { + sleep(Duration::from_millis(100)).await; + continue; + } + Err(e) => return Err(e.into()), + } + } } pub async fn approve_pairing(