Issue #14123 has been updated by akr (Akira Tanaka).


mame (Yusuke Endoh) wrote:
> Okay, my third try: https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/60948
> 

It is still not thread safe.

Consider a thread context switch at just after defining Kernel#pp
before defining PP class.

```
Index: lib/pp.rb
===================================================================
--- lib/pp.rb	(revision 60960)
+++ lib/pp.rb	(working copy)
@@ -26,6 +26,7 @@ module Kernel
   end
   undef __pp_backup__ if method_defined?(:__pp_backup__)
   module_function :pp
+  sleep 1 # thread context switch
 end
 
 ##
```

With the above patch, "uninitialized constant Kernel::PP" can
happen as as follows.

```
% ./ruby -w -Ilib -e '
t1 = Thread.new {
  Thread.current.report_on_exception = true
  pp :foo1
}
t2 = Thread.new {
  Thread.current.report_on_exception = true
  sleep 0.5
  pp :foo2       
}
t1.join rescue nil
t2.join rescue nil
'
#<Thread:0x000055dbf926eaa0@-e:6 run> terminated with exception:
Traceback (most recent call last):
	3: from -e:9:in `block in <main>'
	2: from /home/ruby/tst2/ruby/lib/pp.rb:22:in `pp'
	1: from /home/ruby/tst2/ruby/lib/pp.rb:22:in `each'
/home/ruby/tst2/ruby/lib/pp.rb:23:in `block in pp': uninitialized constant Kernel::PP (NameError)
:foo1
```

Fix is simple: replacing Kernel#pp at last as r60961.



----------------------------------------
Feature #14123: Kernel#pp by default
https://bugs.ruby-lang.org/issues/14123#change-68108

* Author: mame (Yusuke Endoh)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 2.5
----------------------------------------
Matz, may I commit this?  I really want this.

```
diff --git a/prelude.rb b/prelude.rb
index 7b98e28285..87f49ac9fb 100644
--- a/prelude.rb
+++ b/prelude.rb
@@ -141,3 +141,11 @@ def irb
     irb
   end
 end
+
+module Kernel
+  def pp(*objs)
+    undef :pp
+    require 'pp'
+    pp(*objs)
+  end
+end
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>