Skip to content

Graceful stop is aborted when workers are signalled directly as well as by the controller (e.g. Heroku) #80

Open

Description

@willcosgrove

AI disclaimer: I worked on this with Claude, but I've edited the text and confirmed the reproduction myself.

When a platform signals every process rather than just the parent, a forked worker gets two stop signals: the platform's SIGTERM, and the SIGINT the controller sends as it stops the container. Forked maps both to Interrupt in the child, and the second one, arriving during the graceful drain, ends the drain. In-flight responses are cut off mid-body.

Heroku does this on every deploy and dyno restart, so each in-flight response is truncated (its router logs an H18 "Server Request Interrupted" for each).

Versions

falcon 0.57.0, async-container 0.38.0, async 2.46.0, async-http 0.105.0, async-service 0.25.0, io-event 1.22.1, Ruby 4.0.9. Reproduced on macOS; we see the same behavior in production on Heroku.

Reproduction

falcon host with one worker, and a response that streams 6 chunks, one per second. SIGTERM is sent 2s in.

SIGTERM to falcon host only (ruby repro.rb parent):

 2.0s  SIGTERM to falcon host only
 6.0s  /slow closed (EOFError): complete response, 6/6 chunks
 6.1s  falcon host exited (pid 35084 exit 0)

SIGTERM to the whole process group, parent and worker (ruby repro.rb group):

 2.0s  SIGTERM to the whole process group (parent and workers, as Heroku does)
 2.0s  /slow closed (EOFError): TRUNCATED mid-response, 2/6 chunks
 2.1s  falcon host exited (pid 35097 exit 0)
falcon.rb, config.ru and repro.rb

falcon.rb:

#!/usr/bin/env -S falcon host
require "falcon/environment/rack"

service "repro" do
  include Falcon::Environment::Rack
  count 1
  port { 9393 }
  endpoint do
    Async::HTTP::Endpoint.parse("http://127.0.0.1:#{port}").with(protocol: Async::HTTP::Protocol::HTTP11)
  end
end

config.ru:

run do |env|
  body = proc do |stream|
    6.times do |i|
      stream.write("chunk #{i}\n")
      sleep 1
    end
  ensure
    stream.close
  end
  [200, { "content-type" => "text/plain" }, body]
end

repro.rb:

require "socket"
$stdout.sync = true
PORT = 9393
mode = ARGV.fetch(0) # "parent" or "group"

server = Process.spawn("bundle", "exec", "falcon", "host", "falcon.rb", chdir: __dir__, pgroup: true)
100.times do
  TCPSocket.new("127.0.0.1", PORT).close
  break
rescue Errno::ECONNREFUSED
  sleep 0.1
end
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
elapsed = -> { format("%4.1fs", Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) }

client = Thread.new do
  socket = TCPSocket.new("127.0.0.1", PORT)
  socket.write("GET /slow HTTP/1.1\r\nHost: localhost\r\n\r\n")
  data = +""
  begin
    loop { data << socket.readpartial(4096) }
  rescue EOFError, Errno::ECONNRESET => error
    ending = data.end_with?("0\r\n\r\n") ? "complete response" : "TRUNCATED mid-response"
    puts "#{elapsed.call}  /slow closed (#{error.class}): #{ending}, #{data.scan(/chunk \d/).size}/6 chunks"
  end
end

sleep 2
if mode == "group"
  puts "#{elapsed.call}  SIGTERM to the whole process group (parent and workers, as Heroku does)"
  Process.kill("TERM", -Process.getpgid(server))
else
  puts "#{elapsed.call}  SIGTERM to falcon host only"
  Process.kill("TERM", server)
end
client.join(30)
Process.wait(server)
puts "#{elapsed.call}  falcon host exited (#{$?})"

Workaround

We ignore SIGTERM in each worker, so the controller's SIGINT is the only stop signal it sees. This runs from a Falcon::Server#run prepend, so it happens after the worker's traps are installed:

module FalconWorkerSignals
  def run
    previous = Signal.trap(:TERM, "IGNORE")
    # Only replace async-container's worker trap (a Proc); otherwise put it back.
    Signal.trap(:TERM, previous) unless previous.respond_to?(:call)
    super
  end
end

Falcon::Server.prepend(FalconWorkerSignals)

We ran this in production for several weeks with zero deploy-time H18s. They came back when we took it out.

Related

#49 / #50 made SIGTERM graceful (v0.30.0), which is what we want from Heroku. #51 describes this same double-up for Ctrl-C, where the terminal signals the whole foreground process group. I don't think process groups cover this case though: Heroku sends SIGTERM to every process in the dyno regardless of group.

Possible fix

Make a second Interrupt a no-op in a child that is already stopping gracefully. That would cover Ctrl-C, Heroku, and anything else that signals workers directly, without depending on who sent the signal. I haven't traced exactly where the second Interrupt aborts the drain. Happy to put together a PR if that direction sounds right.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions