Project

General

Profile

Actions

Bug #8444

open

Regexp vars $~ and friends are not thread local

Added by jamespharaoh (James Pharaoh) over 9 years ago. Updated over 3 years ago.

Status:
Open
Priority:
Normal
Target version:
-
ruby -v:
trunk
Backport:
[ruby-core:55153]
Tags:

Description

=begin
In the docs for the Regexp special variables, $~ and friends, it says "These global variables are thread-local and method-local variables". However the following gives an unexpected result:

def get_proc
proc do |str|
str =~ /(.+)/
sleep 0.1
puts "got #{$1} from #{str}\n"
end
end
proc = get_proc
t1 = Thread.new { proc.call "abc" }
t2 = Thread.new { proc.call "def" }
t1.join
t2.join

This outputs the following:

got abc from abc
got abc from def

The expected result is of course:

got abc from abc
got def from def

Clearly the variables are being scoped to the get_proc method and are being shared by both threads. This runs contrary to the documentation and also to expectations.

This behaviour should either be changed, or the documentation updated to reflect the actual behaviour.

Interestingly, the following does work as expected:

proc = proc do |str|
str =~ /(.+)/
sleep 0.1
puts "got #{$1} from #{str}\n"
end
t1 = Thread.new { proc.call "abc" }
t2 = Thread.new { proc.call "def" }
t1.join
t2.join

=end


Related issues 3 (2 open1 closed)

Related to Ruby master - Bug #12689: Thread isolation of $~ and $_OpenActions
Related to Ruby master - Bug #16448: regex global variables are working differently depending on scopeClosedActions
Has duplicate Ruby master - Bug #14364: Regexp last match variable in procsOpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0