<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>coderrr &#187; patch</title>
	<atom:link href="http://coderrr.wordpress.com/category/patch/feed/" rel="self" type="application/rss+xml" />
	<link>http://coderrr.wordpress.com</link>
	<description>pronounced &#34;coder&#34; not &#34;code err&#34; (extended r optional)</description>
	<lastBuildDate>Fri, 06 Nov 2009 07:44:59 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='coderrr.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/2bb7acc198eb108988c3a16e267634b6?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>coderrr &#187; patch</title>
		<link>http://coderrr.wordpress.com</link>
	</image>
			<item>
		<title>ActiveRecord&#8217;s with_connection is now useful!</title>
		<link>http://coderrr.wordpress.com/2009/05/05/activerecords-with_connection-is-now-useful/</link>
		<comments>http://coderrr.wordpress.com/2009/05/05/activerecords-with_connection-is-now-useful/#comments</comments>
		<pubDate>Tue, 05 May 2009 22:27:13 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[concurrency]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=685</guid>
		<description><![CDATA[A few days ago my with_connection (originally cleanup_connection) patch finally went in to rails trunk.  It changes the way with_connection works a little bit, but should be mostly backwards compatible.
Note, you&#8217;re probably only going to care about this if you use ActiveRecord in long running threads.
Before this patch, with_connection was useless for most Rails [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=685&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A few days ago my with_connection (originally cleanup_connection) <a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1752">patch</a> finally <a href="http://github.com/rails/rails/commit/5501b99a19a2a67a9a920fd3c7bff071a2ecf058">went in</a> to rails trunk.  It changes the way with_connection works a little bit, but should be mostly backwards compatible.</p>
<p>Note, you&#8217;re probably only going to care about this if you use ActiveRecord in long running threads.</p>
<p>Before this patch, with_connection was useless for most Rails devs&#8217; situations unless we wanted to execute raw SQL:</p>
<pre class="brush: ruby;">
ActiveRecord::Base.connection_pool.with_connection do |conn|
  User.find(1)  # this will NOT use the connection passed to the block (up to Rails 2.3.x), it
                    # will create a new one, or use the one that existed before we called with_connection
  conn.execute(&quot;select raw sql here&quot;)  # this is the only way to use the connection
end
</pre>
<p>So that kinda sucked.  Because the idea of with_connection is exactly what you want when you have lots of long running threads.  You want to check out a connection, use ActiveRecord as you normally would (and have it use that new connection), and then check it back in whenever you&#8217;re done instead of leaving it lying around.  This way you could have hundreds of threads using ActiveRecord sporadically with a small sized connection pool.</p>
<p>That&#8217;s what this patch allows you to do.  Now with_connection will checkout a connection and set it as the main connection for the current thread (if it doesn&#8217;t already exist).  If a connection already exists it will do nothing.  This has the nice effect of allowing you to wrap your ActiveRecord code as closely as possible with with_connection.  If with_connection always checked out a new connection no matter what, then you&#8217;d want to wrap your code at as high a level as possible which in turn would mean you would be keeping connections checked out of the pool when you didn&#8217;t really need them.</p>
<p>Here&#8217;s a (very contrived) example:</p>
<pre class="brush: ruby;">
# shorthand
def db(&amp;b); ActiveRecord::Base.connection_pool.with_connection(&amp;b); end

class User
  def update_score
    db do
      self.score = calculate_score
      save!
    end
  end

  def calcuate_score
    db { self.score_cards.sum(&amp;:points) }
  end
end

100.times { User.all.each {|u| Thread.new { u.update_score } } }
</pre>
<p>In this example we could call calculate_score directly, or indirectly through update_score, and we would never checkout more than one connection from the pool.  We could also feel free to call these methods from any number of threads and know that when the calls finish their connections will be checked back into the pool without having to deal with any special ActiveRecord cleanup methods.</p>
<p>If this is the type of thing you are doing or want to be doing but you don&#8217;t want to actually to have wrap all your code in those annoying blocks, you can check out this <a href="http://github.com/coderrr/cleanup_connection/blob/master/cleanup_connection_patch.rb">monkeypatch</a> (<a href="http://coderrr.wordpress.com/2009/01/16/monkey-patching-activerecord-to-automatically-release-connections/">blog post here</a>)  which essentially wraps all DB touching ActiveRecord methods with with_connection for you.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/685/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=685&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2009/05/05/activerecords-with_connection-is-now-useful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby 1.8 define_method scope bug</title>
		<link>http://coderrr.wordpress.com/2009/03/29/ruby-18-define_method-scope-bug/</link>
		<comments>http://coderrr.wordpress.com/2009/03/29/ruby-18-define_method-scope-bug/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 03:04:38 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[bug]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=623</guid>
		<description><![CDATA[Update: Patch accepted, bug fixed!
While writing some tests for my tunnel_splitter project I ran into this really weird bug.  It&#8217;s hard to believe no one has run into this before.  I&#8217;ll give you the isolated version (which took considerable time to narrow down).

a = 1
Object.send :define_method, :x do
  lambda do  # [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=623&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><b>Update:</b> <a href="http://redmine.ruby-lang.org/repositories/revision/ruby-18?rev=23257">Patch accepted</a>, bug fixed!</p>
<p>While writing some tests for my <a href="http://github.com/coderrr/tunnel_splitter">tunnel_splitter</a> project I ran into this really weird bug.  It&#8217;s hard to believe no one has run into this before.  I&#8217;ll give you the isolated version (which took considerable time to narrow down).</p>
<pre class="brush: ruby;">
a = 1
Object.send :define_method, :x do
  lambda do  # lambda is necessary to reproduce bug
    p a # =&gt; 1
    a = 2
    p a # =&gt; 2
  end.call
end
x(nil) # passing at least one arg is necessary to reproduce bug
p a # =&gt; 1
</pre>
<p>This is wrong.  Instead of 1, 2, 1, the output should be 1, 2, 2.</p>
<p>Here&#8217;s another reproduction, closer to the actual way I encountered the bug.  This one is sensitive to the timing of when threads are scheduled.  By commenting out the <code>p :blah</code> line you can actually prevent the bug from happening.  That makes this bug way more evil and caused me considerable pain.</p>
<pre class="brush: ruby;">
def callblock; yield; end

a = 1
Object.send :define_method, :x do
  p :blah # comment this line out and things will work as expected
  begin
    callblock do
      raise 'blah'
    end
  rescue
    p a
    a = 2
    p a
  end
end
Thread.new{ x(nil) }.join
p a # =&gt; 1
</pre>
<p><b>Offender #1</b><br />
eval.c:8626 proc_invoke()</p>
<pre class="brush: cpp;">
    if (_block.frame.argc &amp;&amp; DMETHOD_P()) {
        NEWOBJ(scope, struct SCOPE);
        OBJSETUP(scope, tmp, T_SCOPE);
        scope-&gt;local_tbl = _block.scope-&gt;local_tbl;
        scope-&gt;local_vars = _block.scope-&gt;local_vars;
        scope-&gt;flags |= SCOPE_CLONE;
        _block.scope = scope;
    }
</pre>
<p>If the block being invoked was a block passed to define_method (DMETHOD_P()) and it is being invoked with some number of args other than 0 (_block.frame.argc) then Ruby will &#8220;clone&#8221; the scope.  Effectively, this doesn&#8217;t really do much.  It creates a new scope struct, but then it points the local vars tables to the exact same place as the original, meaning if you modify a local in this scope, it will be reflected in the original scope.  The issue is that it loses the flags of the original scope.  We will see why this is important in a second.</p>
<p><b>Offender #2</b><br />
eval.c:8214</p>
<pre class="brush: cpp;">
static void
scope_dup(scope)
    struct SCOPE *scope;
{
    ID *tbl;
    VALUE *vars;

    scope-&gt;flags |= SCOPE_DONT_RECYCLE;
    if (scope-&gt;flags &amp; SCOPE_MALLOC) return;

    if (scope-&gt;local_tbl) {
        tbl = scope-&gt;local_tbl;
        vars = ALLOC_N(VALUE, tbl[0]+1);
        *vars++ = scope-&gt;local_vars[-1];
        MEMCPY(vars, scope-&gt;local_vars, VALUE, tbl[0]);
        scope-&gt;local_vars = vars;
        scope-&gt;flags |= SCOPE_MALLOC;
    }
}
</pre>
<p>scope_dup will copy the local_vars (local variables&#8217; values) table into a new memory address.  Whenever this is done, changes to the variables in this scope will NOT affect the original scope&#8217;s variables (unless they are explicitly copied/pointed back).  Note that scope_dup will return right away and do nothing if the scope has the SCOPE_MALLOC flag set, which scope_dup sets after duping a scope, effectively making it perform only one dup per scope, even if called multiple times.</p>
<p><b>Offender #1 + Offender #2 = fail</b></p>
<p>Each of these pieces of code by themselves will not cause the bug to occur.  But put them together, specifically the scope cloning conditional and then the scope_dup function, and voila, your weekend has become an MRI debug session.</p>
<p>The issue is that the proc_invoke() scope cloning conditional will not copy the parent scope&#8217;s SCOPE_MALLOC flag.  Which means if you call scope_dup on a cloned scope you will end up duping the local variables even if they had already been duped once before.  Essentially you will end up duping a scope&#8217;s local variables twice, which means the parent scope&#8217;s variables will not be affected at all by changes to the current scope&#8217;s local variables.</p>
<p><b>Solution</b></p>
<p>If we just copy the SCOPE_MALLOC flag when cloning a scope all our problems are solved:</p>
<pre class="brush: cpp;">
      ...
        scope-&gt;local_vars = _block.scope-&gt;local_vars;
        scope-&gt;flags |= SCOPE_CLONE;
        scope-&gt;flags |= (_block.scope-&gt;flags &amp; SCOPE_MALLOC);  // add this line
        _block.scope = scope;
    }
</pre>
<p>This way if a duped scope is cloned then duped, the local var tables will still be pointing to the same place.</p>
<p>By itself this actually causes ruby to segfault due to multiple free()s on the same memory.</p>
<p>gc.c:1271</p>
<pre class="brush: cpp;">
            if (!(RANY(obj)-&gt;as.scope.flags &amp; SCOPE_CLONE) &amp;&amp; vars[0] == 0)
                RUBY_CRITICAL(free(RANY(obj)-&gt;as.scope.local_tbl));
            if (RANY(obj)-&gt;as.scope.flags &amp; SCOPE_MALLOC))
                RUBY_CRITICAL(free(vars)); // double free segfault here!
</pre>
<p>We can modify the second conditional to make sure the scope doesn&#8217;t have the SCOPE_CLONE flag before free()ing.</p>
<pre class="brush: cpp;">
            if (!(RANY(obj)-&gt;as.scope.flags &amp; SCOPE_CLONE) &amp;&amp; vars[0] == 0)
                RUBY_CRITICAL(free(RANY(obj)-&gt;as.scope.local_tbl));
            if (RANY(obj)-&gt;as.scope.flags &amp; SCOPE_MALLOC)) &amp;&amp; !(RANY(obj)-&gt;as.scope.flags &amp; SCOPE_CLONE))
                RUBY_CRITICAL(free(vars));
</pre>
<p><b>Conclusion</b></p>
<p>I&#8217;m not sure if this is the best solution.  I&#8217;m not even sure what the scope cloning conditional is needed for.  I can&#8217;t think of a reason why you would need to make some modification to the scope of a block passed to define_method called with 1 or more arguments, which you wouldn&#8217;t need to make if it were called with 0 arguments.  There probably is some reason though, and it&#8217;d be nice if someone could point out what it is.  Also I&#8217;m not sure if it might be better to just copy all the scope flags instead of just the SCOPE_MALLOC one.</p>
<p>Also, I&#8217;m definitely not an expert on the internals of MRI so it&#8217;s possible this solution actually causes some other bugs.</p>
<p>Here&#8217;s the <a href="http://gist.github.com/87097">patch</a></p>
<p><b>1.8.7 and 1.9</b></p>
<p>This bug also occurs in 1.8.7 but not in 1.9.</p>
<p><a href="http://redmine.ruby-lang.org/issues/show/1322">bug filed</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/623/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/623/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/623/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=623&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2009/03/29/ruby-18-define_method-scope-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>Ridiculous ruby meta programming hack</title>
		<link>http://coderrr.wordpress.com/2009/02/21/ridiculous-ruby-meta-programming-hack/</link>
		<comments>http://coderrr.wordpress.com/2009/02/21/ridiculous-ruby-meta-programming-hack/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 14:55:26 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[bug]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=579</guid>
		<description><![CDATA[Ruby 1.8.6 has a bug where Dir.glob will glob on a tainted string in $SAFE level 3 or 4 without raising a SecurityError as would be expected.  You can see this from the following code:

lambda { $SAFE = 4; Dir.glob('/**') }.call # raises SecurityError
lambda { $SAFE = 4; Dir.glob(['/**']) }.call # =&#62; [ ... [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=579&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Ruby 1.8.6 has a bug where <code>Dir.glob</code> will glob on a tainted string in <code>$SAFE</code> level 3 or 4 without raising a <code>SecurityError</code> as would be expected.  You can see this from the following code:</p>
<pre class="brush: ruby;">
lambda { $SAFE = 4; Dir.glob('/**') }.call # raises SecurityError
lambda { $SAFE = 4; Dir.glob(['/**']) }.call # =&gt; [ ... files ... ]
</pre>
<p>So I set out to fix this with pure ruby&#8230; and it ended up requiring some really crazy stuff.  I&#8217;ll first show what I ended up with, then go through and explain why:</p>
<pre class="brush: ruby;">
class Dir
  class &lt;&lt; self
    safe_level_password = File.open('/dev/urandom','r'){|f| f.read(10000) }

    m = Dir.method(:glob)
    define_method :glob do |password, safe, *args|
      raise SecurityError if safe_level_password != password
      $SAFE = safe
      args.flatten!

      # pass along glob opts
      opts = args.last.is_a?(Fixnum) ? args.pop : []

      args.flatten.map do |arg|
        m.call(arg, *opts)
      end.flatten
    end

    eval %{
      alias safe_glob glob
      def glob(*a)
        safe_glob(#{safe_level_password.inspect}, $SAFE, *a)
      end
    }
  end
end

# freeze Dir so that no one can redefine safe_glob* to catch password
Dir.freeze
</pre>
<p>So first things first.  The simple way to fix this bug is to alias <code>glob</code>, iterate over the array passed to the new <code>glob</code> and then call the original <code>glob</code> with one argument at a time, since we know it correctly checks taint with only one argument.</p>
<p>But wait, if we alias the original method then someone could still access the original and pass it an array.  So we have to use a <a href="http://coderrr.wordpress.com/2008/10/29/secure-alias-method-chaining/">&#8220;secure&#8221; version</a> of alias method chaining.  Essentially, we capture the method object of the original method in the local scope, then we use <code>define_method</code> to overwrite the original method name with our new implementation and call the original method object which we have &#8216;closed&#8217; over.  This allows us access to the original method while preventing anyone else from doing so.</p>
<p>But there&#8217;s another problem.  <code>$SAFE</code> levels are captured in blocks just as local variables.  This means our <code>define_method</code> version of <code>glob</code> will always run at the <code>$SAFE</code> level it was defined in, namely <code>$SAFE</code> level 0.  Meaning if you call <code>Dir.glob</code> from a <code>$SAFE</code> level 4 it will still get executed at level 0.  This is of course the exact opposite of what we want.  We are in a worse position now than before.  Now we could call <code>Dir.glob</code> with a single tainted parameter and it would work.</p>
<p>How do we fix this?  We need to use <code>def</code> to define the method so that the current <code>$SAFE</code> level 0 isn&#8217;t captured.  Instead <code>$SAFE</code> will reflect the <code>$SAFE</code> level of the caller.  But if we use <code>def</code> we can&#8217;t use the secure alias method technique.</p>
<p>One option is to have the <code>define_method</code> version set the <code>$SAFE</code> level explicitly before calling <code>glob</code>.  But then we run into the issue of how to know what to set it to?  There is no way of determining the <code>$SAFE</code> level of the caller without explicitly passing it in.</p>
<p>Ok, so what if we <code>def</code> a method which then calls the <code>define_method</code> method and passes its <code>$SAFE</code> level as an argument?  Well then the problem is how do you give the <code>def</code> method access to the <code>define_method</code> method without giving other evil code access to the <code>define_method</code> method as well.  Because then that evil code could just lie and pass a level of 0.</p>
<p>This is the crazy part.  The way to prevent the evil method from executing the <code>define_method</code> method is to use a password!</p>
<p>Both the <code>def</code> and <code>define_method</code> methods can share a secret which is only available in the local scope where they are defined.  Since the password is only a string we can use <code>eval</code> to create the <code>def</code> method and insert the password into it.  As long as the <code>define_method</code> method verifies the secret is correct before continuing we can be sure the only method able to call it is the <code>def</code> method.</p>
<p>I never thought I&#8217;d be sharing a secret between methods.  I know this is a big house of cards.  Can anyone figure out how to make it tumble?  Or a better way to fix the <code>glob</code> bug (without C).</p>
<p>And yes, you must also do the same for <code>Dir.[]</code> but I left it out for sake of brevity.</p>
<p>BTW, here&#8217;s the patch if you actually care enough to recompile:</p>
<pre class="brush: cpp;">
--- dir.c.orig	2009-02-21 21:49:09.000000000
+++ dir.c	2009-02-21 21:49:38.000000000
@@ -1659,7 +1659,7 @@
     for (i = 0; i &lt; argc; ++i) {
 	int status;
 	VALUE str = argv[i];
-	StringValue(str);
+	SafeStringValue(str);
 	status = push_glob(ary, RSTRING(str)-&gt;ptr, flags);
 	if (status) GLOB_JUMP_TAG(status);
     }
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/579/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/579/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/579/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/579/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/579/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/579/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/579/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/579/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/579/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/579/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=579&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2009/02/21/ridiculous-ruby-meta-programming-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>Monkey patching ActiveRecord to automatically release connections</title>
		<link>http://coderrr.wordpress.com/2009/01/16/monkey-patching-activerecord-to-automatically-release-connections/</link>
		<comments>http://coderrr.wordpress.com/2009/01/16/monkey-patching-activerecord-to-automatically-release-connections/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 00:25:25 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[concurrency]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=491</guid>
		<description><![CDATA[First let me say this is only really useful if you are using ActiveRecord in a non-Rails, multi-threaded context.  As in standalone Ruby app or maybe even in a different web framework.
The point of this patch is to allow you to take advantage of ActiveRecord&#8217;s connection pool without having to ever deal with it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=491&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>First let me say this is only really useful if you are using ActiveRecord in a non-Rails, multi-threaded context.  As in standalone Ruby app or maybe even in a different web framework.</p>
<p>The point of this patch is to allow you to take advantage of ActiveRecord&#8217;s connection pool without having to ever deal with it explicitly.  The biggest thing you have to worry about with the connection pool is releasing connections when you&#8217;re done with them, so that is what this patch does for you.  This allows you to have large numbers of threads, even long running ones, using ActiveRecord without requiring an equally sized connection pool.</p>
<p>For example, let&#8217;s say you&#8217;ve setup your connection pool to allow 3 connections.  Try to run this code:</p>
<pre class="brush: ruby;">
ts = []
6.times do
  ts &lt;&lt; Thread.new do
    User.find(:first)
  end
end
ts.each &amp;:join
</pre>
<p>It&#8217;s going to take approximately 5 seconds to finish.  Why?  Because the first 3 threads take up all the connections from the pool.  When the 4th thread tries to checkout a connection it waits up to <code>wait_timeout</code> (defaults to 5) seconds for a connection to become available.  If it can&#8217;t get any within that amount of time it will checkin connections for dead threads and then try to checkout again.  If you tried to do this with 7 threads you would get a &#8220;cannot obtain database connection error&#8221; because even after 5 seconds there wasn&#8217;t an available connection for the last thread.</p>
<p>As I showed in my previous posts you could solve this relatively easily by calling a cleanup method periodically or at the end of every thread.  But how about this example:</p>
<pre class="brush: ruby;">
6.times do
  Thread.new do
    User.find(:first)
    sleep 100 # or do something non ActiveRecord related for 100 seconds
  end
end
</pre>
<p>This one cannot be solved as easily.  None of the threads die quickly so there are no connections to reclaim.  This is where my patch comes in.  Even though our threads are alive for 100 seconds we are really only using a connection for a small % of their lifespan.  If your application fits this usage pattern then this patch is for you.</p>
<p><strong>How it works</strong><br />
First we have the <code>cleanup_connection</code> method.  Wrap any code with this method and it will ensure that your connection is checked back into the pool when the block completes:</p>
<pre class="brush: ruby;">
        def cleanup_connection
          return yield if Thread.current[:__AR__cleanup_connection]

          begin
            Thread.current[:__AR__cleanup_connection] = true
            yield
          ensure
            release_connection
            Thread.current[:__AR__cleanup_connection] = false
          end
        end
</pre>
<p>The thread locals are used to make sure the connection won&#8217;t be released on nested calls.  I will demonstrate the need for this protection in a second.  But for now, let&#8217;s fix our previous example:</p>
<pre class="brush: ruby;">
10.times do
  Thread.new do
    User.cleanup_connection do
      User.find(:first)
    end
    sleep 100
  end
end
</pre>
<p>Each thread will release its connection before the sleep and all the threads should complete their find statements very quickly.  But who wants to call <code>cleanup_connections</code> all over their code.  Luckily, you don&#8217;t have to.  My patch wraps all the necessary ActiveRecord internals with it instead of you having to wrap your code.  </p>
<p>The following example illustrates the need for nesting protection.  Keep in mind my patch has wrapped both <code>transaction</code> and <code>create</code> with <code>cleanup_connection</code>.</p>
<pre class="brush: ruby;">
User.transaction do
  User.create(:name =&gt; '1')
  User.create(:name =&gt; '2')
end
</pre>
<p>Without nesting protection the first <code>create</code> call would have released our connection after it completed.  Which means another thread could have checked it out in between or we could have gotten a different connection for the second call.  Either way, our transaction would have been messed up.  In short, nesting protection allows you to wrap the smallest amount of code necessary rather than requiring you wrap your code at a very high level.  This allows you to keep the connections checked out for as short a time as possible.</p>
<p><b>Performance</b><br />
There is of course a performance hit for all the added method calls and the cost of constantly checking out/in connections to the pool, but it&#8217;s not much.  On a query which takes 0.15 seconds to run here are the numbers:</p>
<pre class="brush: ruby;">
1000x queries
----------------
without patch:
19.23s
with patch:
19.82s
with patch where all 1000 calls are nested under a single cleanup_connection to prevent checkin/checkout for every call:
19.66s
</pre>
<p>So the penalty is about 3%.  The actual overhead penalty is about 30% (query of 0.001 seconds) so the faster your queries the closer you move toward this.</p>
<p><b>How to use it</b><br />
<a href="http://github.com/coderrr/cleanup_connection/blob/master/cleanup_connection_patch.rb">Get it here</a>.  It&#8217;s a monkey patch so require it anywhere after ActiveRecord has been loaded.</p>
<p>I&#8217;ve done my best to try to find all the ActiveRecord methods that need to be wrapped but it&#8217;s possible that I have missed some.  Because of this I monkey patch the connection method to raise an alert whenever it is called from outside of a <code>cleanup_connection</code> block.  If you see one of these, you can look through the stack trace, determine which method needs to be wrapped and add it to the <code>methods_to_wrap</code> hash.  After you are confident all necessary methods are patched you can remove the <code>connection</code> monkeypatch to speed things up a bit.</p>
<p><b>Rails core</b><br />
I&#8217;ve submitted a <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1752-added-using_connection-method-like-with_connection-but-sets-it-as-default-connection-while-inside-the-block">patch</a> to Rails for just the <code>cleanup_connection</code> portion.  I think it would be a nice addition to the already existing but not very useful <code>with_connection</code> method.  Feel free to comment or +/-1 the patch.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/491/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=491&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2009/01/16/monkey-patching-activerecord-to-automatically-release-connections/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby and mysqlplus select() deadlock</title>
		<link>http://coderrr.wordpress.com/2009/01/11/ruby-and-mysqlplus-select-deadlock/</link>
		<comments>http://coderrr.wordpress.com/2009/01/11/ruby-and-mysqlplus-select-deadlock/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 19:07:13 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[bug]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=456</guid>
		<description><![CDATA[After setting up mysqlplus in my Rails project I ran into an interpreter wide deadlock in certain situations.  I isolated it and tracked it down to two things&#8230;
&#8230; Before I continue the simple solution to this is to use the C implementation of async_query instead of the Ruby version.  

class Mysql
  alias_method [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=456&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>After setting up <a href="http://github.com/oldmoe/mysqlplus/tree/master">mysqlplus</a> in my Rails project I ran into an interpreter wide deadlock in certain situations.  I isolated it and tracked it down to two things&#8230;</p>
<p>&#8230; Before I continue the simple solution to this is to use the C implementation of <code>async_query</code> instead of the Ruby version.  </p>
<pre class="brush: ruby;">
class Mysql
  alias_method :query, :c_async_query
# instead of
  alias_method :query, :async_query
end
</pre>
<p>If you want more details read on&#8230;</p>
<p>1) Mysqlplus&#8217; default <code>async_query</code> which is implemented in ruby:</p>
<pre class="brush: ruby;">
  def async_query(sql, timeout = nil)
    send_query(sql)
    select [ (@sockets ||= {})[socket] ||= IO.new(socket) ], nil, nil, nil
    get_result
  end
</pre>
<p>The <code>send_query</code>, <code>get_result</code>, and <code>socket</code> methods are C functions.<br />
and<br />
2) <code>ActiveRecord::Base.clear_reloadable_connections!</code> which is called after every Rails request in development mode:</p>
<pre class="brush: ruby;">
# actually implemented in connection_pool.rb
      def clear_reloadable_connections!
        @reserved_connections.each do |name, conn|
          checkin conn
        end
        @reserved_connections = {}
        @connections.each do |conn|
          conn.disconnect! if conn.requires_reloading?
        end
        @connections = []
      end
</pre>
<p>The actual code we care about here is <code>conn.disconnect!</code> which will call mysqlplus&#8217; <code>disconnect</code> method which is implemented as a C function.  If we have Rails skip the call to <code>disconnect!</code>, no deadlock.  Or if we use the C implementation of <code>async_query</code>, which is named <code>c_async_query</code>, no deadlock.</p>
<p>The issue has something to do with calling Ruby&#8217;s <code>IO#select</code> on a file descriptor which you are manipulating with native functions.  While looking into it I found a separate but related issue.  The bug and fix I show below does not actually resolve the deadlock I was running into, but solves a similar one.  The mysqlplus deadlock actually does not occur during the <code>async_query</code>&#8217;s <code>IO#select</code> but at some later point which I couldn&#8217;t exactly determine.</p>
<p>The condition can be reproduced by calling the native C function <code>close()</code> on a file descriptor which Ruby is currently <code>IO#select</code>ing.</p>
<pre class="brush: ruby;">
require 'socket'
require 'rubygems'
require 'inline'

module C
  class &lt;&lt; self
    inline do |builder|
      builder.c %q{
        static VALUE native_close(int s) {
          close(s);
          return Qnil;
        }
      }
    end
  end
end

Thread.new { loop { sleep 1; p 1 } }

Thread.new do
  loop do
    io = TCPSocket.new('google.com', 80)
    fd = io.to_i
    Thread.new { sleep 0.5; C.native_close(fd) }
#   Thread.new { sleep 0.5; io.close }
    p :selecting!
    rdy = select [io]
    p :selected!
  end
end

sleep 99999
</pre>
<p>This example will produce a deadlock on <code>select</code> after the second thread calls <code>native_close</code>.  If you swap the <code>close</code> lines so that you are closing the socket with Ruby&#8217;s <code>close</code> method instead of the native one you won&#8217;t get a deadlock.  After lots of debugging I narrowed it to down to what seems to be a bug in Ruby&#8217;s <code>rb_thread_schedule</code> function:</p>
<pre class="brush: cpp;">
        n = select(max+1, &amp;readfds, &amp;writefds, &amp;exceptfds, delay_ptr);
        if (n &lt; 0) {
            // select is returning -1 indicating an error
            int e = errno;
</pre>
<p>The deadlock is actually Ruby calling <code>rb_thread_schedule</code> over and over for the thread which is <code>select</code>ing instead of deferring to let other threads run.  I stuffed in a call to <code>perror()</code> and saw that the error is caused by a bad file descriptor.  But for some reason Ruby doesn&#8217;t handle that error correctly by removing that fd from the <code>fd_set</code>.  So I fixed it by going through to determine if there are any bad file descriptors in the set and if so remove them:</p>
<p><b>Update</b>: Simplified <code>remove_bad_fds</code> function thanks to <a href="http://blog.costan.us/">costan</a> recommending <code>fcntl()</code> over <code>select()</code></p>
<pre class="brush: cpp;">
        n = select(max+1, &amp;readfds, &amp;writefds, &amp;exceptfds, delay_ptr);
        if (n &lt; 0) {
            int e = errno;
// ...
            if (e == EBADF)
              remove_bad_fds(&amp;th-&gt;readfds, &amp;th-&gt;writefds, &amp;th-&gt;exceptfds, max);

// ...

#include &lt;fcntl.h&gt;
static void
remove_bad_fds(fd_set *r, fd_set *w, fd_set *e, int max) {
  int fd;

  for (fd = 0; fd &lt;= max; fd++)
    if (FD_ISSET(fd, r) || FD_ISSET(fd, w) || FD_ISSET(fd, e))
      if (fcntl(fd, F_GETFD) &lt; 0 &amp;&amp; errno == EBADF) {
        FD_CLR(fd, r);
        FD_CLR(fd, w);
        FD_CLR(fd, e);
      }
}
</pre>
<p>The <code>remove_bad_fds</code> calls <code>fcntl</code> for each fd from the sets to determine if it is bad.  If so, it is removed.</p>
<p><a href="http://redmine.ruby-lang.org/issues/show/1001">bug filed</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/456/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/456/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/456/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=456&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2009/01/11/ruby-and-mysqlplus-select-deadlock/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>IO#readpartial now works in JRuby</title>
		<link>http://coderrr.wordpress.com/2009/01/07/ioreadpartial-now-works-in-jruby/</link>
		<comments>http://coderrr.wordpress.com/2009/01/07/ioreadpartial-now-works-in-jruby/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 21:33:33 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[bug]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=423</guid>
		<description><![CDATA[I was doing some concurrency benchmarks on various Rubys and I ran into a pretty nasty bug with IO#readpartial in JRuby.  On EOF it would just return an empty string forever, instead of raising an EOFError as it should.  I&#8217;m pretty surprised no one ran into this yet.
Since I thought this was pretty [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=423&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I was doing some concurrency benchmarks on various Rubys and I ran into a pretty nasty bug with IO#readpartial in JRuby.  On EOF it would just return an empty string forever, instead of raising an EOFError as it should.  I&#8217;m pretty surprised no one ran into this yet.</p>
<p>Since I thought this was pretty important I submitted a <a href="http://jira.codehaus.org/browse/JRUBY-3276">patch</a>, they took notice, did some refactoring of their IO methods, and now it passes all of the readpartial rubyspecs.  So you can expect a fully functioning IO#readpartial in JRuby 1.1.7.  Good job JRuby guys!</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/423/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/423/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/423/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=423&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2009/01/07/ioreadpartial-now-works-in-jruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>Singleton recursion bug</title>
		<link>http://coderrr.wordpress.com/2008/12/23/singleton-recursion-bug/</link>
		<comments>http://coderrr.wordpress.com/2008/12/23/singleton-recursion-bug/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 20:56:51 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[bug]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=398</guid>
		<description><![CDATA[What would you expect the following code to do?

require 'singleton'
class X
  include Singleton
  def initialize
    X.instance
  end
end
X.instance

Stack overflow right?  Wrong!  In Ruby 1.8 it&#8217;ll hang forever because of the way thread-safety is handled with a while loop.

    def _instantiate?()
      [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=398&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>What would you expect the following code to do?</p>
<pre class="brush: ruby;">
require 'singleton'
class X
  include Singleton
  def initialize
    X.instance
  end
end
X.instance
</pre>
<p>Stack overflow right?  Wrong!  In Ruby 1.8 it&#8217;ll hang forever because of the way thread-safety is handled with a while loop.</p>
<pre class="brush: ruby;">
    def _instantiate?()
      while false.equal?(@__instance__)
        Thread.critical = false
        sleep(0.08)   # timeout
        Thread.critical = true
      end
      @__instance__
   end
</pre>
<p>Basically the first call to <code>X.instance</code> sets <code>@__instance__</code> to <code>false</code> while it calls the <code>initialize</code> method to make sure other threads don&#8217;t call it at the same time.  When <code>initialize</code> calls <code>X.instance</code> again it enters that while loop and sits there forever.  This can make debugging a little tricky in a complex app where you might accidentally put a recursive call to instance 5 methods down the line and wonder why the hell your app isn&#8217;t doing anything.  So watch out for this if you use <code>Singleton</code>.</p>
<p>If you wanna be really safe you can use my <a href="http://github.com/coderrr/singleton_hang/tree/master/singleton_safe.rb">patched version</a> which checks for recursive calls and raises an error.  Or you can just throw this monkey patch somewhere in your app which prints a warning after it&#8217;s hanged for 5 seconds:</p>
<pre class="brush: ruby;">
require 'singleton'
require 'timeout'

class &lt;&lt; Singleton
  module SingletonClassMethods
    private

    def _instantiate?
      start_time = Time.now
      while false.equal?(@__instance__)
        Thread.critical = false
        sleep(0.08) # timeout
        Thread.critical = true
        if Time.now - start_time &gt; 5
          @__once__ ||= ! $stderr.puts(&quot;Possible recursive call to instance in initialize&quot;, caller)
        end
      end
      @__instance__
    end
  end
end
</pre>
<p>In Ruby 1.9 Singleton is rewritten using a Mutex so you&#8217;ll get a nice deadlock message instead of the hang.  In JRuby they use a Mutex too, but you won&#8217;t get any message about the deadlock.  So effectively it will act the same as 1.8.  My monkey patch won&#8217;t work for JRuby but the full patched version which I linked to on github will.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/398/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/398/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/398/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/398/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/398/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/398/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/398/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/398/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/398/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/398/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=398&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2008/12/23/singleton-recursion-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>Contributing to open source can be hard and frustrating</title>
		<link>http://coderrr.wordpress.com/2008/12/04/contributing-to-open-source-can-be-hard-and-frustrating/</link>
		<comments>http://coderrr.wordpress.com/2008/12/04/contributing-to-open-source-can-be-hard-and-frustrating/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 19:47:52 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[bug]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=318</guid>
		<description><![CDATA[Has anyone ever told you that you should try contributing to some open source projects?  Maybe they said it as advice for improving your resume or maybe they offered it as a solution to boredom.  Either way, contributing to open source isn&#8217;t always as easy as it sounds.
Disclaimer: I&#8217;m in no way bitching [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=318&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Has anyone ever told you that you should try contributing to some open source projects?  Maybe they said it as advice for improving your resume or maybe they offered it as a solution to boredom.  Either way, contributing to open source isn&#8217;t always as easy as it sounds.</p>
<p><b>Disclaimer: I&#8217;m in no way bitching or complaining about the following projects/people.  These are just some past experiences I wanted to share.</b></p>
<p>Here&#8217;s a list of a few open source projects I&#8217;ve written (useful) patches for which haven&#8217;t been merged into the original project.</p>
<p><strong>First up: Hpricot</strong><br />
As documented by <a href="http://coderrr.wordpress.com/2007/09/14/hpricot-patch-to-support-arbitrarily-large-elements/">this post</a>, over a year ago I patched the Hpricot C <b>and</b> Java extensions to support arbitrarily large HTML elements.  Instead of throwing an error after a certain buffer size had been hit, it would increase the memory allocation size.  I did so because I was writing software for someone which did some website crawling and could potentially (and actually did) run into very large elements on the web (which crashed hpricot or threw an error).  So it had a real world use case, judging by the responses I&#8217;ve gotten since I wrote it quite a few other people have had uses for it, and it just made sense.  (Oh, it also didn&#8217;t affect performance)</p>
<p>But to this day, over a year later, I haven&#8217;t been able to get _why to merge it into hpricot core.  I created a patch, made sure all the tests passed, sent an email to the mailing list, got a ticket filed on their trac.  I&#8217;ve even rewritten the patch once to make it work with his latest code on github.  I even forked his repo, did a pull request, and messaged him.  He even responded saying he&#8217;d get to it soon.  Over a month later, still nothing.  Now I understand the reality is that he&#8217;s just really busy, but my reality is that it really sucks.  Also, PLEASE, don&#8217;t take this as bitching, I&#8217;m just telling a story, I have total love for _why.<br />
<b>Update</b>: _why just merged my patch, yes!! :P</p>
<p><strong>Number two:</strong><br />
<del datetime="2009-06-18T17:03:17+00:00">I was using the seattle.rb project ImageScience to thumbnail images for one of my client&#8217;s apps.  It crashed on BMPs, and since BMPs were one of my client&#8217;s requirements, I made a (tiny) <a href="http://coderrr.wordpress.com/2007/11/15/imagescience-bmp-file-fix/">patch</a> to their C extension to get it working.  Again, all the tests passed, I created a ticket, harassed some people on IRC, etc.  Over a year later, nothing.  I think someone even closed the bug without giving reason.  Again, judging from the number of downloads of the patched gem, people found real use in it (I&#8217;m trying to save myself from everyone thinking I write a bunch of useless patches).</del></p>
<p><strong>Next up, Rails:</strong><br />
This was when Rails was just beginning their mission of &#8220;let&#8217;s be thread safe&#8221;.  I really enjoy playing in the spaghetti of threaded programming so I decided to see if I could help and find some issues to patch.  I found a race condition in their processing of partials and wrote a <a href="http://dev.rubyonrails.org/ticket/11540">patch</a> to fix it, along with a brute force test to produce the race condition.  I posted it to the mailing list and a <a href="http://www.ruby-forum.com/topic/149301">discussion</a> ensued.  The gist of it was that &#8220;we don&#8217;t like mutexes&#8221;, and my patch was never accepted, and never got any +1s.  Well guess what they ended up doing eventually before rails was considered thread-safe&#8230; they added a mutex -_-.  Needless to say, this turned me off to contributing to Rails.  (In the latest version they actually did make some changes which render the mutex unnecessary but that is irrelevant for this story).  Again please note, I&#8217;m not trying to bitch, I have nothing but love for the Rails guys, this is just my story.</p>
<p><strong>Finally:</strong><br />
For one of my contracts I <a href="http://coderrr.wordpress.com/2007/10/15/patch-to-firewatir-and-jssh-to-support-testing-with-multiple-concurrent-firefox-browsers/">reworked</a> the FireWatir (automated UI testing) code to be thread-safe so that you could control multiple browsers at the same time in different threads.  Something which is <b>extremely</b> helpful when you are trying to simulate multiple users simultaneously.  Loads of people have used my patch to aid in their FireWatir testing.  It passed the entire test suite.  But even after multiple emails to the maintainer of the project&#8217;s repository (who actually used my patch in his projects) for some reason it never got merged.</p>
<p>So what&#8217;s the conclusion from all these stories?  Even after doing all the &#8220;right things&#8221; like making sure all the tests pass, ensuring performance isn&#8217;t affected, contacting the appropriate people through the appropriate channels I&#8217;ve had many patches go unloved.  It&#8217;s kind of disheartening but I think what I&#8217;m learning is that you just have to be relentlessly persistent.  Most people are just really busy and require a lot of external motivation if you want them to do things (which in the grand scheme of things are quite unimportant) for you.  Of course at the same time you don&#8217;t wanna bug the shit out of someone by emailing them all the time, so&#8230;</p>
<p>I wonder if anyone has similar experiences or any advice on this sort of thing&#8230;</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/318/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=318&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2008/12/04/contributing-to-open-source-can-be-hard-and-frustrating/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>HTMLEntities race condition</title>
		<link>http://coderrr.wordpress.com/2008/12/02/htmlentities-race-condition/</link>
		<comments>http://coderrr.wordpress.com/2008/12/02/htmlentities-race-condition/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 20:37:11 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[bug]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=310</guid>
		<description><![CDATA[Ever ran into this error?

/usr/lib/ruby/gems/1.8/gems/htmlentities-4.0.0/lib/htmlentities.rb:110:in `map': uninitialized constant HTMLEntities::MAPPINGS (NameError)

or this?

/usr/lib/ruby/gems/1.8/gems/htmlentities-4.0.0/lib/htmlentities.rb:141:in `reverse_map': undefined method `invert' for nil:NilClass (NoMethodError)

Well you might have if you&#8217;re using the HTMLEntities gem in threaded ruby code.  And if you are but haven&#8217;t gotten one of those errors, they might just be waiting for the stars to align correctly before [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=310&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Ever ran into this error?</p>
<pre class="brush: ruby;">
/usr/lib/ruby/gems/1.8/gems/htmlentities-4.0.0/lib/htmlentities.rb:110:in `map': uninitialized constant HTMLEntities::MAPPINGS (NameError)
</pre>
<p>or this?</p>
<pre class="brush: ruby;">
/usr/lib/ruby/gems/1.8/gems/htmlentities-4.0.0/lib/htmlentities.rb:141:in `reverse_map': undefined method `invert' for nil:NilClass (NoMethodError)
</pre>
<p>Well you might have if you&#8217;re using the HTMLEntities gem in threaded ruby code.  And if you are but haven&#8217;t gotten one of those errors, they might just be waiting for the stars to align correctly before they manifest themselves.</p>
<p>The errors are due to a race condition in the code:</p>
<pre class="brush: ruby;">
  def map
    @map ||= (require &quot;htmlentities/#{@flavor}&quot;; HTMLEntities::MAPPINGS[@flavor])
  end
</pre>
<p>The issue arises in the following scenario:<br />
Thread1 calls require and begins processing the required file, thread2 calls require which returns immediately because ruby has already marked the file as required even though it hasn&#8217;t finished loading.  So then when thread2 tries to access HTMLEntities::MAPPINGS it is either undefined or empty because  thread1 hasn&#8217;t actually finished evaling the required file yet.</p>
<p>Quick monkeypatch to fix the issue:</p>
<pre class="brush: ruby;">
require 'rubygems'
require 'htmlentities'
class HTMLEntities
  alias_method :unsafe_map, :map
  @@map_guard = Mutex.new
  def map
    @@map_guard.synchronize { unsafe_map }
  end
end
</pre>
<p>You could also require all the htmlentities/* files at the beginning of your application but monkeypatching is so much more fun :)</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/310/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=310&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2008/12/02/htmlentities-race-condition/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
		<item>
		<title>Making mixico thread safe</title>
		<link>http://coderrr.wordpress.com/2008/11/14/making-mixico-thread-safe/</link>
		<comments>http://coderrr.wordpress.com/2008/11/14/making-mixico-thread-safe/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 23:57:01 +0000</pubDate>
		<dc:creator>coderrr</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coderrr.wordpress.com/?p=259</guid>
		<description><![CDATA[There is this cool library by _why called Mixico.  Its main purpose (I think) is to get around the issue of instance_eval replacing self, which makes it (instance_eval) kinda sucky to use in DSLs.  Example:

def x &#38;blk
  SomeProxyClass.new(self).instance_eval(&#38;blk)
end

@i = 5
x do
  p @i # =&#62; NOT 5
end

@i won&#8217;t be 5 in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=259&subd=coderrr&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>There is this cool library by _why called <a href="http://hackety.org/2008/10/06/mixingOurWayOutOfInstanceEval.html">Mixico</a>.  Its main purpose (I think) is to get around the issue of instance_eval replacing self, which makes it (instance_eval) kinda sucky to use in DSLs.  Example:</p>
<pre class="brush: ruby;">
def x &amp;blk
  SomeProxyClass.new(self).instance_eval(&amp;blk)
end

@i = 5
x do
  p @i # =&gt; NOT 5
end
</pre>
<p>@i won&#8217;t be 5 in the above example because @i will be searched for in <code>SomeProxyClass.new</code>, not in the <code>self</code> in which the block was created, due to the fact instance_eval hijacks <code>self</code>.  Mixico helps you get around this.</p>
<pre class="brush: ruby;">
require 'mixico'

def x &amp;blk
  Module.mix_eval(SomeProxyModule, &amp;blk)
end

@i = 5
x do
  p @i # =&gt; 5 !!
end
</pre>
<p>Mixico will make all the methods in the proxy module available to your block, but it won&#8217;t hijack self.  Pretty sweet deal!  Except it&#8217;s not thread safe =[.</p>
<p>Why not?  Well essentially what it does is call <code>self.extend SomeProxyModule</code>.  Now we could do this without mixico and all its magic.  But what mixico does allow us to do is <i>un</i>extend the module after the block is finished.  This required some C extensions.  So the problem is that extending an object or a class is not a thread local operation.  It will affect any thread dealing with that object or class.  So say you had two mix_eval&#8217;s going on in different threads on the same object at the same time&#8230; fail.</p>
<p>So at first I was like, yea that&#8217;s too bad.  But then I thought of a clever way to fix it.  Instead of extending <code>self</code> we <code><a href="http://ruby-doc.org/core/classes/Object.html#M000350">dup</a></code> it and then extend the dup.  But now we wan&#8217;t to run the block in the context of the duped self, so how do we do that?  We call <code>instance_eval</code> on the duped self.  But wait, doesn&#8217;t <code>instance_eval</code> hijack self and that&#8217;s bad?  Well yes, but this time we&#8217;re hijacking self and setting it to the dup&#8217;ed self which is almost exactly the same thing.</p>
<p>But not exactly.  The dup&#8217;ed self will have different instance var and method tables.  So you&#8217;ll be able to access all the methods and instance vars of the original but if you were to modify them the changes wouldn&#8217;t persist outside the block.  So how do we fix this?  We change the dup&#8217;ed self&#8217;s <code>iv_tbl</code> and <code>m_tbl</code> pointers to point to the original self&#8217;s tables (<a href="http://github.com/coderrr/mixico-inline/tree/master/lib/mixico.rb#L57">C hax</a>).  This way any modification of ivars or methods will actually be on the original, even though we have a distinct class hierarchy which includes our temporarily mixed in module.</p>
<p>Here&#8217;s the thread safe version of mix_eval:</p>
<pre class="brush: ruby;">
  def safe_mix_eval mod, &amp;blk
    duped_context = blk.context.dup
    # make sure the singleton class is in existence
    class &lt;&lt; duped_context; self; end

    duped_context.redirect_tbls(blk.context)

    duped_context.extend mod
    begin
      m = duped_context.is_a?(Module) ? :class_eval : :instance_eval
      duped_context.send(m, &amp;blk)
    ensure
      duped_context.restore_tbls
      (class &lt;&lt; duped_context; self; end).disable_mixin mod
    end
  end
</pre>
<p>This will even handle the special case of adding methods to a class, which needs class_eval instead of instance_eval.  What this still doesn&#8217;t handle is if you were to try to do something like this:</p>
<pre class="brush: ruby;">
class X
  Module.safe_mix_eval Module.new do
    class &lt;&lt; self
      def x; :x; end
    end
  end
end
X.x # no method error!
</pre>
<p>This wouldn&#8217;t work because the <code>class &lt;&lt; self</code> refers to the singleton class of the dup of X not the singelton class of X itself.  Therefore we add the singleton method to the wrong class and it doesn&#8217;t persist.  I think this case is probably rare, but maybe not.  I doubt many people use mixico anyway so whatever :P</p>
<p><a href="http://github.com/coderrr/mixico-inline/tree/master/test/benchmarks.rb">Benchmarks</a> currently show the thread safe version being about twice as slow as the original.</p>
<p>You can check out the thread safety test case <a href="http://github.com/coderrr/mixico-inline/tree/master/test/thread_safety_test.rb">here</a>.  Or you can check out the rest of the code and get the gem at <a href="http://github.com/coderrr/mixico-inline">github.com/coderrr/mixico-inline</a>.</p>
<p>Props to <a href="http://banisterfiend.wordpress.com/">http://banisterfiend.wordpress.com/</a> for working through this fix with me.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderrr.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderrr.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderrr.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderrr.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderrr.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderrr.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderrr.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderrr.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderrr.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderrr.wordpress.com/259/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderrr.wordpress.com&blog=1719123&post=259&subd=coderrr&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://coderrr.wordpress.com/2008/11/14/making-mixico-thread-safe/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/af4ce9309f8c4f7fc5cb33e7a5b08c64?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">coderrr</media:title>
		</media:content>
	</item>
	</channel>
</rss>