[Mailman-Developers] Re: Dealing with SMTP server down + SMTP_MAX_SESSIONS_PER_CONNECTION fix

Marc MERLIN marc_news@vasoftware.com
Sat, 23 Mar 2002 22:15:00 -0800


On Sat, Mar 16, 2002 at 07:19:45PM -0800, Marc MERLIN wrote:
> mailman-cvs gives me:
> 
> Mar 16 19:18:23 2002 (28557) Uncaught runner exception: (111, 'Connection refused')
 (...)
>   File "/usr/lib/python2.1/smtplib.py", line 222, in connect
>     self.sock.connect((host, port))
> error: (111, 'Connection refused')
> 
> Maybe it could just log the fact that it could not connect to the SMTP server?

Ok, I  fixed that (although  my fix might need  to be tweaked  further), and
more importantly  I fixed  the non  working SMTP_MAX_SESSIONS_PER_CONNECTION
option.

Yeah, it does break the one patch, one fix rule, but it's trivial to take
the two apart.

--- mailman-cvs/Mailman/Handlers/SMTPDirect.py	Fri Mar 15 22:05:17 2002
+++ /var/local/mailman/Mailman/Handlers/SMTPDirect.py	Sat Mar 23 22:03:54 2002
@@ -50,27 +50,39 @@
 
     def __connect(self):
         self.__conn = smtplib.SMTP()
-        self.__conn.connect(mm_cfg.SMTPHOST, mm_cfg.SMTPPORT)
+	try:
+	    self.__conn.connect(mm_cfg.SMTPHOST, mm_cfg.SMTPPORT)
+	except socket.error:
+	    if mm_cfg.SMTPPORT:
+		syslog("error", "Fatal error: cannot connect to SMTP server %s on port %d", mm_cfg.SMTPHOST, mm_cfg.SMTPPORT)
+	    else:
+		syslog("error", "Fatal error: cannot connect to SMTP server %s", mm_cfg.SMTPHOST)
+	    raise
+
         self.__numsessions = mm_cfg.SMTP_MAX_SESSIONS_PER_CONNECTION
 
     def sendmail(self, envsender, recips, msgtext):
-        try:
-            return self.__conn.sendmail(envsender, recips, msgtext)
-        except smtplib.SMTPException:
-            # For safety, reconnect
-            self.__conn.quit()
-            self.__connect()
-            # Let exceptions percolate up
-            raise
         # Decrement the session counter, reconnecting if necessary
         self.__numsessions -= 1
         # By testing exactly for equality to 0, we automatically handle the
         # case for SMTP_MAX_SESSIONS_PER_CONNECTION <= 0 meaning never close
         # the connection.  We won't worry about wraparound <wink>.
+	# There is a really small bug where the first batch will contain one
+	# less mail as asked. That's ok though, we are simply one below the MAX
+	# value for the first batch -- Marc
         if self.__numsessions == 0:
             self.__conn.quit()
             self.__connect()
 
+        try:
+            return self.__conn.sendmail(envsender, recips, msgtext)
+        except smtplib.SMTPException:
+            # For safety, reconnect
+            self.__conn.quit()
+            self.__connect()
+            # Let exceptions percolate up
+            raise
+
     def quit(self):
         self.__conn.quit()
 
@@ -111,7 +139,6 @@
         deliveryfunc = bulkdeliver
     refused = {}
     t0 = time.time()
-    numsessions = mm_cfg.SMTP_MAX_SESSIONS_PER_CONNECTION
     # Open the initial connection
     origrecips = msgdata['recips']
     # `undelivered' is a copy of chunks that we pop from to do deliveries.
@@ -121,7 +148,11 @@
     # This means at worst, the last chunk for which delivery was attempted
     # could get duplicates but not every one, and no recips should miss the
     # message.
-    conn = Connection()
+    try:
+	conn = Connection()
+    except socket.error:
+	# There is probably a better thing to do here -- Marc
+	return
     try:
         msgdata['undelivered'] = chunks
         while chunks:
-- 
Microsoft is to operating systems & security ....
                                      .... what McDonalds is to gourmet cooking
  
Home page: http://marc.merlins.org/   |   Finger marc_f@merlins.org for PGP key