ruby-on-railscrubyruby-on-rails-pluginsffi

Ruby FFI - trapping C calls to exit()


I have a Rails application with a FFI binding to a custom RPC system. Unfortunately, the C code calls exit() when it is unable to connect to the RPC server, terminating the entire Rails application.

Is it possible to trap calls to exit() in Ruby? I'd rather not have to change the C code.


Solution

  • You cannot trap a function call. However maybe you can link the C code with a dummy exit() function that will not actually exit.

    The cleaner solution is by far to change the C code to give it a proper library behaviour.

    You could also fork in ruby before doing the RPC call, a bit on the expensive side performance wise but would solve the issue too...