Skip to content

Commit 3d69d9a

Browse files
committed
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.
1 parent 97ae772 commit 3d69d9a

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

libs/gl-client/src/pairing/attestation_device.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,32 @@ impl<C: TlsConfigProvider + RuneProvider + NodeIdProvider> Client<Unconnected, C
6868

6969
impl<C: TlsConfigProvider + RuneProvider + NodeIdProvider> Client<Connected, C> {
7070
pub async fn get_pairing_data(&self, device_id: &str) -> Result<GetPairingDataResponse> {
71-
Ok(self
72-
.inner
73-
.0
74-
.clone()
75-
.get_pairing_data(GetPairingDataRequest {
76-
device_id: device_id.to_string(),
77-
})
78-
.await?
79-
.into_inner())
71+
use tokio::time::{sleep, Duration, Instant};
72+
73+
// Retry for up to 10 seconds to handle the race condition where the
74+
// attestation device receives the QR code before the new device's
75+
// PairDevice request has been processed by the server.
76+
let deadline = Instant::now() + Duration::from_secs(10);
77+
78+
loop {
79+
let result = self
80+
.inner
81+
.0
82+
.clone()
83+
.get_pairing_data(GetPairingDataRequest {
84+
device_id: device_id.to_string(),
85+
})
86+
.await;
87+
88+
match result {
89+
Ok(response) => return Ok(response.into_inner()),
90+
Err(_) if Instant::now() < deadline => {
91+
sleep(Duration::from_millis(100)).await;
92+
continue;
93+
}
94+
Err(e) => return Err(e.into()),
95+
}
96+
}
8097
}
8198

8299
pub async fn approve_pairing(

0 commit comments

Comments
 (0)