<?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: Database connection leak in Typo 4.0.3: problem solved</title>
    <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved</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>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Adam Byrtek</title>
      <description>&lt;p&gt;For future reference: this problem was caused by the fact that SQLite adapter in Rails didn&amp;#8217;t handle closing of old connections properly. Using allow_concurrency only made this visible. The problem was fixed in &lt;a href="http://github.com/rails/rails/commit/7359dc0028b87f948d188f5fa6bb366a49181a81"&gt;this commit&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 05 Aug 2008 18:42:24 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:6ff647a8-17ae-468e-be09-b054f210d858</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-758</link>
    </item>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Tim Harper</title>
      <description>&lt;p&gt;Well&amp;#8230; since I just posted a comment I realized I was wrong about allow_concurrency making ActiveRecord thread safe.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://tinyurl.com/6b4m6h"&gt;http://tinyurl.com/6b4m6h&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;So, what is it good for?  I have no idea now.&lt;/p&gt;</description>
      <pubDate>Thu, 03 Jul 2008 12:50:17 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:444133a5-7295-4d9b-b607-fabf0c46db28</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-747</link>
    </item>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Tim Harper</title>
      <description>&lt;p&gt;Woah was I late to the party :)&lt;/p&gt;</description>
      <pubDate>Thu, 03 Jul 2008 11:22:47 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:94558433-a41d-4fb6-b2fd-3b53411b4bd8</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-746</link>
    </item>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Tim Harper</title>
      <description>&lt;p&gt;ActiveRecord.allow_concurrency = true is only good for making it thread safe.  However, contrary to what you may believe (and perhaps depending on the database adapter you are using), it does not allow you to run queries in parallel (though it does allocate a connection for each thread.&lt;/p&gt;


	&lt;p&gt;This is because of ruby&amp;#8217;s green threads which are processed by the VM.  Everytime a native function is run, ruby seems to lose control of the execution and therefore can no longer process the running threads.&lt;/p&gt;


	&lt;p&gt;POSIX threads should fix this.&lt;/p&gt;</description>
      <pubDate>Thu, 03 Jul 2008 11:22:22 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:fbda582a-61c5-4087-aa40-18c93ecf48cb</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-745</link>
    </item>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Piers Cawley</title>
      <description>&lt;p&gt;I think I need to get my brain fixed. r1255 is the one you want.&lt;/p&gt;


	&lt;p&gt;[Piers wanders off into the night, mumbling to himself and occasionally exlaiming &amp;#8220;Millenium hand and shrimp!&amp;#8221;]&lt;/p&gt;</description>
      <pubDate>Fri, 25 Aug 2006 03:04:42 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:45c8bf8d-2797-456f-8677-cefe49b5f3db</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-169</link>
    </item>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Piers Cawley</title>
      <description>&lt;p&gt;Sorry, 1254&lt;/p&gt;</description>
      <pubDate>Fri, 25 Aug 2006 02:58:46 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:09a8545c-f456-4483-aa8e-4f49b0c1aa44</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-168</link>
    </item>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Piers Cawley</title>
      <description>&lt;p&gt;And, as the guilty party in turning concurrency back on, I woke up this morning, made Ping threadsafe (I think) and turned concurrency back off.&lt;/p&gt;


	&lt;p&gt;Revision 1245 on the trunk if you&amp;#8217;re doing 4.1 stuff, but I&amp;#8217;m sure it&amp;#8217;ll get backported to the stable branch and you&amp;#8217;ll find 4.04 getting released shortly thereafter.&lt;/p&gt;</description>
      <pubDate>Fri, 25 Aug 2006 02:56:26 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:ccf20c9a-54eb-45d4-a6fb-62a165a49204</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-167</link>
    </item>
    <item>
      <title>"Database connection leak in Typo 4.0.3: problem solved" by Scott Laird</title>
      <description>&lt;p&gt;Oops&amp;#8212;it looks like we screwed this up.  I didn&amp;#8217;t realize that we&amp;#8217;d turned concurrency on.  Unfortunatenly, turning it off breaks Postgres right now.  Hmm.&lt;/p&gt;</description>
      <pubDate>Thu, 24 Aug 2006 19:55:53 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d725613c-3f1f-462d-ac7e-7c7bb3713f77</guid>
      <link>http://blog.moertel.com/articles/2006/08/24/database-connection-leak-in-typo-4-0-3-problem-solved#comment-166</link>
    </item>
  </channel>
</rss>
