Project

General

Profile

Actions

Bug #10133

closed

Bug in ruby

Added by lxh37 (Liz Huang) over 9 years ago. Updated over 9 years ago.

Status:
Third Party's Issue
Assignee:
-
Target version:
-
ruby -v:
2.1.0p0 (2013-12-25 revision 44422
[ruby-core:64361]

Description

I got a new Mac Pro server and tried to upgrade web applets to new version of Ruby On Rails, the web applet calls an external Fortran library I developed, it worked in Ruby on Rails (Ruby 1.8.7 and Rails 2.3.18) by using "DL". I tried to develop the same web applet in Ruby on Rails 4 (ruby 2.1.0p0 (2013-12-25 revision 44422) and Rails 4.1.2), with "DL" deprecated, so I use fiddle and wrapped Fortran by C, I tested the interface by commenting out all computation parts first to make sure it is working. Now I have the computation codes back (same codes as in older version) and I even create a console application to test the code is working. But when I try to call it from Ruby, it crashed.
I saved all messages in terminal window in attached file "bug.rtf" and also send the crash report.
I feel it might a bug in Ruby as it works in older version and the Fortran codes was tested in console application project.


Files

bug.rtf (125 KB) bug.rtf lxh37 (Liz Huang), 08/14/2014 08:51 PM
ruby_2014-08-14-162519_calder229.crash (25 KB) ruby_2014-08-14-162519_calder229.crash lxh37 (Liz Huang), 08/14/2014 08:53 PM
DiagnosticReports.zip (1.21 MB) DiagnosticReports.zip lxh37 (Liz Huang), 08/15/2014 06:28 PM
calculateSamplesize.rb (454 Bytes) calculateSamplesize.rb lxh37 (Liz Huang), 08/20/2014 07:30 PM
libsamplesize.dylib (1.78 MB) libsamplesize.dylib lxh37 (Liz Huang), 08/20/2014 07:31 PM
ruby_2014-08-20-153628_calder229.crash (26.3 KB) ruby_2014-08-20-153628_calder229.crash lxh37 (Liz Huang), 08/20/2014 07:40 PM

Updated by lxh37 (Liz Huang) over 9 years ago

I will be happy to send .dylib and ruby code which calls it if needed.
Thanks!

Updated by naruse (Yui NARUSE) over 9 years ago

At first, could you send CrashReport file?

See Crash Report log file under the one of following:
* ~/Library/Logs/CrashReporter
* /Library/Logs/CrashReporter
* ~/Library/Logs/DiagnosticReports
* /Library/Logs/DiagnosticReports
for more details.

Updated by lxh37 (Liz Huang) over 9 years ago

~/Library/Logs/DiagnosticReports is empty.
Couldn't find ~/Library/Logs/CrashReporter
Couldn't find /Library/Logs/CrashReporter
I compressed /Library/Logs/DiagnosticReports and attached,
but most files are not related, the related one is attached
in first post.

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

  • Status changed from Open to Feedback
  • Priority changed from 6 to Normal

2.1.0 is too old, can't you try with 2.1.2?

Updated by lxh37 (Liz Huang) over 9 years ago

I use rbenv to manage ruby versions, I did rbenv install --list, the latest stable version is 2.1.1, I installed it and it doesn't help, same bug. I wonder if there is a stack size or memory limitation in Ruby On Rails for running external dll, it looks like running out of memory. The program is crashed at a loop of 20000 when calculating entries for a 4X20000 matrix.

Updated by lxh37 (Liz Huang) over 9 years ago

I tried 2.2.0-dev version and still same error.

Updated by avit (Andrew Vit) over 9 years ago

The top stack frame shows Rails ActionDispatch middleware. This line is identified:

https://github.com/rails/rails/blob/4-1-2/actionpack/lib/action_dispatch/middleware/cookies.rb#L571

It might help to test your code in a standalone ruby script without all of Rails in order to narrow down the problem if it's actually happening in DL or breaking something in Rails.

Updated by lxh37 (Liz Huang) over 9 years ago

I tested my code in Ruby script and it works fine when I run the following in irb:

load "calculateSamplesize.rb"
ss=Samplesize.new()
ss.compute()

I attached calculateSamplesize.rb and libsamplesize.dylib, it seems this bug only shows up
in Ruby On Rails, I don't know how to fix...

Updated by lxh37 (Liz Huang) over 9 years ago

Not sure if it is helpful, I attached the .crash file generated today in
Logs/DiagnosticReports folder.

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

  • Status changed from Feedback to Third Party's Issue

Using fiddle?
Check if the arguments are correct, and the library works.

Updated by lxh37 (Liz Huang) over 9 years ago

I used fiddle in my Ruby script and it works! but when I use it in the same way
on Ruby On Rails, it crashed. I pasted the codes below and you can see the codes
are exactly the same. I am not sure if it is fiddle.

Ruby script which works:

class Samplesize

  require 'fiddle/import'

  module Libm
    extend Fiddle::Importer
    dlload '/Library/WebServer/smart/lib/libsamplesize.dylib'
    extern 'long Calculate(long, double, double, char*)'
  end

  
  def compute

    require 'fiddle'
    errmsg ="                                                                     "  
    puts Libm.Calculate( 1000, 0.2, 0.9, errmsg)
  end

end

Samplesize_controller.rb on Ruby On Rails that crashes when call "Libm.Calculate()".

class SamplesizeController < ApplicationController

  require 'fiddle/import'

  module Libm
    extend Fiddle::Importer
    dlload '/Library/WebServer/smart/lib/libsamplesize.dylib'
    extern 'long Calculate(long, double, double, char*)'
  end

  def index
  end

  def progress
    # session[:sigma_sq] = params[:sigma_sq].to_f
    session[:delta] = params[:delta].to_f
    session[:conf] = params[:conf].to_f
    session[:nmax] = params[:nmax].to_i
    redirect_to :action => "compute"
  end
  
  def compute

    require 'fiddle'
    if ( session[:delta] < 0.0 ) || ( session[:delta] > 1.0 )
      session[:errmsg] = "The value of delta must be between 0 and 1."
      session[:n] = -1
    elsif ( session[:conf] < 0.0 ) || ( session[:conf] > 1.0 )
      session[:errmsg] = "The value of conf must be between 0 and 1."
      session[:n] = -1
    elsif ( session[:nmax] < 0.0 )
      session[:errmsg] = "The value of nmax must be positive."
      session[:n] = -1
    else

      errmsg ="                                                                     "  
      resultn= Libm.Calculate(1000,0.2,0.9, errmsg)
      session[:n] = resultn
    end
    redirect_to :action => "results"
  end

  def results
    # @sigma_sq = session[:sigma_sq]
    @delta = session[:delta]
    @conf = session[:conf]
    @nmax = session[:nmax]
    @n = session[:n]
    @errmsg = session[:errmsg]
  end

  def start_over
    reset_session
    redirect_to :action => "index"
  end
end

Updated by lxh37 (Liz Huang) over 9 years ago

BTW, errmsg was set to a string with 70 empty spaces in both places, when I paste it in my previous post, it doesn't look well, so I edited it.

Updated by lxh37 (Liz Huang) over 9 years ago

Where can I get help to fix this bug?

Updated by funny_falcon (Yura Sokolov) over 9 years ago

Can you test script with threads? Wrapping your code into Thread.new do your_code end.join . And wrap it in a loop again.

Updated by lxh37 (Liz Huang) over 9 years ago

Thanks for trying to help.

Thread is new to me, I understand "Wrapping your code into Thread.new do your_code end.join" after read documentation about thread, but not sure what do you mean by "wrap it in a loop again"?

Updated by lxh37 (Liz Huang) over 9 years ago

When I wrap my code by thread, it has trouble to finish. I only see the output
of "Started At ....", never get to see "End at ...".

require 'fiddle/import'

  module Libm
    extend Fiddle::Importer
    dlload '/Library/WebServer/smart/lib/libsamplesize.dylib'
    extern 'long Calculate(long, double, double, char*)'
  end

  puts "Started At #{Time.now}"
  errmsg = "                                                                                " 
  thr1=Thread.new{ puts Libm.Calculate( 1000, 0.2, 0.9, errmsg)}
  #thr2=Thread.new{ puts Libm.Calculate( 800, 0.3, 0.9, errmsg)}
  thr1.join
  #thr2.join
  puts "End at #{Time.now}"

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

What does "libsamplesize"?

FFI is not a magic that make you free from C.
It just eases memory management, data conversion, and etc.
You must be able to use functions properly in C first.

Updated by lxh37 (Liz Huang) over 9 years ago

"libSamplesize.dylib" is external library I genereated from C codes. Hope you read
the thread before you post answer, I have written console application, ruby script
to test C code and it works properly, it only crashes in Ruby On Rails.
The way I called in Ruby Script and in Ruby On Rails is exactly the same, I even
hard-coded the arguments to fixed numbers to eliminate any mistake in user's side,
FFI works in Ruby script, but crashed in Ruby On Rails.

Yura Sokolov suggested to test the code in thread, it crashes. I don't know how
FFI works, why it works when not wrapped in thread, crashes in thread. My C code
doesn't use any thread, it is simply scientific computation.

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

Liz Huang wrote:

"libSamplesize.dylib" is external library I genereated from C codes.

Then you can debug it.

Updated by lxh37 (Liz Huang) over 9 years ago

I could call library libSamplesize.dylib successfully in console application
and ruby script (not in thread). We have created a test project to test this
part of codes thoroughly before we generated the library, actually this part
of code was created several years ago and worked in old version of Ruby On Rails
(we used package DL, but not supported in new version of Ruby On Rails).
Is there anything in FFI or Ruby On Rails? The codes (see below), the ruby script
working and in thread not working seems to suggest the bug in not inside the external
library.

Working:

require 'fiddle/import'

module Libm
extend Fiddle::Importer
dlload '/Library/WebServer/smart/lib/libsamplesize.dylib'
extern 'long Calculate(long, double, double, char*)'
end

errmsg = " "
puts Libm.Calculate( 1000, 0.2, 0.9, errmsg)

Not working in thread:

require 'fiddle/import'

module Libm
extend Fiddle::Importer
dlload '/Library/WebServer/smart/lib/libsamplesize.dylib'
extern 'long Calculate(long, double, double, char*)'
end

puts "Started At #{Time.now}"
errmsg = " "
thr1=Thread.new{ puts Libm.Calculate( 1000, 0.2, 0.9, errmsg)}
thr1.join
puts "End at #{Time.now}"

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0