Skip to content

Commit

Permalink
ffi: duplicate the tokio runtime hack for Encryption :melting:
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjbvr committed Jun 4, 2024
1 parent 1bbe2c4 commit 493ab46
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions bindings/matrix-sdk-ffi/src/encryption.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
use std::sync::Arc;
use std::{mem::ManuallyDrop, sync::Arc};

use futures_util::StreamExt;
use matrix_sdk::{
encryption,
encryption::{backups, recovery},
};
use thiserror::Error;
use tracing::info;
use zeroize::Zeroize;

use super::RUNTIME;
use crate::{error::ClientError, task_handle::TaskHandle};

#[derive(uniffi::Object)]
pub struct Encryption {
inner: matrix_sdk::encryption::Encryption,
inner: ManuallyDrop<matrix_sdk::encryption::Encryption>,
}

impl Drop for Encryption {
fn drop(&mut self) {
// This holds a reference to the `Client`, so it must duplicate the hack on the
// FFI's `Client` data structure, since it may be the last reference to
// a `Client`.
//
// Dropping the inner OlmMachine must happen within a tokio context
// because deadpool drops sqlite connections in the DB pool on tokio's
// blocking threadpool to avoid blocking async worker threads.
let _guard = RUNTIME.enter();
// SAFETY: self.inner is never used again, which is the only requirement
// for ManuallyDrop::drop to be used safely.
unsafe {
info!("Dropping an Encryption");
ManuallyDrop::drop(&mut self.inner);
}
}
}

impl From<matrix_sdk::encryption::Encryption> for Encryption {
fn from(value: matrix_sdk::encryption::Encryption) -> Self {
Self { inner: value }
Self { inner: ManuallyDrop::new(value) }
}
}

Expand Down

0 comments on commit 493ab46

Please sign in to comment.