Fork, Ruby, ActiveRecord and File Descriptors on Fork
Asked Answered
A

1

6

I understand that when we fork a process the child process inherits a copy of the parents open file descriptors and offsets. According to the man pages this refers to the same file descriptors used by the parent. Based on that theory in the following program


puts "Process #{Process.pid}"

file = File.open('sample', 'w')

forked_pid = fork do
    sleep(10)
    puts "Writing to file now..."
    file.puts("Hello World. #{Time.now}")       
end




file.puts("Welcome to winter of my discontent #{Time.now}")
file.close
file = nil

Question 1: Shouldn't the forked process which is sleeping for 10 seconds lose its file descriptor and not be able to write to the file as the parent process completes and closes the file and exits.
Question 2: But for whatever reason if this works then how does ActiveRecord lose its connection in this scenario. It only works if I set :reconnect => true on ActiveRecord connect can it actually connect, which means its losing connection.


require "rubygems"
require "redis"
require 'active_record'
require 'mysql2'

connection = ActiveRecord::Base.establish_connection({
    :adapter => 'mysql2',
    :username => 'root_user',
    :password => 'Pi',
    :host => 'localhost',
    :database => 'list_development', 
    :socket => '/var/lib/mysql/mysql.sock'

    })

class User < ActiveRecord::Base   
end

u = User.first

puts u.inspect

fork do
    sleep 3
    puts "*" * 50
    puts User.first.inspect
    puts "*" * 50

end

puts User.first.inspect

However, the same is not true with Redis (v2.4.8) which does not lose connection on a fork, again. Does the it try to reconnect internally on a fork?

If thats the case then why isn't the write file program not throwing an error.

Could somebody explain whats going on here. Thanks

Alodee answered 26/10, 2012 at 15:28 Comment(0)
S
4

If you close a file descriptor in one process it stays valid in the other process, this is why your file example works fine.

The mysql case is different because it's a socket with another process at the end. When you call close on the mysql adapter (or when the adapter gets garbage collected when ruby exits) it actually sends a "QUIT" command to the server saying that you're disconnecting, so the server tears down its side of the socket. In general you really don't want to share a mysql connection between two processes - you'll get weird errors depending on whether the two processes are trying to use the socket at the same time.

If closing a redis connection just closes the socket (as opposed to sending a "I'm going away " message to the server) then the child connection should continue to work because the socket won't actually have been closed

Selfexamination answered 29/10, 2012 at 10:18 Comment(2)
Thanks for the answer Frederick. Not sure if this is a silly question but could you please explain what you mean by a "I'm going away" message.Alodee
In the mysql case the client actually sends a "QUIT" command so the server end of the socket closes the socket, releases any associated server side resources etc. By 'I'm going away' I just mean a command a client can send that will cause the server to do this.Selfexamination

© 2022 - 2024 — McMap. All rights reserved.