2010/5/4 Aaron Patterson <aaron / tenderlovemaking.com>:
> I think this is a duplicate of [ruby-core:29874].  I've been able to get
> the same stack trace with this program:
>
>    GC.stress = true
>    require 'nokogiri'
>
> It seems to only happen when requiring gems.


Thank you for your narrowing down the bug!
I believe that the following patch works well:

diff --git a/string.c b/string.c
index caca178..dea5339 100644
--- a/string.c
+++ b/string.c
@@ -613,8 +613,8 @@ str_replace_shared(VALUE str2, VALUE str)
 	STR_SET_EMBED_LEN(str2, RSTRING_LEN(str));
     }
     else {
-	FL_SET(str2, STR_NOEMBED);
 	str = rb_str_new_frozen(str);
+	FL_SET(str2, STR_NOEMBED);
 	RSTRING(str2)->as.heap.len = RSTRING_LEN(str);
 	RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str);
 	RSTRING(str2)->as.heap.aux.shared = str;

-- 
Yusuke Endoh <mame / tsg.ne.jp>