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

Fix: don’t send a verification cancel request if the session is closed #1716

Merged
merged 4 commits into from
Feb 15, 2023

Conversation

nimau
Copy link
Contributor

@nimau nimau commented Feb 13, 2023

This PR fixes:

  • a crash in DEBUG when the app tries to send a cancel verification request while the session is closed.
  • 2 retain cycles preventing MXCrypto from being properly released

@nimau nimau force-pushed the nimau/PSB-249_crash_cancel_key_verification branch from abce798 to 1306872 Compare February 13, 2023 09:38
@nimau nimau requested a review from Anderas February 13, 2023 09:38
@codecov
Copy link

codecov bot commented Feb 13, 2023

Codecov Report

Base: 17.52% // Head: 37.38% // Increases project coverage by +19.85% 🎉

Coverage data is based on head (d306d8c) compared to base (7d59096).
Patch coverage: 73.91% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##           develop    #1716       +/-   ##
============================================
+ Coverage    17.52%   37.38%   +19.85%     
============================================
  Files          612      612               
  Lines        95983    96133      +150     
  Branches     40286    41556     +1270     
============================================
+ Hits         16823    35939    +19116     
+ Misses       78649    59146    -19503     
- Partials       511     1048      +537     
Impacted Files Coverage Δ
...SDK/Crypto/Verification/MXKeyVerificationManager.m 53.98% <68.42%> (+53.98%) ⬆️
.../Crypto/KeyBackup/Engine/MXNativeKeyBackupEngine.m 66.21% <100.00%> (+66.21%) ⬆️
MatrixSDK/Crypto/MXCrypto.m 64.19% <100.00%> (+62.12%) ⬆️
...o/Algorithms/RoomEvent/MXRoomEventDecryption.swift 53.27% <0.00%> (-2.07%) ⬇️
...ypto/Verification/MXKeyVerificationManagerV2.swift 20.73% <0.00%> (-0.56%) ⬇️
MatrixSDK/Background/MXBackgroundSyncService.swift 0.00% <0.00%> (ø)
MatrixSDK/Utils/Media/MXMediaManager.m 1.88% <0.00%> (+0.47%) ⬆️
MatrixSDK/Contrib/Swift/MXResponse.swift 43.58% <0.00%> (+0.73%) ⬆️
MatrixSDK/Contrib/Swift/MXRestClient.swift 2.22% <0.00%> (+0.74%) ⬆️
...gations/LocationSharing/MXBeaconAggregations.swift 4.44% <0.00%> (+1.26%) ⬆️
... and 193 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -707,6 +707,15 @@ - (void)cancelVerificationRequest:(id<MXKeyVerificationRequest>)request
{
[self cancelTransaction:transaction code:cancelCode success:success failure:failure];
}
// If the session is closed, don't try to send a request
else if (self.crypto.mxSession.state == MXSessionStateClosed)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this actually fix the problem? What if the first if branch is evaluated first, we never get to check session being closed. Maybe better to put this at the very start of the cancelVerificationRequest?

I would also be curious to know why cancelVerificationRequest is called in the first place when session is already closed, and perhaps the isClosed check really belongs in the caller of the cancelVerificationRequest. This is because if "cancel" can cause troubles, than so could the other methods in this manager, such as cancelTransaction or sendToOtherInRequest. Do you have reliable steps to reproduce this crash?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @Anderas, thanks for the feedback.
I moved the check at the very start of the cancelVerificationRequest
cancelVerificationRequest is called by MXKeyVerificationManager when the timeout (5 minutes by default) is fired.
So, if a user logs into the app, uses the recovery key to validate the session, then logs out or clears the cache (anything that invalidates the session), the app will try to send a verification cancel request even if the session is closed.
In DEBUG, this will crash because of the assertion in sendToOtherInRequest, but in practice, it may prevent some completion handler from being called.

Copy link
Contributor

@Anderas Anderas Feb 14, 2023

Choose a reason for hiding this comment

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

The thing is that when the session is closed (say via the close method), this will also close session.crypto, which will in turn close crypto.keyVerificationManager, even though by nillifying it. In other words when a session is closed no other objects should continue to function, and in the case of MXKeyVerificationManager it should cancel its timeout once closed. The reason this does not work at the moment is that even if crypto nillifies the manager, some other object keeps it in memory (not necessarily as a memory leak).

Whilst your solution does solve this particular problem, I fear this is merely one of many symptoms of the manager continuing its work after a session is closed. I would instead propose this solution:

  • add new close method in the MXLegacyKeyVerificationManager header file
  • implement the close method by clearing any timeouts that we have scheduled
  • call the method just before self->_keyVerificationManager = nil is called from MXLegacyCrypto close

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Anderas, Ok, thanks. The verification manager was retained by its timers. So I removed my previous "fix", change the way we create the timers to prevent them to retain the manager, and finally, properly invalidate the timer on dealloc. It should be better and safer now.

@@ -101,7 +101,9 @@ - (BOOL)enableBackupWithKeyBackupVersion:(MXKeyBackupVersion *)keyBackupVersion
self.crypto.store.backupVersion = keyBackupVersion.version;
Class algorithmClass = AlgorithmClassesByName[keyBackupVersion.algorithm];
// store the desired backup algorithm
MXWeakify(self);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice find on the retain cycles

@nimau nimau marked this pull request as ready for review February 14, 2023 13:50
@nimau nimau requested a review from Anderas February 14, 2023 13:50
Copy link
Contributor

@Anderas Anderas left a comment

Choose a reason for hiding this comment

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

Thats great, thanks for the change

@nimau nimau merged commit 0be841e into develop Feb 15, 2023
@nimau nimau deleted the nimau/PSB-249_crash_cancel_key_verification branch February 15, 2023 07:19
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.

2 participants