Skip to content
This repository has been archived by the owner. It is now read-only.

KeepAliveHandler threads leak #309

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

KeepAliveHandler threads leak #309

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

Comments

@Hornwitser
Copy link
Contributor

@Hornwitser Hornwitser 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
Copy link

@ghost ghost commented Aug 25, 2016

Do you know what may be causing it?

Loading

@ghost
Copy link

@ghost ghost 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.

Loading

@Rapptz Rapptz added the bug label Sep 3, 2016
@Hornwitser
Copy link
Contributor Author

@Hornwitser Hornwitser commented Sep 16, 2016

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

Loading

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants