Skip to content

Instantly share code, notes, and snippets.

@bklang
Created June 10, 2011 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bklang/1018994 to your computer and use it in GitHub Desktop.
Save bklang/1018994 to your computer and use it in GitHub Desktop.
Pry threading test
require 'rubygems'
require 'pry'
#require 'rbtrace'
puts $$
threads = []
threads << Thread.new do
loop do
puts "Thread 1"
sleep 1
end
end
threads << Thread.new do
pry
end
threads << Thread.new do
loop do
puts "Thread 2"
sleep 1
end
end
threads.each {|t| t.join }
@bklang
Copy link
Author

bklang commented Jun 10, 2011

This script will print "Thread 1" once and launch the Pry console if you are using a broken readline on Mac OSX. Ruby on OSX links by default with libedit, which breaks Ruby threads. If you are using a working version of readline, linked against libreadline, then you should alternately see "Thead 1" and "Thread 2" print at intervals of 1 second on top of the running Pry console.

For information on how to fix this problem, see http://tim.theenchanter.com/2010/01/getting-ruby-to-use-readline-instead-of.html

@benlangfeld
Copy link

Is it possible to check this in something that will output a Boolean? We're going to have to put something in Adhearsion to test accurately for it.

@drbrain
Copy link

drbrain commented Aug 3, 2011

I don't see this behavior with Apple Ruby 1.8.7 nor Ruby 1.9.3 (past preview 1):

$ uname -a
Darwin YPCMC10014 11.0.0 Darwin Kernel Version 11.0.0: Sat Jun 18 12:56:35 PDT 2011; root:xnu-1699.22.73~1/RELEASE_X86_64 x86_64

$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
$ otool -L `ruby -S gem which readline`
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin11.0/readline.bundle:
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib (compatibility version 1.8.0, current version 1.8.7)
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)

$ ruby19 -v
ruby 1.9.3dev (2011-08-02 revision 32802) [x86_64-darwin11.0.0]
$ otool -L `ruby19 -S gem which readline`
/usr/local/lib/ruby/1.9.1/x86_64-darwin11.0.0/readline.bundle:
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

@benlangfeld
Copy link

Looks like you're probably linked against libedit. Did you follow the instructions in the blog post?

@bklang
Copy link
Author

bklang commented Aug 3, 2011

Interesting, I had not tried with Apple Ruby. Did you run the test code in the original Gist? What output did you receive?

When I get some time I will try with Ruby 1.9.3. In my tests both 1.8.7 and 1.9.2 (the broken versions) were compiled via RVM.

@drbrain
Copy link

drbrain commented Aug 3, 2011

I compiled 1.9.3 off the branch by hand using gcc not llvm-gcc, I don't use RVM and I ran distclean since upgrading to OS X 10.7.

If I leave either ruby or ruby19 running it'll continue forever

$ GEM_HOME=~/tmp/gems gem list pry

*** LOCAL GEMS ***

pry (0.9.3)

$ curl -s https://raw.github.com/gist/1018994/0bf2737a6ac05e3be254cbe9924bcd65e9a01b57/pry-threads.rb | GEM_HOME=~/tmp/gems ruby
1563
Thread 1
Thread 2

Thread 1
Thread 2
Thread 2
Thread 1
^C-:25:in `join': Interrupt
    from -:25
    from -:25:in `each'
    from -:25

$ curl -s https://raw.github.com/gist/1018994/0bf2737a6ac05e3be254cbe9924bcd65e9a01b57/pry-threads.rb | GEM_HOME=~/tmp/gems ruby19
1570
Thread 1

Thread 2
Thread 1Thread 2

Thread 2Thread 1

Thread 1
Thread 2
^C-:25:in `join': Interrupt
    from -:25:in `block in <main>'
    from -:25:in `each'
    from -:25:in `<main>'

@bklang
Copy link
Author

bklang commented Feb 19, 2012

Another way to fix this with rvm:

rvm pkg install readline
rvm install 1.9.2 --with-readline-dir=$rvm_path/usr

Also works for other versions of Ruby.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment