From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.7 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 59B871F454 for ; Mon, 4 Nov 2019 21:49:20 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id E34CF1208FB; Tue, 5 Nov 2019 06:49:09 +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 B956D1208D0 for ; Tue, 5 Nov 2019 06:49:07 +0900 (JST) Received: by filter0121p3las1.sendgrid.net with SMTP id filter0121p3las1-18309-5DC09CD6-86 2019-11-04 21:49:10.92787137 +0000 UTC m=+9995.125002912 Received: from herokuapp.com (unknown [3.88.230.70]) by ismtpd0058p1iad1.sendgrid.net (SG) with ESMTP id PWB7rSQSSM-VbDSEjrXbDw for ; Mon, 04 Nov 2019 21:49:10.829 +0000 (UTC) Date: Mon, 04 Nov 2019 21:49:11 +0000 (UTC) From: davidnwelton@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71274 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16288 X-Redmine-Issue-Author: davidw X-Redmine-Sender: davidw 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: =?us-ascii?Q?9w824K3b0SHUB398CXnSgxNNt5RmCpnG5xjUB9l+wgQ5bpeC0syzWjwvCtQnQh?= =?us-ascii?Q?n+Ltr=2FhldRNufREx6oL8c5YLJc1HXen+5zHX7=2Fy?= =?us-ascii?Q?Z5gpcK9ejt0V+=2FbPx3VdVYPf7ejI6ftuMViqzCV?= =?us-ascii?Q?9rElfqUI4WE5uVGFLXm87OtuNLHK28orevRB9yR?= =?us-ascii?Q?W3ure2SDnOeUZH3DPLNz+bNHK6Xc1zXz+vw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95682 Subject: [ruby-core:95682] [Ruby master Bug#16288] Segmentation fault with finalizers, threads 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 #16288 has been updated by davidw (David Welton). ``` modified lib/timeout.rb @@ -94,7 +94,7 @@ def timeout(sec, klass = nil, message = nil) #:yield: +sec+ ensure if y y.kill - y.join # make sure y is dead. + # y.join # make sure y is dead. end end end ``` This also stops the segfault from happening. In my test app, the call to timeout seems to be coming from the Mongo gem: ``` def connect! Timeout.timeout(options[:connect_timeout], Error::SocketTimeoutError) do socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) handle_errors { socket.connect(::Socket.pack_sockaddr_in(port, host)) } self end end ``` When I comment out that join, I get the same error message as with my simple example I pasted in the original report: ``` Exception `ThreadError' at /home/davidw/.rvm/gems/ruby-2.6.5/gems/concurrent-ruby-1.1.5/lib/concurrent/atomic/ruby_thread_local_var.rb:87 - can't alloc thread rb_gc_call_finalizer_at_exit ... done ``` ---------------------------------------- Bug #16288: Segmentation fault with finalizers, threads https://bugs.ruby-lang.org/issues/16288#change-82469 * Author: davidw (David Welton) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.6.6p116 (2019-10-02 revision 67825) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- Hi, This is a tricky one and I am still working on narrowing it down, but I will report what I have so far. I compiled a version of 2_6_6 from github: ruby 2.6.6p116 (2019-10-02 revision 67825) [x86_64-linux] I have a minimal Rails project that uses Mongoid. It crashes with a segmentation fault when rspec runs. The concurrent ruby gem is in some way involved, and I have been posting there: https://github.com/ruby-concurrency/concurrent-ruby/issues/808 However, I think there is a deeper problem - I would not expect a user level script to cause a segmentation fault. I have been putting a lot of debugging statements in, and turned on Thread.DEBUG, and have noticed some things. I am not experienced with Ruby's internals, so some of these bits of data might be normal or irrelevant: * The concurrent-ruby gem uses ObjectSpace.define_finalizer to set a finalizer * That finalizer creates a new Thread * However, it appears as if that thread is running after the main thread is already dead, so code that expects to reference the main thread crashes, because it's a NULL reference. I tried the following test code: ``` class Foo def initialize ObjectSpace.define_finalizer(self, proc do Foo.foo_finalizer end) end def bar puts 'bar' end def Foo.foo_finalizer puts "foo_finalizer" t = Thread.new do puts "Thread reporting for duty" end puts "foo_finalizer thread launched" sleep 5 end end f = Foo.new f.bar f = nil ``` While trying to develop a simple test case to demonstrate the problem. It triggers rb_raise(rb_eThreadError, "can't alloc thread"); in thread_s_new, because it looks like the main thread has already been marked as 'killed' in this case. When I check the main thread status in thread_s_new with the above code, it reports 'dead'. When I run my rspec code in the sample Rails project, thread_s_new shows the main thread's status as 'run' even if it should be dead? I have seen some debugging things that shows some exceptions and thread_join interrupts and so on. Is it possible that something like this is happening? Main thread starts doing a cleanup, and gets an exception or something that generates an interrupt, and its KILLED status gets reset to RUNNABLE Then, in the finalizer, it starts creating a Thread, but at this point the main thread actually does get killed, and when that finalizer thread tries to run it runs into a null reference? I can provide the Rails sample project if needs be. Sorry if any of the above isn't clear; I've been staring at the C code for several hours and am a bit cross-eyed! Thank you for any insights. -- https://bugs.ruby-lang.org/