ffi: provide manual cancellation mechanism for identity/cross-signing reset handles

- works around swift issue where nil-ing the handle is not enough for it to get cancelled
This commit is contained in:
Stefan Ceriu
2024-08-09 09:48:12 +03:00
committed by Stefan Ceriu
parent 70f46d48af
commit 89ce8870a9
3 changed files with 40 additions and 5 deletions
@@ -434,6 +434,10 @@ impl IdentityResetHandle {
self.inner.reset(None).await.map_err(|e| ClientError::Generic { msg: e.to_string() })
}
}
pub async fn cancel(&self) {
self.inner.cancel().await;
}
}
#[derive(uniffi::Enum)]
+31 -5
View File
@@ -57,7 +57,7 @@ use ruma::{
},
DeviceId, OwnedDeviceId, OwnedUserId, TransactionId, UserId,
};
use tokio::sync::RwLockReadGuard;
use tokio::sync::{Mutex, RwLockReadGuard};
use tracing::{debug, error, instrument, trace, warn};
use url::Url;
use vodozemac::Curve25519PublicKey;
@@ -246,9 +246,26 @@ pub struct CrossSigningResetHandle {
upload_request: UploadSigningKeysRequest,
signatures_request: UploadSignaturesRequest,
auth_type: CrossSigningResetAuthType,
is_cancelled: Mutex<bool>,
}
impl CrossSigningResetHandle {
/// Set up a new `CrossSigningResetHandle`.
pub fn new(
client: Client,
upload_request: UploadSigningKeysRequest,
signatures_request: UploadSignaturesRequest,
auth_type: CrossSigningResetAuthType,
) -> Self {
Self {
client,
upload_request,
signatures_request,
auth_type,
is_cancelled: Mutex::new(false),
}
}
/// Get the [`CrossSigningResetAuthType`] this cross-signing reset process
/// is using.
pub fn auth_type(&self) -> &CrossSigningResetAuthType {
@@ -264,6 +281,10 @@ impl CrossSigningResetHandle {
// TODO: Do we want to put a limit on this infinite loop? 🤷
while let Err(e) = self.client.send(upload_request.clone(), None).await {
if *self.is_cancelled.lock().await {
return Ok(());
}
if e.client_api_error_kind() != Some(&ErrorKind::Unrecognized) {
return Err(e.into());
}
@@ -273,6 +294,11 @@ impl CrossSigningResetHandle {
Ok(())
}
/// Cancel the ongoing identity reset process
pub async fn cancel(&self) {
*self.is_cancelled.lock().await = true;
}
}
/// information about the additional authentication that is required before the
@@ -1231,12 +1257,12 @@ impl Encryption {
if let Some(auth_type) = CrossSigningResetAuthType::new(&self.client, &error).await? {
let client = self.client.clone();
Ok(Some(CrossSigningResetHandle {
Ok(Some(CrossSigningResetHandle::new(
client,
upload_request: upload_signing_keys_req,
signatures_request: upload_signatures_req,
upload_signing_keys_req,
upload_signatures_req,
auth_type,
}))
)))
} else {
Err(error.into())
}
@@ -669,4 +669,9 @@ impl IdentityResetHandle {
Ok(())
}
/// Cancel the ongoing identity reset process
pub async fn cancel(&self) {
self.cross_signing_reset_handle.cancel().await;
}
}