Ruby/Rails exports, please help fixing a bug in LSAPI

Hi,

We currently need your help to fix a bug in our ruby LSAPI.extension. The bug is related to DB connection in ActiveRecord. We have users reported that they got DB connection errors, after some investigations, we believe that it is related to the way we start Rails dispatcher processes.

In order to reduce time spending in Rails framework initialization, LSAPI start one parent process with framework initialized, then fork children processes off the parent process on-demand, so the framework only need to be initialized once. It works pretty well with some applications. However, if for some reason, one application need to establish a DB connection to backend DB during the framework initialization, all children processes will inherit the file handle of the established DB connection, DB connection error may occur when children processes need to access DB via that connection. It does not happen to all application, the default Rails initialization procedure does not establish the DB connection.

We think a possible fix is to close or reestablish the connection right after fork() returns in the child process, this way, each child process will have its own DB connection. The fix could be

ActiveRecord::Base.clear_active_connections!() or ActiveRecord::Base.remove_connection() or ActiveRecord::Base.establish_connection( ActiveRecord::Base.remove_connection() )

We are not expert ruby developer, would like to consult expert here, which one is the best fix? or is there any better solution? And there are couple questions even the fix we proposed could work. How can we determine if ActiveRecord has been loaded? DB has been initialized? DB connection has been established? How can we write the equivalent ruby code in ruby C extension?

Thanks in advance! George Wang

Problem solved. The solution is pretty simple actually. Much easier than I initially thought. :slight_smile: We just add one line of code in our RailsRunner.rb

ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base)

before calling LSAPI.accept to make sure all DB connections has been closed. We will release LSWS 2.2.1 with the fixed RailsRunner.rb along with a few minor bug fixes. In the meantime, if you experiencing DB connection problem, please replace lsws/fcgi-bin/RailsRunner.rb with the fixed on attached.

Best Regards, George Wang

George Wang wrote:

RailsRunner.rb (296 Bytes)