Bug #16906
closedCalling Thread#thread_variable? in IRB sometimes produce wrong result
Description
Consider this script:
Thread.current.thread_variable_set("ab", 12)
puts Thread.current.thread_variable?("ab")
puts Thread.current.thread_variable?(:ab)
puts Thread.current.thread_variable?("ab")
When I put the script above in a file and run it, I got three true
as expected:
bash-3.2$ ruby test.rb
true
true
true
But when I copy each line to IRB, I got false on the first check:
bash-3.2$ irb
irb(main):001:0> Thread.current.thread_variable_set("ab", 12)
=> 12
irb(main):002:0> puts Thread.current.thread_variable?("ab")
false
=> nil
irb(main):003:0> puts Thread.current.thread_variable?(:ab)
true
=> nil
irb(main):004:0> puts Thread.current.thread_variable?("ab")
true
=> nil
I expect it to print three true
in IRB, just like when I run the script via file.
Files
Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
I can confirm this bug. It's because thread_variable?
uses different code than thread_variable_get
and thread_variable_set
. Removing the rb_check_id
and switching ID2SYM
to rb_to_symbol
fixes it. I've added a pull request for this, and will merge if it passes CI: https://github.com/ruby/ruby/pull/3145
Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
That approach didn't work as there are tests for inadvertent symbol creation. To fix that, we need to force ID creation in thread_local_set. I've updated the pull request to use that approach, and made thread_variable_get use the same inadvertent symbol creation check as thread_variable?.
Updated by jeremyevans (Jeremy Evans) over 4 years ago
- Status changed from Open to Closed
Applied in changeset git|4e1f2283b432e833bd4e6f7724ba0496760e68e8.
Make Thread#thread_variable? similar to #thread_variable_get
Don't use rb_check_id, which only works for pinned symbols.
Switch inadvertent creation test for thread_variable? to
only check for pinned symbols, same as thread_variable_get
and thread_variable_set.
Make key variable name in thread_local_set match
thread_local_get and thread_variable?.
Fixes [Bug #16906]
Updated by greneholt (Connor McKay) about 3 years ago
This needs to be backported to 2.7.x. It wasn't fixed in 2.7.4.