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

"Cannot start a sync session for user '' because this user has been removed" after deleting user #8320

Open
bogdan-pechounov opened this issue Jul 28, 2023 · 1 comment

Comments

@bogdan-pechounov
Copy link

bogdan-pechounov commented Jul 28, 2023

How frequently does the bug occur?

Sometimes

Description

After deleting a user, the app crashes. It didn't happen a week ago, but today the error occurs consistently.

Stacktrace & log output

libc++abi: terminating with uncaught exception of type std::logic_error: Cannot start a sync session for user '64c31c5e274b08f41bf7de09' because this user has been removed.
(lldb)

Can you reproduce the bug?

Sometimes

Reproduction Steps

  1. Login anonymously (top left button) (swipe down the sheet)
  2. Delete account (bottom button)
Button(role: .destructive) {
    app.currentUser?.delete { error in
        self.error = error
    }
} label: {
    Text("Delete Account")
}

Repo

Version

13.17.0 and 13.15.1

What Atlas Services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

macOS 13.4.1

Build environment

Xcode version: 14.3.1
Dependency manager and version: Swift

@bogdan-pechounov
Copy link
Author

bogdan-pechounov commented Aug 1, 2023

It seems that app.currentUser isn't set to nil in time.

struct ContentView: View {
    
    @StateObject var app = Realm.app
    
    var body: some View {
        MainScreen()
            .environmentObject(app)
            .if(app.currentUser) { view, user in
                view.environment(\.realmConfiguration, user.createFlexibleConfiguration())
            }
            .onChange(of: app.currentUser) { user in
                print("USER", user)
            }
    }
    
}

When I log out, USER nil is printed, but not when I delete the account.

["email": Optional(RealmSwift.AnyBSON.string("Test"))]
2023-07-31 21:33:22.405480-0400 Simple Shopping List[45545:19939354] Info: Realm sync client ([realm-core-13.17.1])
2023-07-31 21:33:22.405777-0400 Simple Shopping List[45545:19939354] Info: Platform: iOS Darwin 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:22 PDT 2023; root:xnu-8796.121.3~7/RELEASE_X86_64 x86_64
2023-07-31 21:33:22.420201-0400 Simple Shopping List[45545:19940603] Info: Connection[1]: Session[1]: Binding '/Users/bogdan/Library/Developer/CoreSimulator/Devices/23014226-DA37-443E-A0F4-C3937ECC7F79/data/Containers/Data/Application/CBE0EDA4-6C8B-4A09-8E30-EAEC48BBE24B/Documents/mongodb-realm/application-1-mghwq/64c85fdda369168fbe061e96/flx_sync_default.realm' to ''
2023-07-31 21:33:22.420970-0400 Simple Shopping List[45545:19940603] Info: Connection[1]: Session[1]: client_reset_config = false, Realm exists = true, client reset = false
2023-07-31 21:33:22.422593-0400 Simple Shopping List[45545:19940603] Info: Connection[1]: Connecting to 'wss://ws.us-east-1.aws.realm.mongodb.com:443/api/client/v2.0/app/application-1-mghwq/realm-sync'
USER Optional(<RLMUser: 0x600000e60dc0>)
2023-07-31 21:33:22.451298-0400 Simple Shopping List[45545:19940603] Info: Connected to endpoint '3.210.32.164:443' (from '10.0.0.6:59281')
2023-07-31 21:33:22.643180-0400 Simple Shopping List[45545:19940603] Info: Connection[1]: Connected to app services with request id: "64c860e2262d7a8d21784583"
DELETE USER <RLMUser: 0x600000e5eaa0>
libc++abi: terminating due to uncaught exception of type std::logic_error: Cannot start a sync session for user '64c85fdda369168fbe061e96' because this user has been removed.
(lldb) 

I also tried displaying Text(app.currentUser?.description ?? "nil") to confirm it.

Simulator Screenshot - iPhone 14 Pro Max - 2023-07-31 at 21 44 02

(image taken after crash)

However, this is from a private repo built upon the template code. The code for syncing and deleting is the same. The error always occurs in the private repo, but it is harder to reproduce in the template repo.

extension User {
    func createFlexibleConfiguration() -> Realm.Configuration {
        let config = self.flexibleSyncConfiguration(initialSubscriptions: { subs in
            if subs.first(named: "user_items")  == nil {
                subs.append(QuerySubscription<Item>(name: "user_items") {
                    $0.userId == self.id
                })
            }
        })
        return config
    }
}
struct DeleteAccount: View {
    
    @State private var isPresented = false
    @State private var error: Error?
    
    @EnvironmentObject var app: RealmSwift.App
    
    var body: some View {
        if let user = app.currentUser {
            Button(role: .destructive) {
                isPresented = true
            } label: {
                Label("Delete account", systemImage: "trash")
                    .foregroundColor(.red)
            }
            .confirmationDialog("Are you sure?", isPresented: $isPresented) {
                Button(role: .destructive) {
                    print("DELETE USER", user)
                    user.delete { error in
                        self.error = error
                    }
                } label: {
                    Text("Delete account")
                }
            }
            .errorAlert(error: $error)
        }
    }
}

Edit:
I noticed that scrolling the list was laggy and added print statements such as let _ = print("RENDER ITEMS") inside ItemsView and MainScreen.

struct MainScreen: View {
    @ObservedResults(Item.self) var items
    
    @FocusState var focusedItem: Item?
        
    var body: some View {
        let _ = print("RENDER MAIN")
        NavigationStack {
            ItemsView(focusedItem: $focusedItem)
                .animation(.default, value: items)
                .navigationTitle("Shopping List")
                .toolbar {
                    // User
                    ToolbarItem(placement: .navigationBarLeading) {
                        AccountMenu()
                    }
                    
                    // Edit
                    ToolbarItem(placement: .navigationBarTrailing) {
                        EditButton()
                    }
                    
                    // Add
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        AddItem(focusedItem: $focusedItem)
                    }
                }
        }
    }
}

Simply scrolling was causing the main screen and thus the items view to be re-rendered. As I was debugging the cause, it suddenly stopped (I thought it was because of the focus state var focusedItem: FocusState<Item?>.Binding TextField("Item", text: $item.name).focused(focusedItem, equals: item)).

When I try to delete an account now, there are no issues.

Edit 2:
The error started happening again. I added let _ = Self._printChanges().

struct ContentView: View {
    
    @StateObject var app = Realm.app
    
    var body: some View {
        let _ = Self._printChanges()
        let _ = print(app.currentUser)
        MainScreen()
            .environmentObject(app)
            .if(app.currentUser) { view, user in
                view.environment(\.realmConfiguration, user.createFlexibleConfiguration())
            }
    }
}
ContentView: _app changed.
nil
MainScreen: @self, @identity, _items, @232, @248, @264, @288, _focusedId changed.
libc++abi: terminating due to uncaught exception of type std::logic_error: Cannot start a sync session for user '64caca90e1c5806f3a64f46b' because this user has been removed.

Interestingly, app.currentUser is set to nil, but the UI displays <RLMUser: ...> after the crash.

@bogdan-pechounov bogdan-pechounov changed the title "Cannot start a sync session for user '' because this user has been removed" after deletion "Cannot start a sync session for user '' because this user has been removed" after deleting user Aug 1, 2023
@sync-by-unito sync-by-unito bot removed the T-Bug label Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant