tmm1 / rbtrace

like strace, but for ruby code
MIT License
1.71k stars 64 forks source link

PID not listening? #54

Open shellandbull opened 8 years ago

shellandbull commented 8 years ago

Hello! I have the following setup, using rails 4.0.6

def create
  require "rbtrace"
  pid    = Process.pid
  result = system("bundle exec rbtrace -u 5000 -p #{pid} -e 'load #{Rails.root}/script/heap_dump.rb'")
  render json: { status: result }
end

And I still get the following error:

Error: argument --pid (pid is not listening for messages, did you `require "rbtrace"`).

PID is defined and I'm running in a separate thread which I later then join to the main one, i.e:

# heap_dump.rb

Thread.new do
  require "objspace"
  # should rbtrace be required here?
  ObjectSpace.trace_object_allocations_start
  GC.start
  filename = "#{DateTime.now.strftime("%d-%^B-%Y")}-#{ENV["USER"]}-#{Rails.env.upcase}.json"
  ObjectSpace.dump_all(output: File.open(filename, "w"))

  s3 = Aws::S3::Resource.new(credentials: Aws::Credentials.new(ENV["AWS_ACCESS_KEY_ID"], ENV["AWS_SECRET_ACCESS_KEY"]),
                             region: "us-east-1")

  object = s3.bucket("memory-benchmarks").object(filename)
  object.upload_file(filename, acl: "authenticated-read")
end.join

Maybe I need to require rbtrace on the block passed to the new thread :) but if there's any known issues around requiring the library it would be good to know :)

I'm also using puma as my web server

jamo commented 8 years ago

Just out of curiosity, have you tried with latest Rails version (or even lates 4.0.x series (4.0.13)

tmm1 commented 8 years ago

When you run system() it blocks the current process so it cannot listen to rbtrace messages. Invoking rbtrace from the current process to connect back to itself doesn't make much sense and is not recommended.

shellandbull commented 8 years ago

@tmm1 what would be the best recommendation based on my intention?

olivervbk-studiare commented 7 years ago

You could execute it with: system("nohup bundle exec rbtrace -u 5000 -p #{pid} -e 'load #{Rails.root}/script/heap_dump.rb' &") Though if you're running unicorn there seems to be a problem getting rbtrace to attach itself...