Bernhard
Bernhard

Reputation: 1273

Python IRC Bot silent timeout(?) - Stops working suddenly

I recently started writing my own IRC Bot, nothing fancy, just reads a channel and posts contents of a text file for specific commands.

# let's connect
irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc_socket.connect((server, 6667))

# now we login and send a test message
irc_socket.send(str.encode('PASS ' + pw + '\r\n'))
irc_socket.send(str.encode('NICK ' + bot + '\r\n'))
irc_socket.send(str.encode('USER ' + bot + '\r\n'))
irc_socket.send(str.encode('JOIN #' + channel + '\r\n'))

while 1:
    text = irc_socket.recv(2040).decode()
    if text.startswith('!ideen'):
        do stuff #lot of code, shouldn't be of importance

The weird thing is that everything works as I want it to, but for some reasons it stops working after some time. It doesn't timeout or give any errors, it just stops reacting at all. First I thought it gets overload by too many messages, but it also happens if the channel simply stays empty for 20 mins or so. Happens the same, when I run it from Pycharm or the command line (not sure why it'd make a difference).

I'm probably not the only one with the problem, but I couldn't find anything. Thank you for your help, even hints for how to debug something like this.

The IRC Channel is a Twitch-IRC Chat if that is of any importance.

Edit: Tried @matt-m hint for debugging, it seems he stops getting the messages. The text-messages always are empty strings.

Upvotes: 1

Views: 430

Answers (1)

Teemu Risikko
Teemu Risikko

Reputation: 3265

Are you perhaps not responding to a server PING with a PONG?

https://www.rfc-editor.org/rfc/rfc2812#section-3.7.2

If a connection fails to respond to a PING message within a set amount of time, that connection is closed.

So you should make sure you do that. I thought that it would raise some socket error when the connection closes, but according to How to tell if a connection is dead in python, that might not always be the case.

Upvotes: 4

Related Questions