unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 556aba890ffa1281d7515f0f3722c9ce8276f518 22358 bytes (raw)
$ git show v0.9.2:lib/unicorn.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
 
require 'fcntl'
require 'unicorn/socket_helper'
autoload :Rack, 'rack'

# Unicorn module containing all of the classes (include C extensions) for running
# a Unicorn web server.  It contains a minimalist HTTP server with just enough
# functionality to service web application requests fast as possible.
module Unicorn
  autoload :Const, 'unicorn/const'
  autoload :HttpRequest, 'unicorn/http_request'
  autoload :HttpResponse, 'unicorn/http_response'
  autoload :Configurator, 'unicorn/configurator'
  autoload :TeeInput, 'unicorn/tee_input'
  autoload :ChunkedReader, 'unicorn/chunked_reader'
  autoload :TrailerParser, 'unicorn/trailer_parser'
  autoload :Util, 'unicorn/util'

  Z = '' # the stock empty string we use everywhere...
  Z.force_encoding(Encoding::BINARY) if Z.respond_to?(:force_encoding)
  Z.freeze

  class << self
    def run(app, options = {})
      HttpServer.new(app, options).start.join
    end
  end

  # This is the process manager of Unicorn. This manages worker
  # processes which in turn handle the I/O and application process.
  # Listener sockets are started in the master process and shared with
  # forked worker children.

  class HttpServer < Struct.new(:listener_opts, :timeout, :worker_processes,
                                :before_fork, :after_fork, :before_exec,
                                :logger, :pid, :app, :preload_app,
                                :reexec_pid, :orig_app, :init_listeners,
                                :master_pid, :config)
    include ::Unicorn::SocketHelper

    # prevents IO objects in here from being GC-ed
    IO_PURGATORY = []

    # all bound listener sockets
    LISTENERS = []

    # This hash maps PIDs to Workers
    WORKERS = {}

    # See: http://cr.yp.to/docs/selfpipe.html
    SELF_PIPE = []

    # signal queue used for self-piping
    SIG_QUEUE = []

    # constant lookups are faster and we're single-threaded/non-reentrant
    REQUEST = HttpRequest.new

    # We populate this at startup so we can figure out how to reexecute
    # and upgrade the currently running instance of Unicorn
    START_CTX = {
      :argv => ARGV.map { |arg| arg.dup },
      # don't rely on Dir.pwd here since it's not symlink-aware, and
      # symlink dirs are the default with Capistrano...
      :cwd => `/bin/sh -c pwd`.chomp("\n"),
      0 => $0.dup,
    }

    class Worker < Struct.new(:nr, :tmp)
      # worker objects may be compared to just plain numbers
      def ==(other_nr)
        self.nr == other_nr
      end
    end

    # Creates a working server on host:port (strange things happen if
    # port isn't a Number).  Use HttpServer::run to start the server and
    # HttpServer.run.join to join the thread that's processing
    # incoming requests on the socket.
    def initialize(app, options = {})
      self.app = app
      self.reexec_pid = 0
      self.init_listeners = options[:listeners] ? options[:listeners].dup : []
      self.config = Configurator.new(options.merge(:use_defaults => true))
      self.listener_opts = {}
      config.commit!(self, :skip => [:listeners, :pid])
      self.orig_app = app
    end

    # Runs the thing.  Returns self so you can run join on it
    def start
      BasicSocket.do_not_reverse_lookup = true

      # inherit sockets from parents, they need to be plain Socket objects
      # before they become UNIXServer or TCPServer
      inherited = ENV['UNICORN_FD'].to_s.split(/,/).map do |fd|
        io = Socket.for_fd(fd.to_i)
        set_server_sockopt(io, listener_opts[sock_name(io)])
        IO_PURGATORY << io
        logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
        server_cast(io)
      end

      config_listeners = config[:listeners].dup
      LISTENERS.replace(inherited)

      # we start out with generic Socket objects that get cast to either
      # TCPServer or UNIXServer objects; but since the Socket objects
      # share the same OS-level file descriptor as the higher-level *Server
      # objects; we need to prevent Socket objects from being garbage-collected
      config_listeners -= listener_names
      if config_listeners.empty? && LISTENERS.empty?
        config_listeners << Unicorn::Const::DEFAULT_LISTEN
      end
      config_listeners.each { |addr| listen(addr) }
      raise ArgumentError, "no listeners" if LISTENERS.empty?
      self.pid = config[:pid]
      self.master_pid = $$
      build_app! if preload_app
      maintain_worker_count
      self
    end

    # replaces current listener set with +listeners+.  This will
    # close the socket if it will not exist in the new listener set
    def listeners=(listeners)
      cur_names, dead_names = [], []
      listener_names.each do |name|
        if "/" == name[0..0]
          # mark unlinked sockets as dead so we can rebind them
          (File.socket?(name) ? cur_names : dead_names) << name
        else
          cur_names << name
        end
      end
      set_names = listener_names(listeners)
      dead_names.concat(cur_names - set_names).uniq!

      LISTENERS.delete_if do |io|
        if dead_names.include?(sock_name(io))
          IO_PURGATORY.delete_if do |pio|
            pio.fileno == io.fileno && (pio.close rescue nil).nil? # true
          end
          (io.close rescue nil).nil? # true
        else
          set_server_sockopt(io, listener_opts[sock_name(io)])
          false
        end
      end

      (set_names - cur_names).each { |addr| listen(addr) }
    end

    def stdout_path=(path); redirect_io($stdout, path); end
    def stderr_path=(path); redirect_io($stderr, path); end

    alias_method :set_pid, :pid=
    undef_method :pid=

    # sets the path for the PID file of the master process
    def pid=(path)
      if path
        if x = valid_pid?(path)
          return path if pid && path == pid && x == $$
          raise ArgumentError, "Already running on PID:#{x} " \
                               "(or pid=#{path} is stale)"
        end
      end
      unlink_pid_safe(pid) if pid
      File.open(path, 'wb') { |fp| fp.syswrite("#$$\n") } if path
      self.set_pid(path)
    end

    # add a given address to the +listeners+ set, idempotently
    # Allows workers to add a private, per-process listener via the
    # after_fork hook.  Very useful for debugging and testing.
    def listen(address, opt = {}.merge(listener_opts[address] || {}))
      return if String === address && listener_names.include?(address)

      delay, tries = 0.5, 5
      begin
        io = bind_listen(address, opt)
        unless TCPServer === io || UNIXServer === io
          IO_PURGATORY << io
          io = server_cast(io)
        end
        logger.info "listening on addr=#{sock_name(io)} fd=#{io.fileno}"
        LISTENERS << io
        return io
      rescue Errno::EADDRINUSE => err
        logger.error "adding listener failed addr=#{address} (in use)"
        raise err if tries == 0
        tries -= 1
        logger.error "retrying in #{delay} seconds (#{tries} tries left)"
        sleep(delay)
        retry
      end
    end

    # monitors children and receives signals forever
    # (or until a termination signal is sent).  This handles signals
    # one-at-a-time time and we'll happily drop signals in case somebody
    # is signalling us too often.
    def join
      # this pipe is used to wake us up from select(2) in #join when signals
      # are trapped.  See trap_deferred
      init_self_pipe!
      respawn = true

      QUEUE_SIGS.each { |sig| trap_deferred(sig) }
      trap(:CHLD) { |sig_nr| awaken_master }
      proc_name 'master'
      logger.info "master process ready" # test_exec.rb relies on this message
      begin
        loop do
          reap_all_workers
          case SIG_QUEUE.shift
          when nil
            murder_lazy_workers
            maintain_worker_count if respawn
            master_sleep
          when :QUIT # graceful shutdown
            break
          when :TERM, :INT # immediate shutdown
            stop(false)
            break
          when :USR1 # rotate logs
            logger.info "master reopening logs..."
            Unicorn::Util.reopen_logs
            logger.info "master done reopening logs"
            kill_each_worker(:USR1)
          when :USR2 # exec binary, stay alive in case something went wrong
            reexec
          when :WINCH
            if Process.ppid == 1 || Process.getpgrp != $$
              respawn = false
              logger.info "gracefully stopping all workers"
              kill_each_worker(:QUIT)
            else
              logger.info "SIGWINCH ignored because we're not daemonized"
            end
          when :TTIN
            self.worker_processes += 1
          when :TTOU
            self.worker_processes -= 1 if self.worker_processes > 0
          when :HUP
            respawn = true
            if config.config_file
              load_config!
              redo # immediate reaping since we may have QUIT workers
            else # exec binary and exit if there's no config file
              logger.info "config_file not present, reexecuting binary"
              reexec
              break
            end
          end
        end
      rescue Errno::EINTR
        retry
      rescue Object => e
        logger.error "Unhandled master loop exception #{e.inspect}."
        logger.error e.backtrace.join("\n")
        retry
      end
      stop # gracefully shutdown all workers on our way out
      logger.info "master complete"
      unlink_pid_safe(pid) if pid
    end

    # Terminates all workers, but does not exit master process
    def stop(graceful = true)
      self.listeners = []
      kill_each_worker(graceful ? :QUIT : :TERM)
      timeleft = timeout
      step = 0.2
      reap_all_workers
      until WORKERS.empty?
        sleep(step)
        reap_all_workers
        (timeleft -= step) > 0 and next
        kill_each_worker(:KILL)
      end
    end

    private

    # list of signals we care about and trap in master.
    QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP,
                   :TTIN, :TTOU ].freeze

    # defer a signal for later processing in #join (master process)
    def trap_deferred(signal)
      trap(signal) do |sig_nr|
        if SIG_QUEUE.size < 5
          SIG_QUEUE << signal
          awaken_master
        else
          logger.error "ignoring SIG#{signal}, queue=#{SIG_QUEUE.inspect}"
        end
      end
    end

    # wait for a signal hander to wake us up and then consume the pipe
    # Wake up every second anyways to run murder_lazy_workers
    def master_sleep
      begin
        ready = IO.select([SELF_PIPE.first], nil, nil, 1) or return
        ready.first && ready.first.first or return
        loop { SELF_PIPE.first.read_nonblock(Const::CHUNK_SIZE) }
      rescue Errno::EAGAIN, Errno::EINTR
      end
    end

    def awaken_master
      begin
        SELF_PIPE.last.write_nonblock('.') # wakeup master process from select
      rescue Errno::EAGAIN, Errno::EINTR
        # pipe is full, master should wake up anyways
        retry
      end
    end

    # reaps all unreaped workers
    def reap_all_workers
      begin
        loop do
          wpid, status = Process.waitpid2(-1, Process::WNOHANG)
          wpid or break
          if reexec_pid == wpid
            logger.error "reaped #{status.inspect} exec()-ed"
            self.reexec_pid = 0
            self.pid = pid.chomp('.oldbin') if pid
            proc_name 'master'
          else
            worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
            logger.info "reaped #{status.inspect} " \
                        "worker=#{worker.nr rescue 'unknown'}"
          end
        end
      rescue Errno::ECHILD
      end
    end

    # reexecutes the START_CTX with a new binary
    def reexec
      if reexec_pid > 0
        begin
          Process.kill(0, reexec_pid)
          logger.error "reexec-ed child already running PID:#{reexec_pid}"
          return
        rescue Errno::ESRCH
          reexec_pid = 0
        end
      end

      if pid
        old_pid = "#{pid}.oldbin"
        prev_pid = pid.dup
        begin
          self.pid = old_pid  # clear the path for a new pid file
        rescue ArgumentError
          logger.error "old PID:#{valid_pid?(old_pid)} running with " \
                       "existing pid=#{old_pid}, refusing rexec"
          return
        rescue Object => e
          logger.error "error writing pid=#{old_pid} #{e.class} #{e.message}"
          return
        end
      end

      self.reexec_pid = fork do
        listener_fds = LISTENERS.map { |sock| sock.fileno }
        ENV['UNICORN_FD'] = listener_fds.join(',')
        Dir.chdir(START_CTX[:cwd])
        cmd = [ START_CTX[0] ].concat(START_CTX[:argv])

        # avoid leaking FDs we don't know about, but let before_exec
        # unset FD_CLOEXEC, if anything else in the app eventually
        # relies on FD inheritence.
        (3..1024).each do |io|
          next if listener_fds.include?(io)
          io = IO.for_fd(io) rescue nil
          io or next
          IO_PURGATORY << io
          io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
        end
        logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
        before_exec.call(self)
        exec(*cmd)
      end
      proc_name 'master (old)'
    end

    # forcibly terminate all workers that haven't checked in in timeout
    # seconds.  The timeout is implemented using an unlinked File
    # shared between the parent process and each worker.  The worker
    # runs File#chmod to modify the ctime of the File.  If the ctime
    # is stale for >timeout seconds, then we'll kill the corresponding
    # worker.
    def murder_lazy_workers
      diff = stat = nil
      WORKERS.dup.each_pair do |wpid, worker|
        stat = begin
          worker.tmp.stat
        rescue => e
          logger.warn "worker=#{worker.nr} PID:#{wpid} stat error: #{e.inspect}"
          kill_worker(:QUIT, wpid)
          next
        end
        stat.mode == 0100000 and next
        (diff = (Time.now - stat.ctime)) <= timeout and next
        logger.error "worker=#{worker.nr} PID:#{wpid} timeout " \
                     "(#{diff}s > #{timeout}s), killing"
        kill_worker(:KILL, wpid) # take no prisoners for timeout violations
      end
    end

    def spawn_missing_workers
      (0...worker_processes).each do |worker_nr|
        WORKERS.values.include?(worker_nr) and next
        begin
          Dir.chdir(START_CTX[:cwd])
        rescue Errno::ENOENT => err
          logger.fatal "#{err.inspect} (#{START_CTX[:cwd]})"
          SIG_QUEUE << :QUIT # forcibly emulate SIGQUIT
          return
        end
        worker = Worker.new(worker_nr, Unicorn::Util.tmpio)
        before_fork.call(self, worker)
        WORKERS[fork { worker_loop(worker) }] = worker
      end
    end

    def maintain_worker_count
      (off = WORKERS.size - worker_processes) == 0 and return
      off < 0 and return spawn_missing_workers
      WORKERS.dup.each_pair { |wpid,w|
        w.nr >= worker_processes and kill_worker(:QUIT, wpid) rescue nil
      }
    end

    # once a client is accepted, it is processed in its entirety here
    # in 3 easy steps: read request, call app, write app response
    def process_client(client)
      client.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
      response = app.call(env = REQUEST.read(client))

      if 100 == response.first.to_i
        client.write(Const::EXPECT_100_RESPONSE)
        env.delete(Const::HTTP_EXPECT)
        response = app.call(env)
      end

      HttpResponse.write(client, response)
    # if we get any error, try to write something back to the client
    # assuming we haven't closed the socket, but don't get hung up
    # if the socket is already closed or broken.  We'll always ensure
    # the socket is closed at the end of this function
    rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
      client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
      client.close rescue nil
    rescue HttpParserError # try to tell the client they're bad
      client.write_nonblock(Const::ERROR_400_RESPONSE) rescue nil
      client.close rescue nil
    rescue Object => e
      client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
      client.close rescue nil
      logger.error "Read error: #{e.inspect}"
      logger.error e.backtrace.join("\n")
    end

    # gets rid of stuff the worker has no business keeping track of
    # to free some resources and drops all sig handlers.
    # traps for USR1, USR2, and HUP may be set in the after_fork Proc
    # by the user.
    def init_worker_process(worker)
      QUEUE_SIGS.each { |sig| trap(sig, nil) }
      trap(:CHLD, 'DEFAULT')
      SIG_QUEUE.clear
      proc_name "worker[#{worker.nr}]"
      START_CTX.clear
      init_self_pipe!
      WORKERS.values.each { |other| other.tmp.close rescue nil }
      WORKERS.clear
      LISTENERS.each { |sock| sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
      worker.tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
      after_fork.call(self, worker) # can drop perms
      self.timeout /= 2.0 # halve it for select()
      build_app! unless preload_app
    end

    def reopen_worker_logs(worker_nr)
      logger.info "worker=#{worker_nr} reopening logs..."
      Unicorn::Util.reopen_logs
      logger.info "worker=#{worker_nr} done reopening logs"
      init_self_pipe!
    end

    # runs inside each forked worker, this sits around and waits
    # for connections and doesn't die until the parent dies (or is
    # given a INT, QUIT, or TERM signal)
    def worker_loop(worker)
      ppid = master_pid
      init_worker_process(worker)
      nr = 0 # this becomes negative if we need to reopen logs
      alive = worker.tmp # tmp is our lifeline to the master process
      ready = LISTENERS
      t = ti = 0

      # closing anything we IO.select on will raise EBADF
      trap(:USR1) { nr = -65536; SELF_PIPE.first.close rescue nil }
      trap(:QUIT) { alive = nil; LISTENERS.each { |s| s.close rescue nil } }
      [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
      logger.info "worker=#{worker.nr} ready"

      begin
        nr < 0 and reopen_worker_logs(worker.nr)
        nr = 0

        # we're a goner in timeout seconds anyways if alive.chmod
        # breaks, so don't trap the exception.  Using fchmod() since
        # futimes() is not available in base Ruby and I very strongly
        # prefer temporary files to be unlinked for security,
        # performance and reliability reasons, so utime is out.  No-op
        # changes with chmod doesn't update ctime on all filesystems; so
        # we change our counter each and every time (after process_client
        # and before IO.select).
        t == (ti = Time.now.to_i) or alive.chmod(t = ti)

        ready.each do |sock|
          begin
            process_client(sock.accept_nonblock)
            nr += 1
            t == (ti = Time.now.to_i) or alive.chmod(t = ti)
          rescue Errno::EAGAIN, Errno::ECONNABORTED
          end
          break if nr < 0
        end

        # make the following bet: if we accepted clients this round,
        # we're probably reasonably busy, so avoid calling select()
        # and do a speculative accept_nonblock on every listener
        # before we sleep again in select().
        redo unless nr == 0 # (nr < 0) => reopen logs

        ppid == Process.ppid or return
        alive.chmod(t = 0)
        begin
          # timeout used so we can detect parent death:
          ret = IO.select(LISTENERS, nil, SELF_PIPE, timeout) or redo
          ready = ret.first
        rescue Errno::EINTR
          ready = LISTENERS
        rescue Errno::EBADF
          nr < 0 or return
        end
      rescue Object => e
        if alive
          logger.error "Unhandled listen loop exception #{e.inspect}."
          logger.error e.backtrace.join("\n")
        end
      end while alive
    end

    # delivers a signal to a worker and fails gracefully if the worker
    # is no longer running.
    def kill_worker(signal, wpid)
      begin
        Process.kill(signal, wpid)
      rescue Errno::ESRCH
        worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
      end
    end

    # delivers a signal to each worker
    def kill_each_worker(signal)
      WORKERS.keys.each { |wpid| kill_worker(signal, wpid) }
    end

    # unlinks a PID file at given +path+ if it contains the current PID
    # useful as an at_exit handler.
    def unlink_pid_safe(path)
      (File.read(path).to_i == $$ and File.unlink(path)) rescue nil
    end

    # returns a PID if a given path contains a non-stale PID file,
    # nil otherwise.
    def valid_pid?(path)
      if File.exist?(path) && (wpid = File.read(path).to_i) > 1
        begin
          Process.kill(0, wpid)
          return wpid
        rescue Errno::ESRCH
        end
      end
      nil
    end

    def load_config!
      begin
        logger.info "reloading config_file=#{config.config_file}"
        config[:listeners].replace(init_listeners)
        config.reload
        config.commit!(self)
        kill_each_worker(:QUIT)
        Unicorn::Util.reopen_logs
        self.app = orig_app
        build_app! if preload_app
        logger.info "done reloading config_file=#{config.config_file}"
      rescue Object => e
        logger.error "error reloading config_file=#{config.config_file}: " \
                     "#{e.class} #{e.message}"
      end
    end

    # returns an array of string names for the given listener array
    def listener_names(listeners = LISTENERS)
      listeners.map { |io| sock_name(io) }
    end

    def build_app!
      if app.respond_to?(:arity) && app.arity == 0
        if defined?(Gem) && Gem.respond_to?(:refresh)
          logger.info "Refreshing Gem list"
          Gem.refresh
        end
        self.app = app.call
      end
    end

    def proc_name(tag)
      $0 = ([ File.basename(START_CTX[0]), tag
            ]).concat(START_CTX[:argv]).join(' ')
    end

    def redirect_io(io, path)
      File.open(path, 'a') { |fp| io.reopen(fp) } if path
      io.sync = true
    end

    def init_self_pipe!
      SELF_PIPE.each { |io| io.close rescue nil }
      SELF_PIPE.replace(IO.pipe)
      SELF_PIPE.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
    end

  end
end

git clone https://yhbt.net/unicorn.git