Background jobs with #fork

Patrik Wenger paddor at
Thu Apr 12 22:41:03 UTC 2012

Thanks for the answers.

> So you're only calling fork and not exec (or system/popen, right?)  It
> may be the case that the client socket is kept alive in the background
> process.

Yes, I'm only calling Kernel#fork. @Eric, your guess makes sense to me.

> The client socket has the close-on-exec flag (FD_CLOEXEC) set, but
> there's no close-on-fork flag, so you might have to find + close it
> yourself.  Here's is a nasty workaround for the child process:
>  ObjectSpace.each_object(Kgio::Socket) do |io|
>    io.close unless io.closed?
>  end

Isn't there another way to retrieve the right socket?

Here some additional info that might bring some clarification.

Another action in the same controller which does about the same regarding the background job works (!).
The only differences I see are:

1) it's called via AJAX
2) the response is nothing (render :nothing => true) instead of a redirect (redirect_to checks_path)

I reckon the second difference kind of confirms Eric's guess, as the client socket probably isn't considered anymore with render :nothing => true.

> However, nginx can still forward subsequent requests to the same unicorn
> (even the same unicorn worker), because as far as the unicorn worker is
> concerned (but not the OS), it's done with the original request.  It's
> just the original request (perhaps the original client) is stuck
> waiting for the background process to finish.
> I can probably writeup a better explanation (perhaps on the usp.ruby
> (Unix Systems Programming for Ruby mailing list) if this doesn't make
> sense.

Yeah I don't really understand this part. The "hanging" Unicorn worker can read another request because the client socket wasn't closed because it's still open in the child process? I would appreciate a better explanation, thank you.

More information about the mongrel-unicorn mailing list