]> jfr.im git - erebus.git/commitdiff
Fix a bug that could lose messages from the queue.
authorJohn Runyon <redacted>
Sun, 30 Jan 2022 07:40:27 +0000 (01:40 -0600)
committerJohn Runyon <redacted>
Sun, 30 Jan 2022 07:48:51 +0000 (01:48 -0600)
Specifically, when a message was chosen from the queue, and:
- It was somehow over `recvq` (for example, a certain developer was tinkering with his bot)
- Or it plus the *amount recently sent* was over `recvq`
The message would be silently dropped.

bot.py

diff --git a/bot.py b/bot.py
index 2528d49ac70cd91a86b2633118471af2a4022b9a..3e02f9443007e9b2d07124d6f902821ff54a6102 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -53,8 +53,8 @@ class Bot(object):
 
                self.msgqueue = deque()
                self.slowmsgqueue = deque()
-               self.makemsgtimer()
-               self.msgtimer.start()
+               self._makemsgtimer()
+               self._msgtimer.start()
 
        def __del__(self):
                try:
@@ -427,29 +427,34 @@ class Bot(object):
                return command
 
        def _popmsg(self):
-               self.makemsgtimer()
+               self._makemsgtimer()
                self.conn.bytessent -= self.conn.recvq/3
                if self.conn.bytessent < 0: self.conn.bytessent = 0
-               self.conn.exceeded = False
+               self.conn.exceeded = True
 
+               cmd = None
                try:
                        cmd = self.msgqueue.popleft()
-                       if not self.conn.exceeded and self.conn.bytessent+len(cmd) < self.conn.recvq:
-                               self.conn.send(cmd)
-                               self.conn.exceeded = True
-                       else: raise IndexError
                except IndexError:
                        try:
                                cmd = self.slowmsgqueue.popleft()
-                               if not self.conn.exceeded and self.conn.bytessent+len(cmd) < self.conn.recvq:
-                                       self.conn.send(cmd)
-                                       self.conn.exceeded = True
                        except IndexError:
                                pass
-               self.msgtimer.start()
 
-       def makemsgtimer(self):
-               self.msgtimer = MyTimer(3, self._popmsg)
+               if cmd is not None:
+                       if self.conn.bytessent+len(cmd) > self.conn.recvq: # If it's too long
+                               if len(cmd) > self.conn.recvq: # Is the command itself somehow over max length???
+                                       self._msgtimer.start()
+                                       raise ValueError('Somehow a command that was too long made it into the message queue. Uhoh!', cmd)
+                                       # Discard the message.
+                               self.msgqueue.appendleft(cmd) # Phew, we've just sent too much recently. Put it (back) on the (primary) queue.
+                       else:
+                               self.conn.send(cmd)
+
+               self._msgtimer.start()
+
+       def _makemsgtimer(self):
+               self._msgtimer = MyTimer(3, self._popmsg)
 
        def join(self, chan):
                self.conn.send("JOIN %s" % (chan))