From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.9 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id D15221F453 for ; Mon, 29 Oct 2018 09:01:13 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 83E1B120EFD; Mon, 29 Oct 2018 18:01:11 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 764A0120E41 for ; Mon, 29 Oct 2018 18:01:09 +0900 (JST) Received: by filter0147p3mdw1.sendgrid.net with SMTP id filter0147p3mdw1-8195-5BD6CC30-35 2018-10-29 09:00:32.490309618 +0000 UTC m=+906159.095873600 Received: from herokuapp.com (ec2-54-163-50-253.compute-1.amazonaws.com [54.163.50.253]) by ismtpd0005p1iad1.sendgrid.net (SG) with ESMTP id xZcdlNGDSXWHp6wAMAC2xg Mon, 29 Oct 2018 09:00:32.524 +0000 (UTC) Date: Mon, 29 Oct 2018 09:00:32 +0000 (UTC) From: lars@greiz-reinsdorf.de To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 65001 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15262 X-Redmine-Issue-Author: larskanis X-Redmine-Sender: larskanis X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS7D0qGlV1Wwapf79YmGNVyyBovORlt0grT2Ki 5K/hZqJvsizHwm2fbaTsMgAYSDY1CS2tjBU754XYj+delro/G/jxQTll61FHImixwP0txo4x0XbGIu u9sTl9miNgykldOf3vixsMWCw1SEGDB+gAoub1/Pakb6v0P8Zz9sfMvxlQ== X-ML-Name: ruby-core X-Mail-Count: 89617 Subject: [ruby-core:89617] [Ruby trunk Bug#15262] GCing of object in use X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" 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/