pitchfork 0.18.4 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5f190e470ac20e051cdb12eed6c3c0e955598b75cec7b513c4511e0aafc0e5e
4
- data.tar.gz: e4aff5cfe446b5df2dbd7f22785f6d426b7933e14fe9ffa2b72ed5f18114e8b8
3
+ metadata.gz: 8dedbbeaa108f658fe9d990a823b36a3b9eec67755a6b7597b442ad2cf498ba0
4
+ data.tar.gz: a3041401ab5fae5a3ab2c355d670f49138ef640445154d3543aae2ba2543cc03
5
5
  SHA512:
6
- metadata.gz: f60b1e3a82d781f487988c5bb64ade082abc07e8217fc1e2bac2b8a26686bff47fd8546dd0e2d3c82ecc49696a8d42d991074bc6d12cfb1a27a49c20dbf34dc6
7
- data.tar.gz: ddd7b722730ea28cc7256350ef813e0246eab6b78547c46e7b0c10710a36a0a8e4ea561bafe8827385ddf515a53a450390983efeb6002a102cd41ec9108b0bda
6
+ metadata.gz: 70443c44f2bdad96da75e79ed9d4bc272af92237374b8bcc29cb7791b29690c52ca799d0ca6739d2e200ce93beae16307aa9f98428d93e4945ffab70246d48e4
7
+ data.tar.gz: b286169b61679fb26bf168f041e19a6d294c4f26ad567eca96567ec0f425cd6c0eba0337870dbe6588f433bb03544f4b64ab339727aa271f0a944df32ad9ad64
@@ -0,0 +1,9 @@
1
+ {
2
+ "features": {
3
+ "ghcr.io/devcontainers/features/github-cli:1": {
4
+ "version": "1.1.3",
5
+ "resolved": "ghcr.io/devcontainers/features/github-cli@sha256:bd7ab48a832228f633239277552c30b353867fef2e5b037e064b4e64f0b843f2",
6
+ "integrity": "sha256:bd7ab48a832228f633239277552c30b353867fef2e5b037e064b4e64f0b843f2"
7
+ }
8
+ }
9
+ }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Unreleased
2
2
 
3
+ # 0.19.0
4
+
5
+ - Add `after_child_ready` hook.
6
+ - Fix the monitor using a full CPU core while it waits for workers during a graceful shutdown.
7
+ - Fix a second `QUIT` or `TERM` during a graceful shutdown making the monitor exit before pending or running workers.
8
+ - Fix `TTIN` during a graceful shutdown spawning new workers.
9
+ - Prevent `USR1` from restarting the monitor during a graceful shutdown.
10
+ - Honor shutdown signals while the server is starting.
11
+ - Fix `max_consecutive_spawn_errors` counting a timed out spawn twice.
12
+ - Preserve worker spawn deadlines when the mold starts workers.
13
+ - Fix the monitor raising `NoMethodError` when a mold exits before its `MoldReady` message is handled.
14
+ - Allow future versions to add optional trailing message fields during hot restarts.
15
+
3
16
  # 0.18.4
4
17
 
5
18
  - Avoid stale body source pointers when Ruby compacts strings.
@@ -398,6 +398,18 @@ issues caused by forking. If you detect something wrong, you can
398
398
  call `Process.exit`, and this mold won't be used, another one will be
399
399
  spawned later. e.g. you can check `Socket.getaddrinfo` still works, etc.
400
400
 
401
+
402
+ ### `after_child_ready`
403
+
404
+ Called by the monitor process when a worker, service or mold reports
405
+ ready. Outdated molds discarded by the monitor don't trigger it.
406
+
407
+ ```ruby
408
+ after_child_ready do |server, child|
409
+ server.logger.info("#{child.to_log} ready")
410
+ end
411
+ ```
412
+
401
413
  ### `after_worker_fork`
402
414
 
