If you call this within the worker:<br><br>ActiveRecord::Base.connection.disconect!<br><br>It will close it's connection to the DB. Just make sure you call it after it's done :)<br><br>.: Michael :.<br><br><div><span class="gmail_quote">
On 10/24/06, <b class="gmail_sendername">Renaud Morvan</b> <<a href="mailto:renaud.morvan@feedback20.com">renaud.morvan@feedback20.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br><br>There is an issue on socket connection with backgroundrb (rev 47) and<br>mysql backend: after a certain number of job you reach the mysql<br>connection limit as socket are never closed and nobody can connect to
<br>mysql anymore till you stop the backgroundrb daemon.<br><br>Already reported on:<br><a href="http://rubyforge.org/pipermail/backgroundrb-devel/2006-July/000066.html">http://rubyforge.org/pipermail/backgroundrb-devel/2006-July/000066.html
</a>,<br><a href="http://rubyforge.org/pipermail/backgroundrb-devel/2006-August/000169.html">http://rubyforge.org/pipermail/backgroundrb-devel/2006-August/000169.html</a><br><br>It is quite easy to reproduce:<br>- just have a simple worker with a simple command using AR (
Model.find I.E.)<br>- open a console and launch the work 100+ times, an exception will be<br>raised 'too many connection'. The whole system won't be able to use<br>mysql anymore as mysql socket are not closed<br><br>In my humble opinion there are two reasons for it:
<br>- ActiveRecord::Base.allow_concurrency = true<br>That will make every thread open it's own socket to mysql<br>- Mysql adapter implementations (RAILS or gems) that close connection on<br>Garbage connexion (using ObjectSpace.define_finalizer
)<br>There is probably a memory leak on connections as the Proc finalizer is<br> not be executed when worker thread is killed (= connections are not<br>GC). In this case the only way to release the sockets is to close the
<br>backgroundrb daemon.<br><br>Those two phenomenons lead to an accumulation of opened socket till you<br>reach the system limit, and when you reach it, nothing no new<br>connections can be done on mysql anymore on the whole system. (this is
<br>observable with a netstat |grep mysql)<br><br>This can be workaround by setting ActiveRecord::Base.allow_concurrency<br>to false (do not see the risks but there are some probably) or by<br>manually closing the connections in worker before termination the tread
<br>(in this case you have to use job_ctrl to avoid thread to be killed<br>before having closed the connections)<br><br>Cronned restart of backgroundrb also fix this :)<br><br>Anyway maybe the problem is in the design of Backgroundrb itself, that
<br>should use a pool of thread and not create as many thread as there are<br>works, in this case the pool of thread will use a finite number of<br>socket, whose connections will be reused.<br><br>Otherwise it would be good to find the memory leak (= why killing thread
<br>don't make connections to be GC, or more precisely why<br>ObjectSpace.define_finalizer does not work in this case) but it seems to<br>be a bit tricky.( some rubyist seems to have the same kind of problem<br>with thread and
ObjectSpace.define_finalizer<br><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/291">http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/291</a> or<br><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/145913">
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/145913</a>)<br><br>Hope this help, if someone has better solutions please talk :)<br><br>Renaud Morvan<br><a href="http://exalead.feedback20.com">http://exalead.feedback20.com
</a><br>_______________________________________________<br>Backgroundrb-devel mailing list<br><a href="mailto:Backgroundrb-devel@rubyforge.org">Backgroundrb-devel@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/backgroundrb-devel">
http://rubyforge.org/mailman/listinfo/backgroundrb-devel</a><br></blockquote></div><br>