Mac App: Sync Error, HTTP response 502


#1

Hello Realm Team,

My MacOS app is having a problem synchronizing Realm. It had been synchronizing fine until I created many objects (160,000 or so) and did commitWrite(). Now, it doesn’t sync at all. I get 502 Http response repeatedly. Please see the logs below.

I can access the realm in Cloud through Realm Studio. The changes are not reflected there. I have another app (iOS) that is synced to the same realm. This iOS app has no problem synchronizing. Any changes on the Cloud side get reflected immediately.

  • Realm Studio Version 1.20.0 (1.20.0.265)
  • Realm Swift 3.3.1

Realm Studio Error Messages:

Failed while synchronizing Realm:
End of input

Failed while synchronizing Realm:
Bad WebSocket handshake response 502-504 received

Xcode logs:

2018-04-01 15:02:57.692470-0400 -[7248:702045] Sync: Connection[1]: Connected to endpoint '52.43.20.207:443' (from '192.168.1.153:50375')
2018-04-01 15:03:12.605932-0400 -[7248:702045] Sync: Connection[1]: Reading failed: End of input
2018-04-01 15:03:12.605970-0400 -[7248:702045] Sync: Connection[1]: Connection closed due to error
2018-04-01 15:03:12.696826-0400 -[7248:702045] Sync: Connection[1]: Connected to endpoint '52.43.20.207:443' (from '192.168.1.153:50378')
2018-04-01 15:03:13.109089-0400 -[7248:702045] Sync: Connection[1]: Websocket: Expected HTTP response 101 Switching Protocols, but received:
HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Content-Length: 173
Content-Type: text/html
Date: Sun, 01 Apr 2018 19:03:13 GMT
Server: nginx/1.13.5

<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.13.5</center>
</body>
</html>

Cloud logs:

Internal sync error (path: /__wildcardpermissions). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:12 PM
Internal sync error (path: /__configuration). Message: End of input, category: realm.network, code: 1, name: Error.
3:03:12 PM
Internal sync error (path: /__configuration). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:12 PM
Internal sync error (path: /__perm). Message: End of input, category: realm.network, code: 1, name: Error.
3:03:12 PM
Internal sync error (path: /__perm). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:12 PM
Internal sync error (path: /__password). Message: End of input, category: realm.network, code: 1, name: Error.
3:03:12 PM
Internal sync error (path: /__password). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:12 PM
Internal sync error (path: /default). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:13 PM
Internal sync error (path: /__configuration). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:13 PM
Internal sync error (path: /__wildcardpermissions). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:13 PM
Internal sync error (path: /__revocation). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:13 PM
Internal sync error (path: /__admin). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:13 PM
Internal sync error (path: /__perm). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:14 PM
Internal sync error (path: /__password). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:14 PM
Internal sync error (path: /default). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:15 PM
Internal sync error (path: /__configuration). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:15 PM
Internal sync error (path: /__revocation). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:16 PM
Internal sync error (path: /__perm). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:16 PM
Internal sync error (path: /__wildcardpermissions). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:16 PM
Internal sync error (path: /__admin). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:16 PM
Internal sync error (path: /__password). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.
3:03:17 PM
Internal sync error (path: /default). Message: Connection refused, category: realm.basic_system, code: 111, name: Error.


#2

Hello,

Could you please send me a DM with the address of your instance so that we can check it out?


#3

Okay, just to confirm:
The iOS app is connecting and synchronizing fine.
The macOS app can’t sync.
Realm Studio connects - does it see changes applied from the iOS app?

I checked the logs and health monitoring of your instance and everything comes up fine, so I’m not sure anything’s wrong on the server side.

Does the macOS app authenticate successfully to the server with the SyncUser methods? From the logs you provided it seems that opening a realm fails, as that’s what opens a websocket connection.


#4

The iOS app is synchronizing fine.
The macOS app can’t sync. (It was working fine until yesterday though…)
Realm Studio connects.
And Realm Studio sees changes applied from the iOS app.

Yes, the macOS app authenticates successfully with SyncUser methods.


#5

I suspect something in the local state of the macOS app must have gotten corrupted if everything else is okay.

What happens if you create a blank macOS app in Xcode and try to open the synchronized realm from the server there?


#6

A new macOS app can connect and open the synchronized realm. Any changes are synchronized both ways.

So, if the local file is corrupted, how can I fix this?


#7

Right now there isn’t a way you can delete the sync realm file programmatically - I have reached out to our Cocoa team to get clarification on this, and I’ll get back to you.

What you can do right now is delete file manually - you can find it under ~/Library/Application Support/your.bundle.identifier or, if it’s a sandboxed app, under Library/Application Support within your app’s sandbox container. I think you should also be able to see the precise path to the realm file in the app’s logs the moment you open it and the sync client attempts to establish a session.


#8

Thanks. I deleted the file and the app now synchronizes. (The changes the app made locally are gone, of course.)

I have no idea how the file got corrupted. If I can reproduce the problem, I will be sure to let you know.


#10

For future readers - the following function will programmatically delete your local Realm files

func doDelete() {
    let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
    print("deleting: \(realmURL)")
    let realmURLs = [
        realmURL,
        realmURL.appendingPathExtension("lock"),
        realmURL.appendingPathExtension("note"),
        realmURL.appendingPathExtension("management"),
        realmURL.appendingPathExtension("realm-object-server")
    ]
    for URL in realmURLs {
        do {
            try FileManager.default.removeItem(at: URL)
        } catch {
            // handle error
        }
    }
}