From f2dcb518d1f874627bc83ae8b3921a52eac6f896 Mon Sep 17 00:00:00 2001 From: Serena Fritsch Date: Thu, 19 Nov 2026 09:37:20 +0000 Subject: [PATCH] Add after_child_ready hook Also persist Worker#spawned_at across restarts and preserve the initial mold's spawned_at record. Co-authored-by: Jean Boussier Co-Authored-By: Claude Opus 5.5 --- docs/CONFIGURATION.md | 12 ++++++++++++ lib/pitchfork/children.rb | 4 +++- lib/pitchfork/configurator.rb | 5 +++++ lib/pitchfork/http_server.rb | 9 +++++++-- lib/pitchfork/worker.rb | 5 ++++- test/integration/test_boot.rb | 1 + test/unit/test_children.rb | 20 ++++++++++++++++++-- test/unit/test_worker.rb | 1 + 8 files changed, 51 insertions(+), 6 deletions(-) diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 4b6fba79..0878c089 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -398,6 +398,18 @@ issues caused by forking. If you detect something wrong, you can call `Process.exit`, and this mold won't be used, another one will be spawned later. e.g. you can check `Socket.getaddrinfo` still works, etc. + +### `after_child_ready` + +Called by the monitor process when a worker, service or mold reports +ready. Outdated molds discarded by the monitor don't trigger it. + +```ruby +after_child_ready do |server, child| + server.logger.info("#{child.to_log} ready") +end +``` + ### `after_worker_fork` ```ruby diff --git a/lib/pitchfork/children.rb b/lib/pitchfork/children.rb index 4280d46d..6c23cfc1 100644 --- a/lib/pitchfork/children.rb +++ b/lib/pitchfork/children.rb @@ -85,7 +85,9 @@ def fetch(pid) def update(message) case message when Message::MoldSpawned - mold = Worker.new(nil) + # The initial mold PID is already known + # but for subsequent ones, we had no record of it. + mold = @children[message.pid] || Worker.new(nil) mold.update(message) @pending_molds[mold.pid] = mold @children[mold.pid] = mold diff --git a/lib/pitchfork/configurator.rb b/lib/pitchfork/configurator.rb index 08c16386..f61b1788 100644 --- a/lib/pitchfork/configurator.rb +++ b/lib/pitchfork/configurator.rb @@ -43,6 +43,7 @@ class Configurator :before_fork => nil, :after_worker_fork => nil, :after_mold_fork => nil, + :after_child_ready => nil, :before_worker_exit => nil, :after_worker_exit => lambda { |server, worker, status| m = if worker.nil? @@ -155,6 +156,10 @@ def after_monitor_ready(*args, &block) set_hook(:after_monitor_ready, block_given? ? block : args[0], 1) end + def after_child_ready(*args, &block) + set_hook(:after_child_ready, block_given? ? block : args[0]) + end + def after_worker_timeout(*args, &block) set_hook(:after_worker_timeout, block_given? ? block : args[0], 3) end diff --git a/lib/pitchfork/http_server.rb b/lib/pitchfork/http_server.rb index 5056e098..30e55e5d 100644 --- a/lib/pitchfork/http_server.rb +++ b/lib/pitchfork/http_server.rb @@ -89,7 +89,7 @@ def extend_deadline(extra_time) :listener_opts, :children, :poller, :orig_app, :config, :ready_pipe, :early_hints, :setpgid, :restart_command, :restart_command_argv, :working_directory attr_writer :after_worker_exit, :before_worker_exit, :after_worker_ready, :after_request_complete, - :refork_condition, :after_worker_timeout, :after_worker_hard_timeout, :after_monitor_ready, :refork_max_unavailable, + :refork_condition, :after_worker_timeout, :after_worker_hard_timeout, :after_monitor_ready, :after_child_ready, :refork_max_unavailable, :max_consecutive_spawn_errors attr_reader :logger, :version @@ -233,7 +233,9 @@ def start(sync = true) else build_app! SharedMemory.current_version = @version - after_mold_fork&.call(self, Worker.new(nil, pid: $$).promoted!(@spawn_timeout)) + mold = Worker.new(nil, pid: $$).promoted!(@spawn_timeout) + after_mold_fork&.call(self, mold) + safe_run_callback(@after_child_ready, "after_child_ready", self, mold) end if sync @@ -449,9 +451,12 @@ def monitor_handle_message(message) mold.soft_kill(:TERM) end end + safe_run_callback(@after_child_ready, "after_child_ready", self, new_mold) end when Message::WorkerReady, Message::ServiceReady @consecutive_spawn_errors = 0 + child = @children.fetch(message.pid) rescue nil + safe_run_callback(@after_child_ready, "after_child_ready", self, child) if child else logger.error("Unexpected message in sig_queue #{message.inspect}") end diff --git a/lib/pitchfork/worker.rb b/lib/pitchfork/worker.rb index 4ec7d81f..cab08c75 100644 --- a/lib/pitchfork/worker.rb +++ b/lib/pitchfork/worker.rb @@ -14,7 +14,7 @@ class Worker # :stopdoc: EXIT_SIGNALS = [:QUIT, :TERM] attr_accessor :nr, :pid, :generation, :version - attr_reader :monitor, :requests_count + attr_reader :monitor, :requests_count, :spawned_at class << self def load(data) @@ -34,6 +34,7 @@ def initialize(nr, pid: nil, generation: 0, version: 0, service: false) @monitor = nil @exiting = false @requests_count = 0 + @spawned_at = Pitchfork.time_now if nr @state_drop = SharedMemory.worker_state(@nr) elsif service? @@ -56,6 +57,7 @@ def init_with(data) @monitor = fd ? MessageSocket.for_fd(fd) : nil @exiting = data.fetch(:exiting) @requests_count = data.fetch(:requests_count) + @spawned_at = data.fetch(:spawned_at, 0) @state_drop = if @nr SharedMemory.worker_state(@nr) @@ -84,6 +86,7 @@ def dump monitor_fd: @monitor&.fileno, exiting: exiting?, requests_count: requests_count, + spawned_at: spawned_at, } end diff --git a/test/integration/test_boot.rb b/test/integration/test_boot.rb index c30db58f..fc919ea9 100644 --- a/test/integration/test_boot.rb +++ b/test/integration/test_boot.rb @@ -254,6 +254,7 @@ def server.murder_lazy_workers abandoned_worker_pid = read_stderr.match(/worker=0 gen=0.0 pid=(\d+) spawned but slot is taken/)[1] assert_stderr(/reaped unknown subprocess .* pid #{abandoned_worker_pid} exit 0/) + refute_stderr(/monitor loop error/) assert_clean_shutdown(pid) end diff --git a/test/unit/test_children.rb b/test/unit/test_children.rb index 3dc5d8de..4d680813 100644 --- a/test/unit/test_children.rb +++ b/test/unit/test_children.rb @@ -261,8 +261,8 @@ def test_dump_load assert_equal [worker], @children.workers pipe = Pitchfork.socketpair.last - @children.update(Message::MoldSpawned.new(nil, 42, 1, 0, pipe)) - @children.update(Message::MoldReady.new(42)) + @children.update(Message::MoldSpawned.new(nil, 44, 1, 0, pipe)) + @children.update(Message::MoldReady.new(44)) assert_not_nil @children.mold @children = assert_roundtrip(@children) @@ -277,6 +277,22 @@ def test_dump_load assert_not_nil @children.service end + def test_mold_ready_spawned_at_preserved + pipe = Pitchfork.socketpair.last + + mold = Worker.new(nil, pid: 42) # Initial mold pid is known + spawned_at = mold.spawned_at + @children.register_mold(mold) + + returned_mold = @children.update(Message::MoldSpawned.new(nil, 42, 0, 0, pipe)) + assert_same mold, returned_mold + assert_same spawned_at, returned_mold.spawned_at + + returned_mold = @children.update(Message::MoldReady.new(42)) + assert_same mold, returned_mold + assert_same spawned_at, returned_mold.spawned_at + end + private def assert_roundtrip(children) diff --git a/test/unit/test_worker.rb b/test/unit/test_worker.rb index a59b38cd..ccb5eace 100644 --- a/test/unit/test_worker.rb +++ b/test/unit/test_worker.rb @@ -91,6 +91,7 @@ def test_dump_load mold: false, mold_ready: false, service: false, + spawned_at: worker.spawned_at, nr: 2, pid: 1234, requests_count: 0,