SwiftcordApp/Swiftcord

Flaky Gateway Connectivity

Closed this issue · 21 comments

When loading channels i get this error

<E> [DiscordGateway] Unknown close code: 1000
2022-04-13 07:18:59.697935+0100 Swiftcord[37958:1482007] [SwiftUI] Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

and when looking into xcode
image

Im not sure if this warning is turning into an error or not due to my account having server folders and Swiftcord not supporting that just yet or its an actual error. So i have 2 questions. How do i log out of my account before fully loading in? and Is there a way to solve this warning?

Ok i made a way to log out by copying the log out btn from user settings onto the loading view.

So the error is not related to server folders, ill try my best to solve it but its here if anyone is able to help.

@bentettmar, these warnings are due to me trying out a performance optimization, and do not impact functionality (you can just ignore them, in my testing). Swiftcord simply ignores server folders, so that shouldn't cause problems. To help me debug this issue, could you send more of the logs?

Also, you might want to try the newly-created native-urlsession-websocket branch which contains fixes for the warnings, and potentially has performance increases. Reconnection is not implemented yet, so if you disconnect the Gateway will never reconnect until you restart Swiftcord.

It's quite puzzling that the socket is closing with code 1000, since discord close codes usually are in the range 400x. I'll need more of the log to debug this, up to and after the close code error. Please also try the new branch, that might be easier to debug

It closes after a while of receiving discord's gateway message events.
heres the full log: https://pastebin.com/R8NETuKW

I will try out the native-urlsession-websocket branch

It seems to me that the gateway is being terminated because no heartbeats were received within the tolerance. You could try the other branch, which might fix this issue.

After updating to the new branch, i get this error being spammed in the console.

<D> [DiscordGateway] Resume called
<E> [DiscordGateway] Connection error: Optional(Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected" UserInfo={NSErrorFailingURLStringKey=https://gateway.discord.gg/?v=9&encoding=json, NSErrorFailingURLKey=https://gateway.discord.gg/?v=9&encoding=json})

I did also enable incoming connections but that didnt seem to do anything.

Ok, I'm seeing a common trend of issues here: nothing is received by the WebSocket, so it eventually kills the connection.

What were the messages before that error gets spammed in the console?

I did also enable incoming connections but that didnt seem to do anything.

Was it not already enabled? If it wasn't, I think I see the issue now. But still, please send me the log before the error starts spamming.

Heres the full log: https://pastebin.com/XaWTLeHF
It also spams this as well:
image

It seems like the gateway is randomly disconnecting. Do you have a stable internet connection?

Yes, my internet is fine and i can connect to Discord's gateway because my bots run fine and i can load the original client.

Yeah, the spamming is a side-effect of my incorrect handling of close events. The only conclusion I can get is the websocket disconnects randomly, and without any reason. What you could try to do, is to log the receive closure to get a better idea of the messages coming in
Screen Shot 2022-04-13 at 6 07 08 PM

Screen Shot 2022-04-13 at 6 08 22 PM

Logging the reason and close code here can shed some light on the issue as well,

Screen Shot 2022-04-13 at 6 09 36 PM

And finally, removing the cases for .error, .cancelled and .binary should fix the spamming issue (but does not fix the root issue of the socket disconnecting)

Ok ill take a look at it next time i have time. Thanks for the input.

Ok, hopefully you can sort out this issue :D. This is a weird case I've never met before, after testing it on numerous accounts and different network conditions.

god I wish I knew I how to code, this stuff is awesome I'll pitch in during summer when school is over for sure! love your work

Hey, @bentettmar, I completely rewrote the underlying WebSocket wrapper class, which should hopefully increase its reliability. You could try pulling the latest commits from the native-urlsession-websocket branch and building it again, and seeing if the issue persists.

I pulled the native-urlsession-websocket branch but during build i get this error

error: Build input file cannot be found: '/Macintosh HD/Users/vinkwok/XcodeProjects/Swiftcord/Swiftcord/Utils/LoggerInit.swift' (in target 'Swiftcord' from project 'Swiftcord')

Fixed in discord.