Skip to content

Commit 62e1f94

Browse files
committed
chore: answer review
1 parent 9efe9bc commit 62e1f94

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

core/threshold/src/execution/online/preprocessing/redis/mod.rs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -357,26 +357,44 @@ impl<R: Ring> Drop for RedisPreprocessing<R> {
357357
// to it in rust.
358358
// NOTE: Ideally we'd Zeroize it but afaict Redis doesn't propose such functionality
359359
fn drop(&mut self) {
360-
// Cleanup logic here
361-
let mut con = match self.client.get_connection() {
362-
Ok(con) => {
363-
tracing::info!("Connection to redis for cleanup");
364-
con
365-
}
366-
Err(_) => {
367-
tracing::warn!("Failed to connect to redis to cleanup");
368-
return;
369-
}
370-
};
371-
372-
for randomness_type in CorrelatedRandomnessType::iter() {
373-
let key = compute_key(self.key_prefix.clone(), randomness_type);
360+
let client = self.client.clone();
361+
let keys = CorrelatedRandomnessType::iter()
362+
.map(|randoness_type| compute_key(self.key_prefix.clone(), randoness_type))
363+
.collect::<Vec<_>>();
364+
365+
// Defer cleanup to a tokio blocking thread
366+
// to avoid blocking the potential current tokio async worker
367+
// Note: This thus assumes we are inside a tokio runtime
368+
tokio::task::spawn_blocking(move || {
369+
// Cleanup logic here
370+
let mut con = match client.get_connection() {
371+
Ok(con) => {
372+
tracing::info!("Connection to redis for cleanup");
373+
con
374+
}
375+
Err(_) => {
376+
tracing::warn!("Failed to connect to redis to cleanup");
377+
return;
378+
}
379+
};
380+
381+
for key in keys {
382+
// Log the size to know whether we actually deleted something
383+
match con.llen::<&str, usize>(&key) {
384+
Ok(len) => {
385+
tracing::info!("About to delete {len} elements for key {key}")
386+
}
387+
Err(e) => {
388+
tracing::warn!("Failed to get length of key {key} from Redis: {}", e);
389+
}
390+
}
374391

375-
// Remove the key from Redis
376-
let _: RedisResult<()> = con.del(key).inspect_err(|e| {
377-
tracing::warn!("Failed to delete key from Redis: {}", e);
378-
});
379-
}
392+
// In any case remove the key from Redis
393+
let _: RedisResult<()> = con.del(&key).inspect_err(|e| {
394+
tracing::warn!("Failed to delete key {key} from Redis: {}", e);
395+
});
396+
}
397+
});
380398
}
381399
}
382400

core/threshold/tests/integration_redis.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ macro_rules! test_triples {
4040
paste! {
4141

4242

43-
#[test]
44-
fn [<test_redis_preprocessing $z:lower>]() {
43+
#[tokio::test]
44+
async fn [<test_redis_preprocessing $z:lower>]() {
4545
let test_key_prefix = format!("test_redis_preprocessing_{}",stringify!($z));
4646
let redis_conf = RedisConf::default();
4747
let mut redis_factory = create_redis_factory(test_key_prefix.clone(), &redis_conf);
@@ -81,8 +81,8 @@ macro_rules! test_triples {
8181
};
8282
}
8383

84-
#[test]
85-
fn test_store_fetch_100_triples() {
84+
#[tokio::test]
85+
async fn test_store_fetch_100_triples() {
8686
let test_key_prefix = "test_store_fetch_100_triples".to_string();
8787
let redis_conf = RedisConf::default();
8888
let mut redis_factory = create_redis_factory(test_key_prefix.clone(), &redis_conf);
@@ -118,8 +118,8 @@ fn test_store_fetch_100_triples() {
118118
assert_eq!(triples, fetched_triples);
119119
}
120120

121-
#[test]
122-
fn test_store_fetch_100_randoms() {
121+
#[tokio::test]
122+
async fn test_store_fetch_100_randoms() {
123123
let test_key_prefix = "test_store_fetch_100_randoms".to_string();
124124
let redis_conf = RedisConf::default();
125125
let mut redis_factory = create_redis_factory(test_key_prefix.clone(), &redis_conf);
@@ -141,8 +141,8 @@ fn test_store_fetch_100_randoms() {
141141
assert_eq!(randoms, fetched_shares);
142142
}
143143

144-
#[test]
145-
fn test_store_fetch_100_bits() {
144+
#[tokio::test]
145+
async fn test_store_fetch_100_bits() {
146146
let test_key_prefix = "test_store_fetch_100_bits".to_string();
147147
let redis_conf = RedisConf::default();
148148
let mut redis_factory = create_redis_factory(test_key_prefix.clone(), &redis_conf);
@@ -164,8 +164,8 @@ fn test_store_fetch_100_bits() {
164164
assert_eq!(bits, fetched_bits);
165165
}
166166

167-
#[test]
168-
fn test_fetch_more_than_stored() {
167+
#[tokio::test]
168+
async fn test_fetch_more_than_stored() {
169169
let store_count = 100;
170170
let fetch_count = 101;
171171

@@ -191,8 +191,8 @@ fn test_fetch_more_than_stored() {
191191
.contains("Pop length error."));
192192
}
193193

194-
#[test]
195-
fn test_cleanup_on_drop() {
194+
#[tokio::test]
195+
async fn test_cleanup_on_drop() {
196196
let test_key_prefix = "test_cleanup_on_drop".to_string();
197197
let redis_conf = RedisConf::default();
198198
let mut redis_factory = create_redis_factory(test_key_prefix.clone(), &redis_conf);
@@ -217,6 +217,10 @@ fn test_cleanup_on_drop() {
217217
// Drop the preprocessing instance
218218
drop(bit_redis_preprocessing);
219219

220+
// Sleep for a while because drop of the Redis preproc is
221+
// sent to a tokio blocking thread so drop might return early
222+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
223+
220224
// Check that the shares have been cleaned up
221225
assert_eq!(bit_redis_preprocessing_bis.bits_len(), 0);
222226

@@ -360,8 +364,8 @@ fn test_dkg_orchestrator_params8_small_no_sns() {
360364
}
361365

362366
#[cfg(feature = "testing")]
363-
#[test]
364-
fn test_cast_fail_memory_bit_dec_preprocessing() {
367+
#[tokio::test]
368+
async fn test_cast_fail_memory_bit_dec_preprocessing() {
365369
use threshold_fhe::{
366370
algebra::galois_rings::degree_4::ResiduePolyF4Z64,
367371
execution::online::preprocessing::{

0 commit comments

Comments
 (0)