jaraco / irc

Full-featured Python IRC library for Python.
MIT License
390 stars 84 forks source link

set_keepalive resulting into "irc.client.ServerNotConnectedError: Not connected." #209

Open OrpheusGr opened 1 year ago

OrpheusGr commented 1 year ago

I'm developing a Relay Bot for Discord + IRC. It's logic is it creates a new ServerConnection object for each Discord user. It all works fine, untill i recently made every new connection use the set_keepalive() function. When i call quit() on a ServerConnection object sortly after i get this error.

File "/home/orfeasgr/NewRelayTest/classcon.py", line 48, in startloop
    reactor.process_once(0.2)
  File "/home/orfeasgr/.local/lib/python3.7/site-packages/irc/client.py", line 830, in process_once
    self.process_timeout()
  File "/home/orfeasgr/.local/lib/python3.7/site-packages/irc/client.py", line 800, in process_timeout
    self.scheduler.run_pending()
  File "/home/orfeasgr/.local/lib/python3.7/site-packages/tempora/schedule.py", line 185, in run_pending
    self.run(command)
  File "/home/orfeasgr/.local/lib/python3.7/site-packages/tempora/schedule.py", line 203, in run
    command.target()
  File "/home/orfeasgr/.local/lib/python3.7/site-packages/irc/client.py", line 556, in ping
    self.send_items('PING', target, target2)
  File "/home/orfeasgr/.local/lib/python3.7/site-packages/irc/client.py", line 595, in send_items
    self.send_raw(' '.join(filter(None, items)))
  File "/home/orfeasgr/.local/lib/python3.7/site-packages/irc/client.py", line 603, in send_raw
    raise ServerNotConnectedError("Not connected.")
irc.client.ServerNotConnectedError: Not connected.

At first i thought it was me using quit() instead of disconnect() but changing that still raised the same error. Then i wondered if process.once() could be causing the error, but then i realised that would have happened a lot earlier, so i tried to remove the set_keepalive call, which was a much more recent addition to my code and after disconnecting a connection the error didn't happen.

So i came to the conclusion that set_keepalive is apparently called when process_once() is called(?) and that it tries to send a PING command to the server for all ServerConnection objects and that in turn raises the Not Connected error, when any of the objects are not connected.

I assume somewhere along the way the connection object is not removed properly from the Reactor object's connections list when a ServerConnection object is disconnected.

EDIT: I realised i didn't really explain how i'm using the library. My code basically creates a Reactor object and then, when needed, makes a new ServerConnection object, and when needed can disconnect one of those. That's when set_keepalive() ultimately leaded to the error being raised.