ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:89625] [Ruby trunk Bug#15262] GCing of object in use
Date: Tue, 30 Oct 2018 01:34:49 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-74663.20181030013448.952d90539229e787@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15262.20181027171221@ruby-lang.org

Issue #15262 has been updated by nobu (Nobuyoshi Nakada).


larskanis (Lars Kanis) wrote:
> > It waits just once.
> 
> Yes, but the one event is sent after all 10 threads have been called, so that `@count` is decremented to 0.

Why do you think so?

----------------------------------------
Bug #15262: GCing of object in use
https://bugs.ruby-lang.org/issues/15262#change-74663

* Author: larskanis (Lars Kanis)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.0dev (2018-10-27 trunk 65390) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
Given the following program:

```ruby
require "weakref"

Thread.abort_on_exception = true

class Adder
  def self.start_adder(obj)
    obj.add
  end

  def initialize
    @qu = Queue.new
    @mutex = Mutex.new
    @mutex.lock
    begin
      @count = 10
      @count.times do
        Thread.new(WeakRef.new(self), &self.class.method(:start_adder))
      end
    ensure
      @mutex.unlock
    end
    @qu.deq
  end

  def add
    @mutex.lock
    begin
      @count-=1
      if @count == 0
        @qu.enq true
      elsif @count < 0
        raise "shouldn't happen"
      end
    ensure
      @mutex.unlock
    end
  end
end

def test_adder
  10.times.map do
    Thread.new do
      Adder.new
    end
  end.each(&:join)
end

100.times do
  test_adder
end
```

## Expected behaviour:

The program should simply execute without error. This is the case on JRuby but not on MRI.

## Actual behavior:

The program stops with a probability of approximately 80% with the following error:
```
$ ruby -W2 adder-test.rb 
#<Thread:0x0000562d5089fea8@adder-test.rb:6 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
adder-test.rb:7:in `start_adder': Invalid Reference - probably recycled (WeakRef::RefError)
Traceback (most recent call last):
adder-test.rb:7:in `start_adder': Invalid Reference - probably recycled (WeakRef::RefError)
```

Although `start_adder` works with a `WeakRef`, the `Adder` object should still be GC marked, since `Adder.new` doesn't return before all calls to `start_adder` finished.

I verified this on ruby-trunk, but get mostly the same behavior on all older MRI versions.


---Files--------------------------------
adder-test2.rb (476 Bytes)


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

  parent reply	other threads:[~2018-10-30  1:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15262.20181027171221@ruby-lang.org>
2018-10-27 17:12 ` [ruby-core:89579] [Ruby trunk Bug#15262] GCing of object in use lars
2018-10-28 10:39 ` [ruby-core:89602] [Ruby trunk Bug#15262][Feedback] " nobu
2018-10-28 11:52 ` [ruby-core:89606] [Ruby trunk Bug#15262] " lars
2018-10-29  1:13 ` [ruby-core:89610] " nobu
2018-10-29  9:00 ` [ruby-core:89617] " lars
2018-10-30  1:34 ` nobu [this message]
2018-10-30 15:08 ` [ruby-core:89643] [Ruby trunk Bug#15262] WeakRef::RefError for object that is still " lars
2018-10-30 16:16 ` [ruby-core:89646] " merch-redmine
2018-10-31 10:07 ` [ruby-core:89658] " lars
2020-01-14 11:13 ` [ruby-core:96852] [Ruby master " lars

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-74663.20181030013448.952d90539229e787@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).