Project

General

Profile

Bug #16906

Calling Thread#thread_variable? in IRB sometimes produce wrong result

Added by tyok (Mohammad Satrio) about 2 months ago. Updated about 2 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin17]
[ruby-core:98480]

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

test.rb (173 Bytes) test.rb Sample test file tyok (Mohammad Satrio), 05/22/2020 02:30 AM

Updated by jeremyevans0 (Jeremy Evans) about 2 months 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) about 2 months 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?.

#3

Updated by jeremyevans (Jeremy Evans) about 2 months 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]

Also available in: Atom PDF