https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112021-03-08T16:21:30ZRuby Issue Tracking SystemRuby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=907942021-03-08T16:21:30Zivoanjo (Ivo Anjo)ivo.anjo@datadoghq.com
<ul><li><strong>ruby -v</strong> set to <i>ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]</i></li></ul> Ruby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=908012021-03-09T00:42:13Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>ko1 (Koichi Sasada)</i></li></ul> Ruby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=908032021-03-09T00:42:41Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Tags</strong> set to <i>ractor</i></li></ul> Ruby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=1043042023-08-24T21:12:01Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>ruby -v</strong> deleted (<del><i>ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]</i></del>)</li><li><strong>Backport</strong> deleted (<del><i>2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN</i></del>)</li></ul><p>As Ractors always use separate OS threads, and fork only runs the current thread in the forked process, I don't see a way for Ractors to continue where they left off after fork. I think auto-starting would likely be a bad idea, because auto-starting would not return them to the state they were at fork.</p>
<p>The addition of <code>Ractor#alive?</code> and/or <code>Ractor#status</code> makes sense to me. Even in non-forked processes such methods could be useful. Note that you can get what you want already, by calling <code>Ractor#inspect</code>, so these methods would only need to expose information that Ractor is already storing.</p> Ruby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=1043272023-08-25T08:12:01Zivoanjo (Ivo Anjo)ivo.anjo@datadoghq.com
<ul></ul><blockquote>
<p>The addition of Ractor#alive? and/or Ractor#status makes sense to me. Even in non-forked processes such methods could be useful. Note that you can get what you want already, by calling Ractor#inspect, so these methods would only need to expose information that Ractor is already storing.</p>
</blockquote>
<p>Thanks for looking into this!</p>
<p>I don't think the info is there in #inspect... At least I don't get it on stable or latest ruby-head? 😅</p>
<p>Here's an updated example:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">puts</span> <span class="no">RUBY_DESCRIPTION</span>
<span class="n">r2</span> <span class="o">=</span> <span class="no">Ractor</span><span class="p">.</span><span class="nf">new</span> <span class="p">{</span> <span class="nb">puts</span> <span class="s2">"[</span><span class="si">#{</span><span class="no">Process</span><span class="p">.</span><span class="nf">pid</span><span class="si">}</span><span class="s2">] Ractor started!"</span><span class="p">;</span> <span class="nb">sleep</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="p">}</span>
<span class="nb">puts</span> <span class="s2">"[</span><span class="si">#{</span><span class="no">Process</span><span class="p">.</span><span class="nf">pid</span><span class="si">}</span><span class="s2">] In parent process, ractor status is </span><span class="si">#{</span><span class="n">r2</span><span class="p">.</span><span class="nf">inspect</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"[</span><span class="si">#{</span><span class="no">Process</span><span class="p">.</span><span class="nf">pid</span><span class="si">}</span><span class="s2">] Forking..."</span>
<span class="nb">fork</span> <span class="k">do</span>
<span class="nb">puts</span> <span class="s2">"[</span><span class="si">#{</span><span class="no">Process</span><span class="p">.</span><span class="nf">pid</span><span class="si">}</span><span class="s2">] In child process, ractor status is </span><span class="si">#{</span><span class="n">r2</span><span class="p">.</span><span class="nf">inspect</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="no">Process</span><span class="p">.</span><span class="nf">wait</span>
</code></pre>
<p>and here's what I get:</p>
<pre><code>$ ruby ractor-test.rb
ruby 3.3.0dev (2023-08-24T12:12:51Z master 5ec1fc52c1) [x86_64-linux]
ractor-test.rb:3: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
[10] In parent process, ractor status is #<Ractor:#2 ractor-test.rb:3 blocking>
[10] Ractor started!
[10] Forking...
[12] In child process, ractor status is #<Ractor:#2 ractor-test.rb:3 blocking>
</code></pre> Ruby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=1043402023-08-25T14:35:19Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Tracker</strong> changed from <i>Feature</i> to <i>Bug</i></li><li><strong>Backport</strong> set to <i>3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN</i></li></ul><p>Thanks for that information. It looks like ractors contain information about their state (shown in <code>inspect</code>), but that state is not updated on fork, unlike threads. So I think there is a bug, and it's that non-main ractors do not have their state to <code>terminated</code> upon fork (forking from non-main ractors is going to be prohibited, see <a class="issue tracker-1 status-2 priority-4 priority-default" title="Bug: forking in a ractor causes Ruby to crash (Assigned)" href="https://bugs.ruby-lang.org/issues/17516">#17516</a>).</p> Ruby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=1043412023-08-25T15:29:26Zivoanjo (Ivo Anjo)ivo.anjo@datadoghq.com
<ul></ul><p>Ack, that seems a reasonable way of looking at this, having a way to detect that the ractor is dead would be enough to write some auto-restart code after fork (Erlang supervisor trees here we go :D).</p> Ruby master - Bug #17678: Ractors do not restart after forkhttps://bugs.ruby-lang.org/issues/17678?journal_id=1043452023-08-25T16:29:28Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>I updated <a href="https://github.com/ruby/ruby/pull/8283" class="external">https://github.com/ruby/ruby/pull/8283</a> to mark non-main ractors as terminated after fork.</p>