Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

- Preserve worker spawn deadlines when the mold starts workers.
- Fix the monitor raising `NoMethodError` when a mold exits before its `MoldReady` message is handled.

# 0.18.4

Expand Down
2 changes: 1 addition & 1 deletion lib/pitchfork/children.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def update(message)
return service
end

if child = @children[message.pid] || (message.nr && @workers[message.nr])
if child = @children[message.pid] || (message.respond_to?(:nr) && message.nr && @workers[message.nr])
if child.pid && child.pid != message.pid
# An abandoned spawn may still come up late: never rebind a slot that already has a process.
return
Expand Down
5 changes: 4 additions & 1 deletion lib/pitchfork/http_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ def monitor_handle_message(message)
message.pipe.close
end
when Message::MoldReady
new_mold = @children.update(message)
unless new_mold = @children.update(message)
logger.info("mold pid=#{message.pid} ready but already reaped, ignoring")
return
end

# current_mold may not be new_mold if there was a race
# between refork and restart.
Expand Down
42 changes: 42 additions & 0 deletions test/integration/test_reforking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,48 @@ def test_exiting_mold
assert_clean_shutdown(pid)
end

def test_mold_reaped_before_ready_message
addr, port = unused_port

pid = spawn_server(app: File.join(ROOT, "test/integration/env.ru"), config: <<~CONFIG)
listen "#{addr}:#{port}"
worker_processes 2
refork_after [5, 5]

monitor_pid = Process.pid
after_mold_fork do |_server, mold|
if mold.generation > 0 && !File.exist?("exited-once.txt")
File.write("exited-once.txt", "1")
sleep 0.01 until File.read("stderr.log").include?("pid=\#{Process.pid} spawned")
# Pause the monitor so it reaps this mold before reading its MoldReady message.
Process.kill(:STOP, monitor_pid)
def mold.finish_promotion(control_socket)
super
$stderr.puts "[mold exiting]"
exit!(0)
end
end
end
CONFIG

assert_healthy("http://#{addr}:#{port}")
assert_stderr(/worker=1 gen=0.0 pid=\d+ ready/, timeout: 5)

7.times do
assert_equal true, healthy?("http://#{addr}:#{port}")
end

assert_stderr("[mold exiting]", timeout: 3)
mold_pid = Integer(read_stderr[/mold gen=0\.1 pid=(\d+) spawned/, 1])
sleep 0.01 until File.read("/proc/#{mold_pid}/stat").rpartition(")").last.split.first == "Z"
Process.kill(:CONT, pid)

assert_stderr("mold pid=#{mold_pid} ready but already reaped, ignoring")
refute_stderr(/monitor loop error/)

assert_clean_shutdown(pid)
end

def test_stuck_mold
addr, port = unused_port

Expand Down
11 changes: 11 additions & 0 deletions test/unit/test_children.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ def test_reap_pending_mold
assert_nil @children.reap(mold.pid)
end

def test_mold_ready_after_reap
pipe = IO.pipe.last
@children.update(Message::MoldSpawned.new(nil, 42, 1, 0, pipe))
@children.reap(42)

assert_nil @children.update(Message::MoldReady.new(42))
assert_nil @children.mold
assert_equal [], @children.molds
refute_predicate @children, :pending_promotion?
end

def test_dump_load_pending_service
@children.register_service(Worker.new(nil, service: true, version: 2, generation: 3))
@children = assert_roundtrip(@children)
Expand Down