403
415
  ```ruby
@@ -85,7 +85,9 @@ module Pitchfork
85
85
  def update(message)
86
86
  case message
87
87
  when Message::MoldSpawned
88
- mold = Worker.new(nil)
88
+ # The initial mold PID is already known
89
+ # but for subsequent ones, we had no record of it.
90
+ mold = @children[message.pid] || Worker.new(nil)
89
91
  mold.update(message)
90
92
  @pending_molds[mold.pid] = mold
91
93
  @children[mold.pid] = mold
@@ -97,7 +99,7 @@ module Pitchfork
97
99
  return service
98
100
  end
99
101
 
100
- if child = @children[message.pid] || (message.nr && @workers[message.nr])
102
+ if child = @children[message.pid] || (message.respond_to?(:nr) && message.nr && @workers[message.nr])
101
103
  if child.pid && child.pid != message.pid
102
104
  # An abandoned spawn may still come up late: never rebind a slot that already has a process.
103
105
  return
@@ -43,6 +43,7 @@ module Pitchfork
43
43
  :before_fork => nil,
44
44
  :after_worker_fork => nil,
45
45
  :after_mold_fork => nil,
46
+ :after_child_ready => nil,
46
47
  :before_worker_exit => nil,
47
48
  :after_worker_exit => lambda { |server, worker, status|
48
49
  m = if worker.nil?
@@ -155,6 +156,10 @@ module Pitchfork
155
156
  set_hook(:after_monitor_ready, block_given? ? block : args[0], 1)
156
157
  end
157
158
 
159
+ def after_child_ready(*args, &block)
160
+ set_hook(:after_child_ready, block_given? ? block : args[0])
161
+ end
162
+
158
163
  def after_worker_timeout(*args, &block)
159
164
  set_hook(:after_worker_timeout, block_given? ? block : args[0], 3)
160
165
  end
@@ -89,7 +89,7 @@ module Pitchfork
89
89
  :listener_opts, :children, :poller,
90
90
  :orig_app, :config, :ready_pipe, :early_hints, :setpgid, :restart_command, :restart_command_argv, :working_directory
91
91
  attr_writer :after_worker_exit, :before_worker_exit, :after_worker_ready, :after_request_complete,
92
- :refork_condition, :after_worker_timeout, :after_worker_hard_timeout, :after_monitor_ready, :refork_max_unavailable,
92
+ :refork_condition, :after_worker_timeout, :after_worker_hard_timeout, :after_monitor_ready, :after_child_ready, :refork_max_unavailable,
93
93
  :max_consecutive_spawn_errors
94
94
 
95
95
  attr_reader :logger, :version
@@ -112,6 +112,7 @@ module Pitchfork
112
112
  @exit_status = 0
113
113
  @app = app
114
114
  @respawn = false
115
+ @graceful_shutdown = true
115
116
  @refork_max_unavailable = nil
116
117
  @consecutive_spawn_errors = 0
117
118
  @max_consecutive_spawn_errors = nil
@@ -226,14 +227,17 @@ module Pitchfork
226
227
 
227
228
  if REFORKING_AVAILABLE
228
229
  spawn_initial_mold
229
- wait_for_pending_workers
230
+ return self if wait_for_pending_workers == StopIteration
230
231
  unless @children.mold
231
232
  raise BootFailure, "The initial mold failed to boot"
232
233
  end
233
234
  else
235
+ mold = Worker.new(nil, pid: $$)
234
236
  build_app!
235
237
  SharedMemory.current_version = @version
236
- after_mold_fork&.call(self, Worker.new(nil, pid: $$).promoted!(@spawn_timeout))
238
+ mold.promoted!(@spawn_timeout)
239
+ after_mold_fork&.call(self, mold)
240
+ safe_run_callback(@after_child_ready, "after_child_ready", self, mold)
237
241
  end
238
242
 
239
243
  if sync
@@ -241,7 +245,7 @@ module Pitchfork
241
245
  # We could just return here as we'd register them later in #join.
242
246
  # However a good part of the test suite assumes #start only return
243
247
  # once all initial workers are spawned.
244
- wait_for_pending_workers
248
+ return self if wait_for_pending_workers == StopIteration
245
249
  end
246
250
 
247
251
  @after_monitor_ready&.call(self)
@@ -324,7 +328,7 @@ module Pitchfork
324
328
  # one-at-a-time time and we'll happily drop signals in case somebody
325
329
  # is signalling us too often.
326
330
  def join
327
- @respawn = true
331
+ @respawn = !SharedMemory.shutting_down?
328
332
 
329
333
  proc_name role: 'monitor', status: ARGV.join(' ')
330
334
 
@@ -336,7 +340,7 @@ module Pitchfork
336
340
  end
337
341
  @ready_pipe = @ready_pipe.close rescue nil
338
342
  end
339
- while true
343
+ until SharedMemory.shutting_down?
340
344
  begin
341
345
  if monitor_loop == StopIteration
342
346
  break
@@ -345,7 +349,7 @@ module Pitchfork
345
349
  Pitchfork.log_error(@logger, "monitor loop error", e)
346
350
  end
347
351
  end
348
- stop # gracefully shutdown all workers on our way out
352
+ stop
349
353
  logger.info "monitor complete status=#{@exit_status}"
350
354
  @exit_status
351
355
  end
@@ -376,8 +380,7 @@ module Pitchfork
376
380
  if @max_consecutive_spawn_errors && @consecutive_spawn_errors > @max_consecutive_spawn_errors && !SharedMemory.shutting_down?
377
381
  logger.fatal("#{@consecutive_spawn_errors} consecutive failures to spawn children, aborting - broken after_worker_fork callback?")
378
382
  @exit_status = 1
379
- SharedMemory.shutting_down!
380
- stop(false)
383
+ request_shutdown(false)
381
384
  return StopIteration
382
385
  end
383
386
  else
@@ -393,17 +396,17 @@ module Pitchfork
393
396
 
394
397
  def monitor_handle_message(message)
395
398
  case message
396
- when :QUIT, :TERM # graceful shutdown
397
- SharedMemory.shutting_down!
398
- logger.info "#{message} received, starting graceful shutdown"
399
- return StopIteration
400
- when :INT # immediate shutdown
401
- SharedMemory.shutting_down!
402
- logger.info "#{message} received, starting immediate shutdown"
403
- stop(false)
399
+ when :QUIT, :TERM, :INT
400
+ graceful = message != :INT
401
+ request_shutdown(graceful)
402
+ logger.info "#{message} received, starting #{graceful ? 'graceful' : 'immediate'} shutdown"
404
403
  return StopIteration
405
404
  when :USR1 # trigger a hot restart
406
- trigger_restart
405
+ if SharedMemory.shutting_down?
406
+ logger.error "Can't trigger a restart as the server is shutting down"
407
+ else
408
+ trigger_restart
409
+ end
407
410
  when :USR2 # trigger a promotion
408
411
  if @respawn
409
412
  trigger_refork
@@ -411,7 +414,6 @@ module Pitchfork
411
414
  logger.error "Can't trigger a refork as the server is shutting down"
412
415
  end
413
416
  when :TTIN
414
- @respawn = true
415
417
  self.worker_processes += 1
416
418
  when :TTOU
417
419
  self.worker_processes -= 1 if self.worker_processes > 0
@@ -430,7 +432,10 @@ module Pitchfork
430
432
  message.pipe.close
431
433
  end
432
434
  when Message::MoldReady
433
- new_mold = @children.update(message)
435
+ unless new_mold = @children.update(message)
436
+ logger.info("mold pid=#{message.pid} ready but already reaped, ignoring")
437
+ return
438
+ end
434
439
 
435
440
  # current_mold may not be new_mold if there was a race
436
441
  # between refork and restart.
@@ -449,9 +454,12 @@ module Pitchfork
449
454
  mold.soft_kill(:TERM)
450
455
  end
451
456
  end
457
+ safe_run_callback(@after_child_ready, "after_child_ready", self, new_mold)
452
458
  end
453
459
  when Message::WorkerReady, Message::ServiceReady
454
460
  @consecutive_spawn_errors = 0
461
+ child = @children.fetch(message.pid) rescue nil
462
+ safe_run_callback(@after_child_ready, "after_child_ready", self, child) if child
455
463
  else
456
464
  logger.error("Unexpected message in sig_queue #{message.inspect}")
457
465
  end
@@ -461,20 +469,19 @@ module Pitchfork
461
469
  def stop(graceful = true)
462
470
  proc_name role: 'monitor', status: 'shutting down'
463
471
  @respawn = false
464
- SharedMemory.shutting_down!
465
- wait_for_pending_workers
472
+ request_shutdown(graceful)
473
+ wait_for_pending_workers(stop_on_signal: false)
466
474
  Pitchfork.close_listeners
467
475
 
468
476
  limit = Pitchfork.time_now + timeout
469
477
  until @children.empty? || Pitchfork.time_now > limit
470
- if graceful
478
+ if @graceful_shutdown
471
479
  @children.soft_kill_all(:TERM)
472
480
  else
473
481
  @children.hard_kill_all(:INT)
474
482
  end
475
- if monitor_loop(false) == StopIteration
476
- return StopIteration
477
- end
483
+ monitor_sleep(0.1)
484
+ monitor_loop(false)
478
485
  end
479
486
 
480
487
  @children.each do |child|
@@ -528,6 +535,11 @@ module Pitchfork
528
535
 
529
536
  private
530
537
 
538
+ def request_shutdown(graceful)
539
+ @graceful_shutdown = false unless graceful
540
+ SharedMemory.shutting_down!
541
+ end
542
+
531
543
  def refork_max_unavailable
532
544
  @refork_max_unavailable ||= (worker_processes * 0.1).ceil
533
545
  end
@@ -614,9 +626,8 @@ module Pitchfork
614
626
  end
615
627
 
616
628
  def hard_timeout(child)
617
- @consecutive_spawn_errors += 1 unless child.ready?
618
-
619
629
  if child.pid.nil? # Not yet registered, likely never spawned
630
+ @consecutive_spawn_errors += 1
620
631
  logger.error "#{child.to_log} timed out during spawn, abandoning"
621
632
  @children.abandon(child)
622
633
  return
@@ -847,8 +858,10 @@ module Pitchfork
847
858
  generation = 0
848
859
  end
849
860
 
861
+ # Only the monitor resets shared state; the mold attaches to the same slots.
850
862
  if @before_service_worker_ready && !@children.service
851
863
  service = Worker.new(nil, service: true, version: version, generation: generation)
864
+ service.reset_state!
852
865
  spawn_service_from_mold(service)
853
866
  end
854
867
 
@@ -861,7 +874,9 @@ module Pitchfork
861
874
  next
862
875
  end
863
876
 
864
- missing_workers << Worker.new(worker_nr, version: version, generation: generation)
877
+ worker = Worker.new(worker_nr, version: version, generation: generation)
878
+ worker.reset_state!
879
+ missing_workers << worker
865
880
  end
866
881
 
867
882
  unless missing_workers.empty?
@@ -872,10 +887,10 @@ module Pitchfork
872
887
  exit!
873
888
  end
874
889
 
875
- def wait_for_pending_workers
890
+ def wait_for_pending_workers(stop_on_signal: true)
876
891
  while @children.pending_workers?
877
892
  monitor_sleep(0.5)
878
- if monitor_loop(false) == StopIteration
893
+ if monitor_loop(false) == StopIteration && stop_on_signal
879
894
  return StopIteration
880
895
  end
881
896
  end
@@ -1069,6 +1084,12 @@ module Pitchfork
1069
1084
  [:QUIT, :TERM, :INT].each { |sig| trap(sig) { exit!(0) } }
1070
1085
  end
1071
1086
 
1087
+ def register_child_and_exit_if_shutting_down(child)
1088
+ # Register first so the monitor can account for the child during shutdown.
1089
+ child.register_to_monitor(@control_socket[1])
1090
+ exit!(0) if SharedMemory.shutting_down?
1091
+ end
1092
+
1072
1093
  # gets rid of stuff the worker has no business keeping track of
1073
1094
  # to free some resources and drops all sig handlers.
1074
1095
  # traps for USR2, and HUP may be set in the after_worker_fork/after_mold_fork Procs
@@ -1076,7 +1097,7 @@ module Pitchfork
1076
1097
  def init_worker_process(worker)
1077
1098
  proc_name role: "(#{worker.version}.#{worker.generation}) worker[#{worker.nr}]", status: "init"
1078
1099
  worker.reset
1079
- worker.register_to_monitor(@control_socket[1])
1100
+ register_child_and_exit_if_shutting_down(worker)
1080
1101
  # we'll re-trap :QUIT and :TERM later for graceful shutdown iff we accept clients
1081
1102
  exit_sigs = [ :QUIT, :TERM, :INT ]
1082
1103
  exit_sigs.each { |sig| trap(sig) { exit!(0) } }
@@ -1102,7 +1123,7 @@ module Pitchfork
1102
1123
  def init_service_process(service)
1103
1124
  proc_name role: "(#{service.version}.#{service.generation}) service", status: "init"
1104
1125
  Pitchfork.close_listeners # Don't appear as listening to incoming requests
1105
- service.register_to_monitor(@control_socket[1])
1126
+ register_child_and_exit_if_shutting_down(service)
1106
1127
  readers = [service]
1107
1128
  trap(:QUIT) { nuke_listeners!(readers) }
1108
1129
  trap(:TERM) { nuke_listeners!(readers) }
@@ -120,9 +120,20 @@ module Pitchfork
120
120
  end
121
121
 
122
122
  klass, *args = Marshal.load(payload)
123
+ field_count = klass.members.length
124
+ fds = data&.unix_rights
125
+
126
+ if args.length > field_count
127
+ args.slice!(field_count, args.length).each do |arg|
128
+ fds.fetch(arg.index).close if arg.is_a?(FD)
129
+ end
130
+ else
131
+ args.fill(nil, args.length, field_count - args.length)
132
+ end
133
+
123
134
  args.map! do |arg|
124
135
  if arg.is_a?(FD)
125
- data.unix_rights.fetch(arg.index)
136
+ fds.fetch(arg.index)
126
137
  else
127
138
  arg
128
139
  end
@@ -171,6 +182,16 @@ module Pitchfork
171
182
  end
172
183
  end
173
184
 
185
+ # Message schemas are append-only: new fields must be optional and added at
186
+ # the end, without changing existing fields. Receivers ignore unknown
187
+ # trailing fields and treat missing trailing fields as nil; code using new
188
+ # fields must handle nil from older senders.
189
+ #
190
+ # Before deploying a version that sends new fields, first upgrade all running
191
+ # processes to a version that supports extensible messages. Earlier versions
192
+ # cannot receive additional fields. Stop the server before rolling back to a
193
+ # version without extensible messages.
194
+
174
195
  SpawnWorkers = new(:nrs)
175
196
  WorkerSpawned = new(:nr, :pid, :generation, :version, :pipe)
176
197
  WorkerReady = new(:nr, :pid)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pitchfork
4
- VERSION = "0.18.4"
4
+ VERSION = "0.19.0"
5
5
  module Const
6
6
  UNICORN_VERSION = '6.1.0'
7
7
  end
@@ -14,7 +14,7 @@ module Pitchfork
14
14
  # :stopdoc:
15
15
  EXIT_SIGNALS = [:QUIT, :TERM]
16
16
  attr_accessor :nr, :pid, :generation, :version
17
- attr_reader :monitor, :requests_count
17
+ attr_reader :monitor, :requests_count, :spawned_at
18
18
 
19
19
  class << self
20
20
  def load(data)
@@ -34,7 +34,14 @@ module Pitchfork
34
34
  @monitor = nil
35
35
  @exiting = false
36
36
  @requests_count = 0
37
- init_state
37
+ @spawned_at = Pitchfork.time_now
38
+ if nr
39
+ @state_drop = SharedMemory.worker_state(@nr)
40
+ elsif service?
41
+ @state_drop = SharedMemory.service_state
42
+ else
43
+ promoted!(nil)
44
+ end
38
45
  end
39
46
 
40
47
  def init_with(data)
@@ -50,6 +57,7 @@ module Pitchfork
50
57
  @monitor = fd ? MessageSocket.for_fd(fd) : nil
51
58
  @exiting = data.fetch(:exiting)
52
59
  @requests_count = data.fetch(:requests_count)
60
+ @spawned_at = data.fetch(:spawned_at, 0)
53
61
 
54
62
  @state_drop = if @nr
55
63
  SharedMemory.worker_state(@nr)
@@ -78,6 +86,7 @@ module Pitchfork
78
86
  monitor_fd: @monitor&.fileno,
79
87
  exiting: exiting?,
80
88
  requests_count: requests_count,
89
+ spawned_at: spawned_at,
81
90
  }
82
91
  end
83
92
 
@@ -306,6 +315,10 @@ module Pitchfork
306
315
  @to_io = nil
307
316
  end
308
317
 
318
+ def reset_state!
319
+ @state_drop.reset!
320
+ end
321
+
309
322
  def reaped!
310
323
  close
311
324
  # Not ideal: All molds share two state_drops, so when reaping a mold
@@ -314,7 +327,7 @@ module Pitchfork
314
327
  # unix sockets, we could consider assigning a unique page for each mold to solve
315
328
  # this problem.
316
329
  unless mold?
317
- @state_drop.reset!
330
+ reset_state!
318
331
  end
319
332
  end
320
333
 
@@ -342,18 +355,6 @@ module Pitchfork
342
355
 
343
356
  private
344
357
 
345
- def init_state
346
- if nr
347
- @state_drop = SharedMemory.worker_state(@nr)
348
- @state_drop.reset!
349
- elsif service?
350
- @state_drop = SharedMemory.service_state
351
- @state_drop.reset!
352
- else
353
- promoted!(nil)
354
- end
355
- end
356
-
357
358
  def pipe=(socket)
358
359
  raise ArgumentError, "pipe can't be nil" unless socket
359
360
  Info.keep_io(socket)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pitchfork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.4
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
@@ -48,6 +48,7 @@ extensions:
48
48
  - ext/pitchfork_http/extconf.rb
49
49
  extra_rdoc_files: []
50
50
  files:
51
+ - ".devcontainer/devcontainer-lock.json"
51
52
  - ".devcontainer/devcontainer.json"
52
53
  - ".git-blame-ignore-revs"
53
54
  - ".gitattributes"