https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-07-17T23:58:25ZRuby Issue Tracking SystemRuby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=796932019-07-17T23:58:25Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Backport</strong> deleted (<del><i>1.9.3: UNKNOWN, 2.0.0: UNKNOWN</i></del>)</li></ul><p>This behavior is still present in the master branch and it does seem like a bug to me. It does seems specific to returning a closure from a method and having two separate threads call that closure.</p>
<p>It doesn't occur if you pass the proc directly to <code>Thread.new</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">get_proc</span>
<span class="nb">lambda</span> <span class="k">do</span> <span class="o">|</span><span class="n">str</span><span class="o">|</span>
<span class="n">str</span> <span class="o">=~</span> <span class="sr">/(.+)/</span>
<span class="nb">sleep</span> <span class="mf">0.1</span>
<span class="nb">puts</span> <span class="s2">"got </span><span class="si">#{</span><span class="vg">$1</span><span class="si">}</span><span class="s2"> from </span><span class="si">#{</span><span class="n">str</span><span class="si">}</span><span class="se">\n</span><span class="s2">"</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="nb">proc</span> <span class="o">=</span> <span class="n">get_proc</span>
<span class="n">t1</span> <span class="o">=</span> <span class="no">Thread</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"abc"</span><span class="p">,</span> <span class="o">&</span><span class="nb">proc</span><span class="p">)</span>
<span class="n">t2</span> <span class="o">=</span> <span class="no">Thread</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"def"</span><span class="p">,</span> <span class="o">&</span><span class="nb">proc</span><span class="p">)</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">join</span>
<span class="n">t2</span><span class="p">.</span><span class="nf">join</span>
</code></pre>
<p>or if you switch to calling a method directly:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">get_proc</span> <span class="n">str</span>
<span class="n">str</span> <span class="o">=~</span> <span class="sr">/(.+)/</span>
<span class="nb">sleep</span> <span class="mf">0.1</span>
<span class="nb">puts</span> <span class="s2">"got </span><span class="si">#{</span><span class="vg">$1</span><span class="si">}</span><span class="s2"> from </span><span class="si">#{</span><span class="n">str</span><span class="si">}</span><span class="se">\n</span><span class="s2">"</span>
<span class="k">end</span>
<span class="n">t1</span> <span class="o">=</span> <span class="no">Thread</span><span class="p">.</span><span class="nf">new</span> <span class="p">{</span> <span class="n">get_proc</span> <span class="s2">"abc"</span> <span class="p">}</span>
<span class="n">t2</span> <span class="o">=</span> <span class="no">Thread</span><span class="p">.</span><span class="nf">new</span> <span class="p">{</span> <span class="n">get_proc</span> <span class="s2">"def"</span> <span class="p">}</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">join</span>
<span class="n">t2</span><span class="p">.</span><span class="nf">join</span>
</code></pre>
<p>From some debugging, one difference between the two cases in the original post is that different branches are taken in <code>lep_svar</code>, with the failing case taking the <code>if</code> and the passing case taking the <code>else</code>.</p> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=800192019-07-25T17:49:34Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-1 priority-4 priority-default" href="/issues/12689">Bug #12689</a>: Thread isolation of $~ and $_</i> added</li></ul> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=802552019-07-30T07:38:30Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>ko1 (Koichi Sasada)</i></li></ul> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=810442019-08-26T16:48:56Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Has duplicate</strong> <i><a class="issue tracker-1 status-1 priority-4 priority-default" href="/issues/14364">Bug #14364</a>: Regexp last match variable in procs</i> added</li></ul> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=833622019-12-23T17:49:37Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/16448">Bug #16448</a>: regex global variables are working differently depending on scope</i> added</li></ul> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=1053002023-11-13T19:34:55ZAnonymous
<ul></ul><p>RubyConf Hack Day: suggest to close</p> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=1053052023-11-13T19:47:48Zjemmai (Jemma Issroff)
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=1053072023-11-13T19:50:40Zjemmai (Jemma Issroff)
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li></ul> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=1053242023-11-14T04:49:22Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/105324/diff?detail_id=65886">diff</a>)</li></ul><p>"Thread-local" in that sentence was wrong, and removed at <a class="changeset" title="[DOC] Regexp doc (#7923)" href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/932dd9f10e684fa99b059054fbc934607d85b45a">932dd9f10e684fa99b059054fbc934607d85b45a</a>, but "method-local" is removed too.</p> Ruby master - Bug #8444: Regexp vars $~ and friends are not thread localhttps://bugs.ruby-lang.org/issues/8444?journal_id=1053352023-11-15T14:06:39ZDan0042 (Daniel DeLorme)
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote in <a href="#note-9">#note-9</a>:</p>
<blockquote>
<p>"Thread-local" in that sentence was wrong, and removed at <a class="changeset" title="[DOC] Regexp doc (#7923)" href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/932dd9f10e684fa99b059054fbc934607d85b45a">932dd9f10e684fa99b059054fbc934607d85b45a</a>, but "method-local" is removed too.</p>
</blockquote>
<p>Indeed, the documentation used to say "Regexp Global Variables" or "Special Global Variables" and this has been reduced to just "Global Variables" which I think is quite misleading. Sure they have the <code>$</code> sigil, but they're not global at all. If anything, "Special Variables" would be more accurate.</p>