Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: ensure the SessionStore is cleared when regenerating the OlmMachine #3338

Merged
merged 5 commits into from
Apr 19, 2024

Conversation

kegsay
Copy link
Member

@kegsay kegsay commented Apr 18, 2024

This fixes #3110

@kegsay kegsay requested a review from a team as a code owner April 18, 2024 09:11
@kegsay kegsay requested review from andybalaam and removed request for a team April 18, 2024 09:11
@andybalaam andybalaam requested review from poljar and removed request for andybalaam April 18, 2024 09:13
@andybalaam
Copy link
Contributor

@poljar I approve this code but can't review as I am a co-author

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this looks good, but surely we can add some tests for this.

Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

The code looks good. How hard is it to add a test to ensure the caches are cleared as expected?

crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-sqlite/src/crypto_store.rs Outdated Show resolved Hide resolved
Copy link

codecov bot commented Apr 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.60%. Comparing base (f7329c7) to head (381c02d).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3338      +/-   ##
==========================================
+ Coverage   83.58%   83.60%   +0.01%     
==========================================
  Files         240      240              
  Lines       24841    24849       +8     
==========================================
+ Hits        20764    20774      +10     
+ Misses       4077     4075       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kegsay
Copy link
Member Author

kegsay commented Apr 18, 2024

The current test for this is https://github.com/matrix-org/complement-crypto/pull/52/files#diff-fd8835211753c17a95b8c01292df184e7a252c2544f0535baf79ea9db1d56d7cR243 which is what we used to verify the fix. We can probably write a unit test though for clear_caches?

@andybalaam
Copy link
Contributor

I am working on a couple of tests.

Co-authored-by: Ivan Enderlin <ivan@mnt.io>
Signed-off-by: Kegan Dougal <7190048+kegsay@users.noreply.github.com>
@andybalaam
Copy link
Contributor

Testing the clear_caches() implementation on the sqlite and indexeddb stores is difficult since the cache is not observable via external interfaces, and we need a real DB to run this code. I don't want to add to the CryptoStore interface just to test this.

I've added a test that regenerate_olm() calls clear_caches().

A real test will bring up two store instances against the same backing store and check that they keep up to date with each other.

I HEREBY PINKIE PROMISE TO WRITE A GENUINE REGRESSION TEST FOR #3110 OR FAIL TRYING.

But that will be a later PR.

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this is a test, so you got me there...

Sorry but can we have a test that puts a session into the store and cache and then when we call the method we check that the cache is empty?

As we agreed, porting the complement test isn't necessary but please at least something that doesn't just check that a method gets called.

@poljar
Copy link
Contributor

poljar commented Apr 18, 2024

Testing the clear_caches() implementation on the sqlite and indexeddb stores is difficult since the cache is not observable via external interfaces, and we need a real DB to run this code. I don't want to add to the CryptoStore interface just to test this.

To help you out, you don't need a new interface, the field is available inside the store specific tests:

diff --git a/crates/matrix-sdk-sqlite/src/crypto_store.rs b/crates/matrix-sdk-sqlite/src/crypto_store.rs
index 612165662..80a5fec40 100644
--- a/crates/matrix-sdk-sqlite/src/crypto_store.rs
+++ b/crates/matrix-sdk-sqlite/src/crypto_store.rs
@@ -1304,7 +1304,11 @@ mod tests {
 
 #[cfg(test)]
 mod encrypted_tests {
-    use matrix_sdk_crypto::{cryptostore_integration_tests, cryptostore_integration_tests_time};
+    use matrix_sdk_crypto::{
+        cryptostore_integration_tests, cryptostore_integration_tests_time,
+        store::{Changes, CryptoStore as _, PendingChanges},
+    };
+    use matrix_sdk_test::async_test;
     use once_cell::sync::Lazy;
     use tempfile::{tempdir, TempDir};
 
@@ -1321,6 +1325,26 @@ mod encrypted_tests {
             .expect("Can't create a passphrase protected store")
     }
 
+    #[async_test]
+    async fn cache_cleared() {
+        let store = get_store("cache_cleaerd", None).await;
+        // Given we created a session and saved it in the store
+        let (account, session) = cryptostore_integration_tests::get_account_and_session().await;
+        let sender_key = session.sender_key.to_base64();
+
+        store
+            .save_pending_changes(PendingChanges { account: Some(account.deep_clone()) })
+            .await
+            .expect("Can't save account");
+
+        let changes = Changes { sessions: vec![session.clone()], ..Default::default() };
+        store.save_changes(changes).await.unwrap();
+
+        store.session_cache.get(&sender_key).expect("We should have a session");
+
+        // TODO: Clear the cache
+    }
+
     cryptostore_integration_tests!();
     cryptostore_integration_tests_time!();
 }

@andybalaam
Copy link
Contributor

Thanks @poljar , working on it.

@andybalaam andybalaam requested a review from poljar April 18, 2024 12:47
Copy link
Member

@BillCarsonFr BillCarsonFr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the other test a bit pointless, but ok if we manage to port the complement test, then we can remove it.

@andybalaam andybalaam merged commit a3e6a07 into main Apr 19, 2024
35 checks passed
@andybalaam andybalaam deleted the kegan/drop-store branch April 19, 2024 08:05
@@ -1695,6 +1697,36 @@ mod tests {
client.get_room(room_id).expect("Just-created room not found!")
}

#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_regerating_olm_clears_store_caches() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this test is really useful? This is using a fake crypto store impl, and just checking that a method has been called, which is better tested in the other test. How do y'all feel about removing this test instead and all the bootstrapping that it requires, which is a bit noisey to me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agreed as much in my final review, it is a bit useful since it's the only place which checks that the method gets called.

Porting the complement crypto test is still in the pinkie promise so that might get rid of this noisy code.

@bnjbvr
Copy link
Member

bnjbvr commented Apr 19, 2024

E_TOO_SLOW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Olm sessions can wedge when using multiple Clients (e.g with NSE process)
6 participants