negativecode / vines

An XMPP chat server for Ruby.
http://www.getvines.org/
MIT License
469 stars 75 forks source link

OpenSSL::PKey::RSA.generate sometimes fails in test #34

Closed monban closed 10 years ago

monban commented 10 years ago

Ubuntu 14.04 Ruby 2.1.2 Vines HEAD

80% of the time, all 900 assertions run without issue, 20% of the time I get this:

monban@caesura:~/vines$ script/tests
Run options: --seed 63033

# Running:

................................................................................................................................................................................................................................................................E.....................................................................................................................................

Finished in 2.687001s, 145.1432 runs/s, 334.2016 assertions/s.

  1) Error:
Vines::Store#test_0001_parses certificate files:
OpenSSL::PKey::RSAError: BN lib
    /home/monban/vines/test/store_test.rb:106:in `generate'
    /home/monban/vines/test/store_test.rb:106:in `certificate'
    /home/monban/vines/test/store_test.rb:13:in `block (2 levels) in <top (required)>'

390 runs, 898 assertions, 0 failures, 1 errors, 0 skips
rake aborted!
Command failed with status (1): [ruby -I"lib:test:test/storage" -I"/home/monban/vines/.bundle/gems/ruby/2.0.0/gems/rake-10.1.1/lib" "/home/monban/vines/.bundle/gems/ruby/2.0.0/gems/rake-10.1.1/lib/rake/rake_test_loader.rb" "test/**/*_test.rb" ]

Tasks: TOP => test
(See full trace by running task with --trace)

Seems to be some kind of race condition? It does not always occur with the same seed. I am also NOT able to reproduce it by doing this:

require 'openssl'
10000.times { OpenSSL::PKey::RSA.generate(256) }

That runs flawlessly every time.

Funny note, the bug does NOT appear on Ruby HEAD. The shipping version of eventmachine does not compile against Ruby HEAD, but the latest version from the repo DOES, so it might be related to eventmachine#457?

In any event the problem will likely resolve itself with new versions of Ruby, and I'm not sure if this happens in actual production (haven't seen it yet) or just because of the way this test is run. This is mostly for documentation purposes if (when) people see it running Vines on Ruby 2.1.2.

dgraham commented 10 years ago

Interesting. I'm seeing this with Ruby 2.1.1 on OS X 10.9.2 as well. I thought maybe it was caused by the small test key size, so I just tested with 512, 1024, and 2048 bit keys. All of those sizes failed too.

I'm closing this issue since it's informational and fixed in upstream Ruby. Thanks for the detailed report!

bmishkin commented 10 years ago

Ruby 2.1.3 Rails 4.1.6 OS X 10.9.5

Not using vines myself, but this is the only place I've seen this error also reported. Originally saw on ruby 2.1.2, I was not able to resolve by upgrading to 2.1.3.

In a separate project I'm seeing same problem when executing OpenSSL::PKey::RSA.generate(1024) => OpenSSL::PKey::RSAError: BN lib via an after_create: rails callback within a loop. Occurs 100% on the second or third iteration.

Curiously, I cannot reproduce in console via loop 10000.times { OpenSSL::PKey::RSA.generate(256) }

I am able to work around by adding: sleep(0.1) within the loop.

I suspect it's something related to random key generation using the system clock within ruby-openssl.

dcu commented 10 years ago

@bmishkin I'm having the same issue. were you able to resolve it? what compiler did you use to compile ruby? one way to find out is printing the configuration options:

ruby -e 'puts RbConfig::CONFIG["configure_args"]'
bmishkin commented 9 years ago

@dcu compiled via gcc yes, the sleep() resolved it for me