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

Drop / reconnect database connections when app becomes backgrounded/active #467

Merged
merged 7 commits into from
Aug 9, 2024

Conversation

nmalzieu
Copy link
Collaborator

@nmalzieu nmalzieu commented Aug 9, 2024

Since we have so many issues with db locks, I suspect we have an issue calling the db disconnect at the right moment.

My thinking is that there are issues either in the Converse code or the XMTP React Native code, that blocks the bridge for too long and we don't manage to actually dicsonnect db before the app is suspended

By adding that code directly in Swift we bypass the bridge and should be able to prevent some of these crashes

Possible improvements:

  • add a boolean in the config to enable / disable that feature, because it's an issue only if the db is stored in the shared container. However I don't really see how disabling that feature would bring any value
  • implement this in the iOS SDK rather than the RN Module. However swift developers have access to the Client instance and can just call the dropDb method themselves!

Also some weirdness: why is client.reconnectLocalDatabase async but client.dropLocalDatabaseConnection sync?

@nmalzieu nmalzieu requested a review from a team as a code owner August 9, 2024 08:53
ios/XMTPModule.swift Outdated Show resolved Hide resolved
@nplasterer
Copy link
Contributor

@nmalzieu you'll need a semantic commit to make this create a release. https://github.com/semantic-release/semantic-release

nplasterer
nplasterer previously approved these changes Aug 9, 2024
Copy link
Contributor

@nplasterer nplasterer left a comment

Choose a reason for hiding this comment

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

Part of me thinks this change should be done on the beta branch. But I'm not sure it matters that much.

func reconnectAllLocalDatabaseConnections() async throws {
for (_, client) in clients {
// Call the method on each client
try await client.reconnectLocalDatabase()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a blocking call. Hopefully this won't create too much lag when people reopen the app if they have a lot of clients. I really think we should consider setting a limit of how many clients a user can be logged in with. For MVP I really think we should just allow 1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a blocking call but in an actor, isn't everything called in an async manner? I'm not used to actors

Comment on lines +1501 to +1512
OnAppBecomesActive {
Task {
try await clientsManager.reconnectAllLocalDatabaseConnections()
}
}


OnAppEntersBackground {
Task {
try await clientsManager.dropAllLocalDatabaseConnections()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need to be a little more carful about this since these methods will error for any client that does not pass a V3Enabled flag. So they should only be called if the client is v3enabled.

@nplasterer nplasterer dismissed their stale review August 9, 2024 15:40

Possibly breaks v2 only clients

Comment on lines 1483 to 1488

OnAppBecomesActive {
Task {
try await clientsManager.reconnectAllLocalDatabaseConnections()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a duplicate of line 1504

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sorry about that must have been the conflict when rebasing. Fixing.

@cameronvoell cameronvoell self-requested a review August 9, 2024 16:44
Copy link
Contributor

@cameronvoell cameronvoell left a comment

Choose a reason for hiding this comment

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

Verified in example app that V2 clients seem to be working fine through multiple background and foreground actions.

@cameronvoell cameronvoell merged commit daea446 into main Aug 9, 2024
5 of 6 checks passed
@cameronvoell cameronvoell deleted the noe/ios-db-disconnect branch August 9, 2024 16:51
Copy link

github-actions bot commented Aug 9, 2024

🎉 This PR is included in version 2.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

4 participants