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

Sign out crash - Realm configuration mismatch #4480

Merged
merged 4 commits into from
Nov 17, 2021

Conversation

ouchadam
Copy link
Contributor

@ouchadam ouchadam commented Nov 16, 2021

Fixes an intermittent crash on sign out caused by a race condition where we attempt to access an already closed session causing it to be recreated which in turn causes realm to crash because the new session is slightly different.

the race condition boils down to...

sessionComponents.remove(sessionId)

runOnUiThread {
  if (sessionParamsStore[sessionId] != null) {
    val session = sessionComponents.getOrCreate(sessionId).session()
    onStopped(session)
  }
} 

sessionParamsStore.delete(sessionId)
  • The solution is to avoid using the SessionManager within the SessionListeners and instead pass the current session directly (to avoid creating a new one), which we were already doing in most cases
BEFORE AFTER
before-signout after-signout-crash

… always querying the session manager

- fixes the close sesion flow causing the session to be recreated
…read which holds the handler lazily provides itself meaning the session exists and is in sync
@@ -174,8 +174,8 @@ internal class DefaultSession @Inject constructor(
lifecycleObservers.forEach {
it.onSessionStarted(this)
}
sessionListeners.dispatch { _, listener ->
listener.onSessionStarted(this)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as we were already avoiding the session parameter in the callback it felt like the SessionListeners didn't actually need to hold the session itself

this also means that the close flow can safely use the session instance without worrying about it dissapearing from the SessionManager which would cause onSessionStopped to not be called

@@ -50,7 +51,7 @@ private const val GET_GROUP_DATA_WORKER = "GET_GROUP_DATA_WORKER"

internal class SyncResponseHandler @Inject constructor(
@SessionDatabase private val monarchy: Monarchy,
@SessionId private val sessionId: String,
private val session: Session,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure about this change, technically this works as the SyncThread uses a Dagger Provider so the instance is always correct but it feels a little overkill~

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we should not inject the Session in the SDK (I have checked and this is never done, AFAICS)
Is is required to update this file to fix the issue?

Copy link
Contributor Author

@ouchadam ouchadam Nov 16, 2021

Choose a reason for hiding this comment

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

we need a session instance in order to call

sessionListeners.dispatch(session) { session, listener ->
    listener.onNewInvitedRoom(session, roomId)
}

https://github.com/vector-im/element-android/pull/4480/files#diff-68049ad71866a8668f6f8c2a2d181ebe0eb25cbfab0637e44bc78cdf25e8d0a1L163

I could pass the SessionManager to the SyncResponseHandler like the SessionListeners was previously doing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

405cda7 it's an inlined version of what the SessionListeners was doing

Copy link
Member

Choose a reason for hiding this comment

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

I think we could also replace onNewInvitedRoom by listening to the Flow instead

Copy link
Member

Choose a reason for hiding this comment

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

@ganfra yes, please do that in another PR :p

@@ -42,18 +38,19 @@ internal class SessionListeners @Inject constructor(
}
}

fun dispatch(block: (Session, Session.Listener) -> Unit) {
fun dispatch(session: Session, block: (Session, Session.Listener) -> Unit) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the fix, instead of relying on sessionManager.getSessionComponent(sessionId)?.session() which may create a new session if one doesn't exist, the callers provide a known session instance

@bmarty
Copy link
Member

bmarty commented Nov 16, 2021

This black icon is strange, maybe due to the video recording/compression?

image

@ouchadam
Copy link
Contributor Author

This black icon is strange, maybe due to the video recording/compression?

image

I'm not actually sure, I thought it was meant to be like this 😅

going frame by frame I can see it transition from white to black

2021-11-16T15:34:13,389376883+00:00 2021-11-16T15:34:22,919342615+00:00 2021-11-16T15:34:33,926603071+00:00 2021-11-16T15:34:45,238805858+00:00

@github-actions
Copy link

github-actions bot commented Nov 16, 2021

Unit Test Results

  66 files  ±0    66 suites  ±0   56s ⏱️ -11s
135 tests ±0  135 ✔️ ±0  0 💤 ±0  0 ±0 
418 runs  ±0  418 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 405cda7. ± Comparison against base commit 10ec6e7.

♻️ This comment has been updated with latest results.

Copy link
Member

@bmarty bmarty 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 update. I will squash the commit to cleanup the git history.

@bmarty bmarty merged commit 10a460b into develop Nov 17, 2021
@bmarty bmarty deleted the feature/adm/sign-out-close-crash branch November 17, 2021 09:39
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.

3 participants