<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: Tag activerecord</title>
    <link>http://blog.moertel.com/articles/tag/activerecord?tag=activerecord</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Database connection leak in Typo 4.0.3: problem solved</title>
      <description>&lt;p&gt;In &lt;a href="http://blog.moertel.com/articles/2006/08/24/typo-4-0-3-instability-and-a-minor-patch-for-sqlite3-ruby"&gt;an earlier post&lt;/a&gt; I wrote about stability
problems that have plagued my blog since upgrading from &lt;a href="http://typosphere.org"&gt;Typo&lt;/a&gt; 4.0.0 to 4.0.3.  I have finally traced the problem to its source, and here&amp;#8217;s the deal:&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;If you&amp;#8217;re serving Typo up via &lt;a href="http://mongrel.rubyforge.org/index.html"&gt;Mongrel&lt;/a&gt;, do not configure ActiveRecord to allow concurrency.&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;One of the changes between Typo 4.0.0 and 4.0.3 is this
addition to the &lt;code&gt;environment.rb&lt;/code&gt; file:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;config.active_record.allow_concurrency = true
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;del&gt;Comment out this line, restart Typo, and the problem is solved.&lt;/del&gt;
Apply Changeset 1255, and the problem is solved.  (See
&lt;a href="#article165-update2"&gt;Update 2&lt;/a&gt;, below.)&lt;/p&gt;


	&lt;h3&gt;Discussion&lt;/h3&gt;


&lt;p&gt;When &lt;code&gt;ActiveRecord::Base.allow_concurrency&lt;/code&gt; is set to
&lt;code&gt;true&lt;/code&gt;, AR will give each thread its own database
connections and cache them in thread-localized storage.  The idea is
that, in a multi-threaded environment, this simple policy prevents
unsafe interactions between threads and the database.  (Imagine what
would happen if one thread &amp;#8220;borrowed&amp;#8221; a connection over which
another thread had opened a transaction.  Oops, there goes
transactional isolation.)&lt;/p&gt;

	&lt;p&gt;This policy, however, does place a burden on the owner of the threads to
make sure that each thread&amp;#8217;s local connection cache is cleared when
the thread is joined, a burden that is not, it would seem, being
carried by Typo under Mongrel.  As a result, Typo rapidly chews
through the allotment of file descriptors that the operating system
kindly had reserved for Mongrel:&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://community.moertel.com/~thor/pix/20060824/blog-fd-usage-vs-time.png" title="Typo 4.0.3 on Mongrel w/ SQLite3 consumes about 1.7 file descriptors per minute when ActiveRecord is configured to allow concurrency" alt="Typo 4.0.3 on Mongrel w/ SQLite3 consumes about 1.7 file descriptors per minute when ActiveRecord is configured to allow concurrency" /&gt;&lt;/p&gt;


	&lt;p&gt;(On my Linux server, the Mongrel process gets an allotment of 1024
file descriptors.)&lt;/p&gt;


	&lt;p&gt;Lucky for us, this each-thread-gets-its-own-connections policy is unnecessary under
Mongrel because Mongrel, while being multi-threaded itself, serializes
all access to the Rails-based applications it serves up:&lt;/p&gt;


&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Q: Is [Mongrel] multi-threaded or can it handle concurrent requests?&lt;/strong&gt;&lt;/p&gt;

	&lt;p&gt;Mongrel is uses a pool of thread workers to do it&amp;#8217;s processing. This means that it is able to handle concurrent access and should be thread safe. This also means that you have to be more careful about how you use Mongrel. You can&amp;#8217;t just write your application assuming that there are no threads involved. ...&lt;/p&gt;


	&lt;p&gt;Ruby on Rails is not thread safe so there is a synchronized block around the calls to Dispatcher.dispatch. This means that everything is threaded right before and right after Rails runs. While Rails is running there is only one controller in operation at a time.&lt;/p&gt;


(Source: &lt;a href="http://mongrel.rubyforge.org/faq.html"&gt;Mongrel &lt;span class="caps"&gt;FAQ&lt;/span&gt; list&lt;/a&gt;)
&lt;/blockquote&gt;

Thus we can safely turn off (i.e., comment out in Typo&amp;#8217;s
&lt;code&gt;environment.rb&lt;/code&gt; file) ActiveRecord&amp;#8217;s allow-currency option
without having to worry about nasty concurrency or performance issues:

&lt;pre&gt;&lt;code&gt;# the following line is commented out
# config.active_record.allow_concurrency = true
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;For more on this subject, see &lt;a href="http://dev.rubyonrails.org/ticket/2162"&gt;Rails ticket
#2162&lt;/a&gt; and &lt;a href="http://dev.rubyonrails.org/ticket/2742"&gt;Rails ticket
#2742&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Now, here&amp;#8217;s my question: Are there any environments in which
Typo can run with the allow-concurrency option enabled and &lt;em&gt;not&lt;/em&gt;
leak database connections?  Inquiring minds want to know.&lt;/p&gt;


&lt;div class="update"&gt;

	&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Upon further investigation, turning off
concurrency might not be altogether without risk.  Some of the Typo
code that handles potentially long tasks, such as making trackbacks
and pings, spawns new threads in which to carry out its work.  I&amp;#8217;m
looking further into this risk.  Updates to come.&lt;/p&gt;


&lt;p id="article165-update2"&gt;&lt;strong&gt;Update 2:&lt;/strong&gt; Piers Cawley added &lt;a href="http://www.typosphere.org/trac/changeset/1255"&gt;Changeset
1255&lt;/a&gt;, which turns AR&amp;#8217;s
allow-concurrency flag back off and revises the ping code so that
it does not attempt concurrent database access.  Apply &lt;a href="http://www.typosphere.org/trac/changeset/1255?format=diff&amp;#38;new=1255"&gt;the patch version of
1255&lt;/a&gt;
and restart Typo to get the fix.  A tip of the hat to Piers for making
the quick fix when he was supposed to be on holiday.&lt;/p&gt;

&lt;/div&gt;</description>
      <pubDate>Thu, 24 Aug 2006 15:41:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:ef03f59b-8bc4-4744-b94d-2966da53dca2</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved</link>
      <category>ruby</category>
      <category>typo</category>
      <category>rails</category>
      <category>typo</category>
      <category>sqlite3</category>
      <category>rails</category>
      <category>activerecord</category>
      <category>concurrency</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/165</trackback:ping>
    </item>
  </channel>
</rss>
