Can Unicorn Listen to an Existing Socket Descriptor?

Eric Wong normalperson at yhbt.net
Thu Apr 4 18:09:21 UTC 2013


Jacob <groundwater at gmail.com> wrote:
> I know that Unicorn opens and shares it's bound socket amongst its
> sub-processes, however I was hoping I could also pre-open a socket to
> be handed off to Unicorn.

Yes, but unicorn has this feature to support in-place upgrades
(SIGUSR2).  You still need to tell unicorn about the socket via config
file or command-line, otherwise it'll close sockets it does not know
about (to support changing addresses/ports during USR2 upgrades).

You need to set the UNICORN_FD environment variable in the parent

Something like this (untested):

  require "socket"

  # bind the socket in the parent
  s = TCPServer.new("0.0.0.0", 1234)

  # this may be comma-delimited list of FD numbers:
  ENV["UNICORN_FD"] = s.fileno.to_s

  # sharing non-standard FDs over exec() must be explicit in Ruby 2.0:
  s.close_on_exec = false
  redirects = { s => s }

  # unicorn must know about the bound address of the FDs its inheriting
  exec("unicorn", "-l", "0.0.0.0:1234", redirects)


More information about the mongrel-unicorn mailing list