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

KeepAliveHandler threads leak #309

Closed
Hornwitser opened this issue Aug 21, 2016 · 1 comment

Comments

@Hornwitser
Copy link
Contributor

commented Aug 21, 2016

By adding the following debug code to KeepAliveHandler in gateway.py, (basically assigning a number to each instance of the keep alive handler started, and then printing it when it starts and stops).

diff --git a/discord/gateway.py b/discord/gateway.py
index 7ba78b0..36b01f3 100644
--- a/discord/gateway.py
+++ b/discord/gateway.py
@@ -65,7 +65,12 @@ class KeepAliveHandler(threading.Thread):
         self.msg = 'Keeping websocket alive with sequence {0[d]}'
         self._stop_ev = threading.Event()

+    count = 0
     def run(self):
+      try:
+        count = KeepAliveHandler.count
+        KeepAliveHandler.count += 1
+        print("Keep alive", count, "started")
         while not self._stop_ev.wait(self.interval):
             data = self.get_payload()
             log.debug(self.msg.format(data))
@@ -76,6 +81,8 @@ class KeepAliveHandler(threading.Thread):
                 f.result()
             except Exception:
                 self.stop()
+      finally:
+        print("Keep alive", count, "stopped")

     def get_payload(self):
         return {

I get the following printed out in the console when running my bot for a long time

[... snip ...]
INFO:discord.gateway:sent the resume payload to create the websocket
INFO:discord.gateway:Websocket closed with 1001 (), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 4 started
INFO:discord.gateway:sent the resume payload to create the websocket
INFO:discord.gateway:Websocket closed with 1001 (CloudFlare WebSocket proxy rest
arting), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 5 started
INFO:discord.gateway:sent the resume payload to create the websocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 6 started
INFO:discord.gateway:sent the identify payload to create the websocket
Keep alive 5 stopped
INFO:discord.gateway:Websocket closed with 1001 (), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 7 started
INFO:discord.gateway:sent the resume payload to create the websocket
INFO:websockets.protocol:Failing the WebSocket connection: 1006
INFO:discord.gateway:Websocket closed with 1006 (), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 8 started

[... snip ..]

INFO:websockets.protocol:Failing the WebSocket connection: 1006
INFO:discord.gateway:Websocket closed with 1006 (), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 13 started
INFO:discord.gateway:sent the resume payload to create the websocket
INFO:websockets.protocol:Failing the WebSocket connection: 1006
INFO:discord.gateway:Websocket closed with 1006 (), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 14 started
INFO:discord.gateway:sent the resume payload to create the websocket
INFO:discord.gateway:Websocket closed with 1001 (), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
Keep alive 15 started
INFO:discord.gateway:sent the resume payload to create the websocket
INFO:discord.gateway:Websocket closed with 1001 (), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
INFO:discord.gateway:sent the resume payload to create the websocket
Keep alive 16 started

[... snip ...]

INFO:discord.gateway:Websocket closed with 1001 (CloudFlare WebSocket proxy rest
arting), attempting a reconnect.
INFO:discord.client:Got ResumeWebSocket
INFO:discord.gateway:Created websocket connected to wss://gateway.discord.gg?enc
oding=json&v=5
INFO:discord.gateway:sent the resume payload to create the websocket
Keep alive 22 started
shutdown
Keep alive 22 stopped

[... snip exception unrelated to this issue ...]

Indicating that these keep alive threads do not die. Only two out of 23 threads spawned has the corresponding stopped message appear. The bot ran on v0.10.0 of the library.

@ghost

This comment has been minimized.

Copy link

commented Aug 25, 2016

Do you know what may be causing it?

@ghost

This comment has been minimized.

Copy link

commented Aug 25, 2016

If not, I believe this may be solved by keeping one in a variable and closing it when a new one starts.

@Rapptz Rapptz added the bug label Sep 3, 2016

@Hornwitser

This comment has been minimized.

Copy link
Contributor Author

commented Sep 16, 2016

I have tested 0b6e74e and can comfirm that it fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.