ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "alexdowad (Alex Dowad)" <alexinbeijing@gmail.com>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:47904] [ruby-trunk - Bug #7135] GC bug in Ruby 1.9.3-p194?
Date: Thu, 11 Oct 2012 10:53:44 +0900	[thread overview]
Message-ID: <redmine.journal-30229.20121011105343@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-7135.20121010222810@ruby-lang.org


Issue #7135 has been updated by alexdowad (Alex Dowad).


> +    RB_GC_GUARD(str);

This also fixes the problem. I looked on GitHub, and it looks like this patch hasn't been applied to the newest version of the Ruby source... I'll submit a pull request.

Thanks to @nobu and @normalperson for your help!!!
----------------------------------------
Bug #7135: GC bug in Ruby 1.9.3-p194?
https://bugs.ruby-lang.org/issues/7135#change-30229

Author: alexdowad (Alex Dowad)
Status: Feedback
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]


I'm just doing some refactoring/performance work on a popular Ruby gem called Prawn (it's used for PDF generation). I'm fighting with a strange, intermittent failure on the spec tests, and from my experimentation so far, it seems very, very likely to be a bug in Ruby's garbage collector.

I'll try to keep this as brief as possible, but please be patient...

The code where the intermittent failure comes from measures the width of a string when rendered using a TTF font. This is part of the method body (including my debug print statements):


          # GC.disable
          # string.freeze
          p string.bytes.to_a if $my_debug
          p string.codepoints.to_a if $my_debug
          p scale if $my_debug
          result = string.codepoints.inject(0) do |s,r|
            print r if $my_debug
            print "," if $my_debug
            s + character_width_by_code(r)
          end * scale
          puts if $my_debug
          result

When the tests pass normally (which is about 7/8 of the time), the debug print statements show:

    [104, 101, 108, 108, 111, 194, 173]
    [104, 101, 108, 108, 111, 173]
    0.012
    104,101,108,108,111,173,

...You can see that the "print" calls in the "string.codepoints.inject" loop print the same series of codepoints as "p string.codepoints.to_a". This is what you would expect, because nothing is modifying the string. But about 1/8 of the time I get:

    [104, 101, 108, 108, 111, 194, 173]
    [104, 101, 108, 108, 111, 173]
    0.012
    104,42,0,0,0,0,0,

I have also seen "104,0,0,0,0,0" on occasion. In all cases, "p string.codepoints.to_a" prints the correct sequence of codepoints for the string.

You might think that something in the "string.codepoints.inject" loop is modifying the string, but it's not. I could show the contents of "character_width_by_code", but it would just be wasting your time, because it basically contains nothing but a couple of hash lookups.

If I uncomment "string.freeze", I can run the tests 100 times or more with no failure. (This proves that the string is not being modified by my code, because it would throw an exception otherwise!) Or, if I change the code to "string.codepoints.to_a.inject", again, the failure never happens. Most revealingly, if I uncomment "GC.disable", I can run the test 100 or more times with no failure. As soon as I comment out "GC.disable", the random failure comes back, for about 1/7 - 1/10 of runs.

Sometimes I also get another random failure from the same place: an "invalid codepoint in UTF-8" exception from "string.codepoints.inject". Again, this proves something inside Ruby is corrupting the string, because the call to "string.codepoints" just 2 lines before prints the correct sequence, with no exception raised.

I'd like to boil this down to a smaller example which demonstrates the failure, but it's a hopeless task. When I take pieces of the code and run them in irb, the failure never happens. Even rebooting the computer may make it go away... but then, when I am working on Prawn again, sooner or later it happens again. (I know because similar intermittent failures have happened before in the past.) Once it starts happening, though, it's pretty consistent at about 1/7 - 1/10 of test runs. Another clue is that the corrupted codepoints are *always* zero.

I can try to track down the problem, perhaps by adding some logging code to the Ruby interpreter source and recompiling, but I need some guidance on where to look. Can anyone who is familiar with Ruby internals (especially Strings and the GC) give me some ideas how to start?


-- 
http://bugs.ruby-lang.org/

  parent reply	other threads:[~2012-10-11  1:45 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-10 13:28 [ruby-core:47881] [ruby-trunk - Bug #7135][Open] GC bug in Ruby 1.9.3-p194? alexdowad (Alex Dowad)
2012-10-10 14:39 ` [ruby-core:47886] [ruby-trunk - Bug #7135][Feedback] " nobu (Nobuyoshi Nakada)
2012-10-10 20:12 ` [ruby-core:47890] [ruby-trunk - Bug #7135] " alexdowad (Alex Dowad)
2012-10-10 21:07   ` [ruby-core:47891] " Eric Wong
2012-10-10 21:50 ` [ruby-core:47892] " alexdowad (Alex Dowad)
2012-10-10 22:50   ` [ruby-core:47895] " Eric Wong
2012-10-10 22:00 ` [ruby-core:47893] " alexdowad (Alex Dowad)
2012-10-10 22:03 ` [ruby-core:47894] " alexdowad (Alex Dowad)
2012-10-10 23:05 ` [ruby-core:47896] " alexdowad (Alex Dowad)
2012-10-10 23:32 ` [ruby-core:47897] " alexdowad (Alex Dowad)
2012-10-11  1:13   ` [ruby-core:47899] " Nobuyoshi Nakada
2012-10-11  1:08 ` [ruby-core:47898] " alexdowad (Alex Dowad)
2012-10-11  1:27   ` [ruby-core:47900] " Nobuyoshi Nakada
2012-10-11  1:30 ` [ruby-core:47901] " alexdowad (Alex Dowad)
2012-10-11  1:33 ` [ruby-core:47902] " alexdowad (Alex Dowad)
2012-10-11  1:44   ` [ruby-core:47903] " Nobuyoshi Nakada
2012-10-11  1:53 ` alexdowad (Alex Dowad) [this message]
2012-10-11  2:03 ` [ruby-core:47905] " alexdowad (Alex Dowad)
2012-10-11  2:27   ` [ruby-core:47906] " Nobuyoshi Nakada
2012-10-11  7:20 ` [ruby-core:47907] " alexdowad (Alex Dowad)
2012-10-11  8:19 ` [ruby-core:47908] " alexdowad (Alex Dowad)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-30229.20121011105343@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).