I update Xamarin App with 10.5.0. After my first connection with my test smartphone all client with 10.3 or 10.4 have Realm Exception with InnerExecption: Invalid Session.
Unique solution: delete app and reinstall !!!
Why? What appens?
Please Help me, I have 3k user and I don’t want to ask all to remove App
The “Invalid Session” error means that the user is not authenticated - it is thrown by the server and is unrelated to the sync session, despite the similar naming. Can you check out the server logs for any indication as to what might be the reason behind this.
Ending session with error: integrating changesets failed: could not complete upload integration as this connection no longer owns the file ident; no action is needed, as the client has already established a new connection to the sync server to complete its upload (ProtocolErrorCode=101)
Per the docs, anonymous users’ refresh token is valid for 60 days. When the token expires, the anonymous user is logged out and the user on the device is invalidated.
The issue you’re seeing is likely these users that logged in 2 months ago are expiring right now and they can no longer synchronize. This is what the “Invalid Session” error is caused by and it only affects users that logged in 2 months ago (but will keep happening in a rolling 2-month window). The reason why this breaks your app though is due to this bug - instead of logging the user out on token expiration, we still return it from app.CurrentUser or app.LoginAsync(Credentials.Anonymous()). I’ll ping the team to prioritize fixing it.
The reason why deleting the app works is that this deletes the Realm that stores the user metadata - it’s unrelated to the Realm you’re interfacing with. This deletes the invalid user and you can login with a new user that will be valid for another 60 days. You can achieve the same by subscribing to the Session.Error event and looking at the SessionException.ErrorCode and logging out the user if the error code is BadAuthentication:
Session.Error += (s, e) =>
{
if (e is SessionException sessionEx && sessionEx.ErrorCode == ErrorCode.BadUserAuthentication)
{
// User token was expired - log them out
var session = (Session)s;
_ = session.User.LogOutAsync();
// Redirect user to main screen and reauthenticate
}
};
This is obviously not great, which is why I’ve reached out to the cloud team to see if we can remove the expiration for anonymous users’ refresh tokens. Even if we do that, your current users’ refresh tokens have an expiry date, so it might be a good idea to add the temporary workaround code that I shared above.
If I want publish a release that solve problem simply update the app from store, can I logout always User and reconnect? Or I have to intercept error as in your sample code?
If you always log out the user, you’ll have to redownload everything on every app start, which is probably going to be pretty bad UX. I’d recommend using the code I posted as that should only happen once every 2 months. And again, we’ll try to remove the token expiration for anonymous users and fix the linked realm-core issue that will make this code unnecessary.
How does it get stuck? Keep in mind that if you have a lot of data, it may take a while for a new user to download things. Also, can you bump the logging level and check if there’s actually any activity in the logs? You do it by setting Realms.Logging.Logger.LogLevel = Realms.Logging.LogLevel.Detail.
GetInstanceAsync needs to download all the data that is not on the device. If you had logged in previously, you would only download recent changes, but a new user needs to download everything from scratch. My guess is that you have a lot of data that needs to be transferred to the device, but you weren’t seeing that previously because you were only downloading incremental changes. You can check the amount of data that is being downloaded by adding an OnProgress callback to your sync configuration. You can log the amount of data that needs to be downloaded as well as the download progress to get a sense of how much time it’ll take for GetInstanceAsync to complete.