From: lars@... Date: 2018-10-29T09:00:32+00:00 Subject: [ruby-core:89617] [Ruby trunk Bug#15262] GCing of object in use Issue #15262 has been updated by larskanis (Lars Kanis). File adder-test2.rb added > 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. The same error is present, when we wait for 10 calls to the queue. So the class can actually be simplified like so: ```ruby class Adder def self.start_adder(obj) obj.add end def initialize @qu = Queue.new count = 10 count.times do Thread.new(WeakRef.new(self), &self.class.method(:start_adder)) end count.times do @qu.deq end end def add @qu.enq true end end ``` This version raises `Invalid Reference - probably recycled (WeakRef::RefError)` with a probability of around 50% on my Linux systems. The whole file is attached. ---------------------------------------- Bug #15262: GCing of object in use https://bugs.ruby-lang.org/issues/15262#change-74655 * 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 # 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/ Unsubscribe: