# Pop3 Ruby calls cause loss of MySQL connection

**URL:** <https://www.ruby-forum.com/t/pop3-ruby-calls-cause-loss-of-mysql-connection/147840>\
**Category:** Rails\
**Created:** [September 5, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/pop3-ruby-calls-cause-loss-of-mysql-connection/147840 "2008-09-05T22:15:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvargo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@mvargo](https://www.ruby-forum.com/u/mvargo)\
**Post date:** [September 5, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/pop3-ruby-calls-cause-loss-of-mysql-connection/147840/1 "2008-09-05T22:15:12Z")

</div>

Any help is very much appreciated.

MySQL 5.0 and Rails 1.2.6

I have a Deamon process that looks for incoming mail using pop3. It  
all works smashing, except after a day or so, the process starts  
throwing this error every time it tries to talk to my database.

Mysql::Error: Lost connection to MySQL server during query: SELECT \*  
FROM incoming\_email\_messages WHERE (incoming\_email\_messages.name =  
‘UID26-1219353451’) LIMIT 1

I have 4 rails instances running in a mongrel pack as well as 5 other  
daemon processes all talking to the same database. They never  
experience this loss of connectivity. The issue seems to be around my  
repeated calls to the Ruby pop3 library. I suspect that this is some  
kind of memory or garbage collection issue, but I don’t know for  
sure. Right now, I have to bounce my thread once a day to snap it out  
of it. I tried moving the Net::POP3.new call outside the loop and  
just calling start/finish on the same object but that just make it  
stop working altogether.

Here is the thread

#!/usr/bin/env ruby

## 

require ‘net/pop’  
require ‘rubygems’ # if you use RubyGems  
require ‘daemons’

Daemons.run\_proc(“incoming\_email\_thread”, {:dir\_mode =\> :normal, :dir  
=\> ‘tmp/pids’}) do  
require “/home/xxxxx/user/xxxxxx/config/environment.rb”  
loop {  
logger.info('incoming\_email\_thread: polling for email messages at  
’ + Time.now.to\_s)  
begin  
pop = Net::POP3.new(‘[mail.myserver.com](http://mail.myserver.com)’)  
pop.start(‘removed\_email\_address@domain.invalid’, ‘password’)  
if pop.mails.empty?  
logger.info('incoming\_email\_thread: no mail at ’ +  
Time.now.to\_s)  
else  
i = 0  
pop.each\_mail do |m|  
logger.info('incoming\_email\_thread: message with id ’ +  
m.unique\_id)  
filename = File.expand\_path(“#{RAILS\_ROOT}/public/models/  
emails/#{m.unique\_id}”)  
dbrec = IncomingEmailMessage.new(:name =\>  
m.unique\_id, :header =\> m.header, :status =\> ‘writing’)  
# This is where I see the error. The model has a  
validate\_uniqueness :name  
if dbrec.save  
if !File.exists?(filename)  
File.open(filename, ‘w’) do |f|  
m.pop do |chunk| # get a message little by little.  
f.write chunk  
end  
end  
end  
dbrec.update\_attribute(:status, ‘ready’)  
else  
logger.info(‘incoming\_email\_thread: message with id ’ +  
m.unique\_id + ’ has already been fetched’)  
end  
m.delete  
end  
logger.info(“incoming\_email\_thread: #{pop.mails.size} mails  
popped.”)  
end  
pop.finish  
rescue Exception =\> exc  
logger.error(“incoming\_email\_thread: receiving email…” +  
exc.message)  
logger.error(exc.backtrace.join(“\n”))  
end  
sleep 60  
}  
end
