puma 8.0.2 → 8.1.2

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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +154 -0
  3. data/README.md +28 -378
  4. data/docs/binding.md +135 -0
  5. data/docs/cluster_mode.md +241 -0
  6. data/docs/configuration.md +78 -0
  7. data/docs/{stats.md → control_server.md} +28 -8
  8. data/docs/{deployment.md → deployment/README.md} +23 -7
  9. data/docs/{kubernetes.md → deployment/kubernetes.md} +4 -4
  10. data/docs/{systemd.md → deployment/systemd.md} +4 -2
  11. data/docs/plugins.md +62 -3
  12. data/docs/puma_support.md +92 -0
  13. data/docs/rails_dev_mode.md +2 -2
  14. data/docs/restart.md +8 -3
  15. data/docs/signals.md +4 -2
  16. data/docs/{5.0-Upgrade.md → upgrading/5.0-Upgrade.md} +3 -3
  17. data/docs/{6.0-Upgrade.md → upgrading/6.0-Upgrade.md} +3 -3
  18. data/docs/{7.0-Upgrade.md → upgrading/7.0-Upgrade.md} +2 -2
  19. data/docs/{8.0-Upgrade.md → upgrading/8.0-Upgrade.md} +4 -4
  20. data/ext/puma_http11/extconf.rb +2 -0
  21. data/ext/puma_http11/http11_parser.c +6 -2
  22. data/ext/puma_http11/mini_ssl.c +18 -7
  23. data/ext/puma_http11/puma_http11.c +28 -3
  24. data/lib/puma/binder.rb +29 -0
  25. data/lib/puma/cli.rb +6 -0
  26. data/lib/puma/client.rb +178 -44
  27. data/lib/puma/client_env.rb +32 -18
  28. data/lib/puma/cluster/worker.rb +109 -33
  29. data/lib/puma/cluster/worker_handle.rb +16 -0
  30. data/lib/puma/cluster.rb +173 -45
  31. data/lib/puma/commonlogger.rb +3 -3
  32. data/lib/puma/configuration.rb +23 -3
  33. data/lib/puma/const.rb +14 -9
  34. data/lib/puma/control_cli.rb +14 -3
  35. data/lib/puma/dsl.rb +279 -92
  36. data/lib/puma/launcher.rb +6 -4
  37. data/lib/puma/minissl.rb +98 -56
  38. data/lib/puma/plugin/systemd.rb +1 -1
  39. data/lib/puma/reactor.rb +35 -3
  40. data/lib/puma/response.rb +28 -6
  41. data/lib/puma/runner.rb +5 -1
  42. data/lib/puma/sd_notify.rb +4 -1
  43. data/lib/puma/server.rb +131 -35
  44. data/lib/puma/thread_pool.rb +12 -1
  45. data/lib/puma/util.rb +8 -4
  46. data/lib/puma.rb +1 -1
  47. metadata +19 -31
  48. data/docs/fork_worker.md +0 -41
  49. data/docs/grpc.md +0 -62
  50. data/docs/jungle/README.md +0 -9
  51. data/docs/testing_benchmarks_local_files.md +0 -150
  52. data/docs/testing_test_rackup_ci_files.md +0 -36
  53. data/ext/puma_http11/PumaHttp11Service.java +0 -17
  54. data/ext/puma_http11/http11_parser.java.rl +0 -131
  55. data/ext/puma_http11/http11_parser.rl +0 -149
  56. data/ext/puma_http11/http11_parser_common.rl +0 -54
  57. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +0 -15
  58. data/ext/puma_http11/org/jruby/puma/EnvKey.java +0 -241
  59. data/ext/puma_http11/org/jruby/puma/Http11.java +0 -321
  60. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +0 -441
  61. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +0 -509
  62. data/tools/Dockerfile +0 -26
  63. data/tools/trickletest.rb +0 -44
  64. /data/docs/{nginx.md → deployment/nginx.md} +0 -0
  65. /data/docs/{jungle → deployment}/rc.d/README.md +0 -0
  66. /data/docs/{jungle → deployment}/rc.d/puma +0 -0
  67. /data/docs/{jungle → deployment}/rc.d/puma.conf +0 -0
@@ -25,6 +25,7 @@ module Puma
25
25
  @last_checkin = Time.now
26
26
  @last_status = {}
27
27
  @term = false
28
+ @mold = false
28
29
  @worker_max = Array.new WORKER_MAX_KEYS.length, 0
29
30
  end
30
31
 
@@ -54,6 +55,21 @@ module Puma
54
55
  @term
55
56
  end
56
57
 
58
+ def mold?
59
+ @mold
60
+ end
61
+
62
+ def mold!
63
+ @mold = true
64
+ Process.kill "URG", @pid
65
+ rescue Errno::ESRCH
66
+ end
67
+
68
+ def retire!
69
+ Process.kill "URG", @pid
70
+ rescue Errno::ESRCH
71
+ end
72
+
57
73
  def ping!(status)
58
74
  hsh = {}
59
75
  k, v = nil, nil
data/lib/puma/cluster.rb CHANGED
@@ -15,6 +15,9 @@ module Puma
15
15
  # via the `spawn_workers` method call. Each worker will have it's own
16
16
  # instance of a `Puma::Server`.
17
17
  class Cluster < Runner
18
+ MOLD_FORK_FORMAT = "L"
19
+ MOLD_FORK_BYTES = 4
20
+
18
21
  def initialize(launcher)
19
22
  super(launcher)
20
23
 
@@ -24,6 +27,11 @@ module Puma
24
27
 
25
28
  @worker_max = [] # keeps track of 'max' stat values
26
29
  @pending_phased_restart = false
30
+ @promoted_mold = nil
31
+ @mold = nil
32
+ @retired_molds = []
33
+ @mold_ready = false
34
+ @mold_threshold_index = 0
27
35
  end
28
36
 
29
37
  # Returns the list of cluster worker handles.
@@ -32,12 +40,12 @@ module Puma
32
40
 
33
41
  def stop_workers
34
42
  log "- Gracefully shutting down workers..."
35
- @workers.each { |x| x.term }
43
+ (@workers + molds).each { |x| x.term }
36
44
 
37
45
  begin
38
46
  loop do
39
47
  wait_workers
40
- break if @workers.reject {|w| w.pid.nil?}.empty?
48
+ break if @workers.reject {|w| w.pid.nil?}.empty? && molds.empty?
41
49
  sleep 0.2
42
50
  end
43
51
  rescue Interrupt
@@ -69,7 +77,7 @@ module Puma
69
77
 
70
78
  def spawn_workers
71
79
  diff = @options[:workers] - @workers.size
72
- return if diff < 1
80
+ return if diff < 1 || @promoted_mold
73
81
 
74
82
  master = Process.pid
75
83
  if @options[:fork_worker]
@@ -82,6 +90,9 @@ module Puma
82
90
  if @options[:fork_worker] && idx != 0
83
91
  @fork_writer << "#{idx}\n"
84
92
  pid = nil
93
+ elsif @mold
94
+ @fork_writer << [idx].pack(MOLD_FORK_FORMAT)
95
+ pid = nil
85
96
  else
86
97
  pid = spawn_worker(idx, master)
87
98
  end
@@ -168,6 +179,8 @@ module Puma
168
179
  end
169
180
 
170
181
  def all_workers_idle_timed_out?
182
+ return false if @pending_phased_restart || @workers.empty?
183
+
171
184
  (@workers.map(&:pid) - idle_timed_out_worker_pids).empty?
172
185
  end
173
186
 
@@ -206,6 +219,10 @@ module Puma
206
219
  t.map!(&:ping_timeout)
207
220
 
208
221
  @next_check = [t.min, @next_check].compact.min
222
+ if @options[:fork_worker] && @workers.any?(&:term?)
223
+ # Orphaned workers cannot wake the master through worker 0 as they exit.
224
+ @next_check = [@next_check, Time.now + 0.1].min
225
+ end
209
226
  end
210
227
 
211
228
  def worker(index, master)
@@ -216,7 +233,7 @@ module Puma
216
233
  @fork_writer.close
217
234
 
218
235
  pipes = { check_pipe: @check_pipe, worker_write: @worker_write }
219
- if @options[:fork_worker]
236
+ if refork?
220
237
  pipes[:fork_pipe] = @fork_pipe
221
238
  pipes[:wakeup] = @wakeup
222
239
  end
@@ -252,7 +269,6 @@ module Puma
252
269
  @status = :stop if @status == :run
253
270
  wakeup!
254
271
  @control&.stop true
255
- Process.waitall
256
272
  end
257
273
 
258
274
  def halt
@@ -299,14 +315,22 @@ module Puma
299
315
  @options[:preload_app]
300
316
  end
301
317
 
318
+ def refork?
319
+ @options[:fork_worker] || @options[:mold_worker]
320
+ end
321
+
302
322
  # @version 5.0.0
303
323
  def fork_worker!
304
324
  if (worker = worker_at 0)
305
- worker.phase += 1
325
+ worker.phase = @phase + 1
306
326
  end
307
327
  phased_restart(true)
308
328
  end
309
329
 
330
+ def mold_worker!
331
+ phased_restart(true)
332
+ end
333
+
310
334
  # We do this in a separate method to keep the lambda scope
311
335
  # of the signals handlers as small as possible.
312
336
  def setup_signals
@@ -325,6 +349,24 @@ module Puma
325
349
  end
326
350
  end
327
351
 
352
+ if @options[:mold_worker]
353
+ Signal.trap "SIGURG" do
354
+ mold_worker!
355
+ end
356
+
357
+ @events.register(:ping!) do |w|
358
+ threshold = @options[:mold_worker][@mold_threshold_index]
359
+ next unless threshold &&
360
+ !w.mold? &&
361
+ !@pending_phased_restart &&
362
+ w.last_status[:requests_count] >= threshold &&
363
+ all_workers_in_phase?
364
+
365
+ @mold_threshold_index += 1
366
+ mold_worker!
367
+ end
368
+ end
369
+
328
370
  Signal.trap "SIGCHLD" do
329
371
  wakeup!
330
372
  end
@@ -375,7 +417,7 @@ module Puma
375
417
 
376
418
  before = Thread.list.reject(&fork_safe)
377
419
 
378
- log "* Restarts: (\u2714) hot (\u2716) phased (#{@options[:fork_worker] ? "\u2714" : "\u2716"}) refork"
420
+ log "* Restarts: (\u2714) hot (\u2716) phased (#{refork? ? "\u2714" : "\u2716"}) refork"
379
421
  log "* Preloading application"
380
422
  load_and_bind
381
423
 
@@ -393,7 +435,7 @@ module Puma
393
435
  end
394
436
  end
395
437
  else
396
- log "* Restarts: (\u2714) hot (\u2714) phased (#{@options[:fork_worker] ? "\u2714" : "\u2716"}) refork"
438
+ log "* Restarts: (\u2714) hot (\u2714) phased (#{refork? ? "\u2714" : "\u2716"}) refork"
397
439
 
398
440
  unless @config.app_configured?
399
441
  error "No application configured, nothing to run"
@@ -437,6 +479,9 @@ module Puma
437
479
 
438
480
  @config.run_hooks(:before_fork, nil, @log_writer)
439
481
 
482
+ # Reparent workers to the master if their mold exits.
483
+ Puma.enable_child_subreaper if @options[:mold_worker]
484
+
440
485
  spawn_workers
441
486
 
442
487
  Signal.trap "SIGINT" do
@@ -446,7 +491,6 @@ module Puma
446
491
  begin
447
492
  booted = false
448
493
  in_phased_restart = false
449
- workers_not_booted = @options[:workers]
450
494
 
451
495
  while @status == :run
452
496
  begin
@@ -455,15 +499,11 @@ module Puma
455
499
  break
456
500
  end
457
501
 
458
- if @pending_phased_restart
502
+ if @pending_phased_restart && prepare_phased_restart
459
503
  start_phased_restart(@pending_phased_restart == :refork)
460
504
 
461
505
  in_phased_restart = @pending_phased_restart
462
506
  @pending_phased_restart = false
463
-
464
- workers_not_booted = @options[:workers]
465
- # worker 0 is not restarted on refork
466
- workers_not_booted -= 1 if in_phased_restart == :refork
467
507
  end
468
508
 
469
509
  check_workers(in_phased_restart == :refork)
@@ -479,20 +519,28 @@ module Puma
479
519
 
480
520
  result = read.gets
481
521
  pid = result.to_i
522
+ w = (@workers + molds).find { |x| x.pid == pid }
482
523
 
483
- if req == PIPE_BOOT || req == PIPE_FORK
524
+ if !w && (req == PIPE_BOOT || req == PIPE_FORK)
484
525
  pid, idx = result.split(':').map(&:to_i)
485
526
  w = worker_at idx
486
- w.pid = pid if w.pid.nil?
527
+ if w && w.pid.nil?
528
+ w.pid = pid
529
+ else
530
+ w = nil
531
+ begin
532
+ Process.kill "KILL", pid
533
+ rescue Errno::ESRCH
534
+ end
535
+ end
487
536
  end
488
537
 
489
- if w = @workers.find { |x| x.pid == pid }
538
+ if w
490
539
  case req
491
540
  when PIPE_BOOT
492
541
  w.boot!
493
542
  log "- Worker #{w.index} (PID: #{pid}) booted in #{w.uptime.round(2)}s, phase: #{w.phase}"
494
543
  @next_check = Time.now
495
- workers_not_booted -= 1
496
544
  when PIPE_EXTERNAL_TERM
497
545
  # external term, see worker method, Signal.trap "SIGTERM"
498
546
  w.term!
@@ -503,7 +551,7 @@ module Puma
503
551
  w.ping!(status)
504
552
  @events.fire(:ping!, w)
505
553
 
506
- if in_phased_restart && @options[:fork_worker] && workers_not_booted.positive? && w0 = worker_at(0)
554
+ if in_phased_restart && @options[:fork_worker] && w0 = worker_at(0)
507
555
  w0.ping!(status)
508
556
  @events.fire(:ping!, w0)
509
557
  end
@@ -519,13 +567,17 @@ module Puma
519
567
  else
520
568
  idle_workers[pid] = true
521
569
  end
570
+ when PIPE_MOLD
571
+ activate_mold(w)
572
+ @next_check = Time.now
522
573
  end
523
574
  else
524
575
  log "! Out-of-sync worker list, no #{pid} worker"
525
576
  end
526
577
  end
527
578
 
528
- if in_phased_restart && workers_not_booted.zero?
579
+ if in_phased_restart && @workers.size == @options[:workers] && all_workers_booted? && all_workers_in_phase?
580
+ @retired_molds.each(&:term)
529
581
  @events.fire_after_booted!
530
582
  debug_loaded_extensions("Loaded Extensions - master:") if @log_writer.debug?
531
583
  in_phased_restart = false
@@ -574,43 +626,119 @@ module Puma
574
626
  end
575
627
  end
576
628
 
629
+ fork_worker_exited = false
577
630
  @workers.reject! do |w|
578
- next false if w.pid.nil?
579
- begin
580
- # We may need to check the PID individually because:
581
- # 1. From Ruby versions 2.6 to 3.2, `Process.detach` can prevent or delay
582
- # `Process.wait2(-1)` from detecting a terminated process: https://bugs.ruby-lang.org/issues/19837.
583
- # 2. When `fork_worker` is enabled, some worker may not be direct children,
584
- # but grand children. Because of this they won't be reaped by `Process.wait2(-1)`.
585
- if (status = reaped_children.delete(w.pid) || Process.wait2(w.pid, Process::WNOHANG)&.last)
586
- w.process_status = status
587
- @config.run_hooks(:after_worker_shutdown, w, @log_writer)
588
- true
589
- else
590
- w.term if w.term?
591
- nil
592
- end
593
- rescue Errno::ECHILD
594
- begin
595
- Process.kill(0, w.pid)
596
- # child still alive but has another parent (e.g., using fork_worker)
597
- w.term if w.term?
598
- false
599
- rescue Errno::ESRCH, Errno::EPERM
600
- true # child is already terminated
601
- end
631
+ next w.term? if w.pid.nil?
632
+
633
+ status = reap(w, reaped_children)
634
+ fork_worker_exited = true if status && @options[:fork_worker] && w.index == 0
635
+ if status.is_a?(Process::Status)
636
+ w.process_status = status
637
+ @config.run_hooks(:after_worker_shutdown, w, @log_writer)
602
638
  end
639
+ status
603
640
  end
604
641
 
642
+ clear_pending_workers if fork_worker_exited
643
+ @promoted_mold = nil if @promoted_mold && reap(@promoted_mold, reaped_children)
644
+ if @mold && reap(@mold, reaped_children)
645
+ @mold = nil
646
+ clear_pending_workers
647
+ end
648
+ @retired_molds.reject! { |m| reap(m, reaped_children) }
649
+
605
650
  # Log unknown children
606
651
  reaped_children.each do |pid, status|
607
652
  log "! reaped unknown child process pid=#{pid} status=#{status}"
608
653
  end
609
654
  end
610
655
 
656
+ def clear_pending_workers
657
+ @workers.reject! { |w| w.pid.nil? }
658
+ while @fork_pipe.read_nonblock(4096, exception: false).is_a?(String)
659
+ end
660
+ end
661
+
662
+ # Returns the process status once the process has exited, true if it has
663
+ # exited but its status cannot be collected, or nil if it is still running.
664
+ def reap(handle, reaped_children)
665
+ # We may need to check the PID individually because:
666
+ # 1. From Ruby versions 2.6 to 3.2, `Process.detach` can prevent or delay
667
+ # `Process.wait2(-1)` from detecting a terminated process: https://bugs.ruby-lang.org/issues/19837.
668
+ # 2. When `fork_worker` or `mold_worker` is enabled, some workers may not be direct children,
669
+ # but grandchildren. Because of this they won't be reaped by `Process.wait2(-1)`.
670
+ status = reaped_children.delete(handle.pid) || Process.wait2(handle.pid, Process::WNOHANG)&.last
671
+ handle.term if !status && handle.term?
672
+ status
673
+ rescue Errno::ECHILD
674
+ begin
675
+ Process.kill(0, handle.pid)
676
+ # child still alive but has another parent (e.g., using fork_worker)
677
+ handle.term if handle.term?
678
+ nil
679
+ rescue Errno::ESRCH, Errno::EPERM
680
+ true # child is already terminated
681
+ end
682
+ end
683
+
684
+ def molds
685
+ [@promoted_mold, @mold, *@retired_molds].compact
686
+ end
687
+
688
+ # Returns true once workers can be restarted. With `mold_worker`, a refork
689
+ # first promotes a new mold and waits for it, while a phased restart
690
+ # retires all molds so workers are forked from the master.
691
+ def prepare_phased_restart
692
+ return true unless @options[:mold_worker]
693
+
694
+ if @pending_phased_restart != :refork
695
+ retire_molds
696
+ @mold_ready = false
697
+ @mold_threshold_index = 0
698
+ true
699
+ elsif @mold_ready
700
+ @mold_ready = false
701
+ true
702
+ else
703
+ promote_mold unless @promoted_mold
704
+ false
705
+ end
706
+ end
707
+
708
+ def promote_mold
709
+ candidate = @workers.select { |w| w.booted? && !w.term? }
710
+ .max_by { |w| w.last_status[:requests_count].to_i }
711
+ return unless candidate
712
+
713
+ log "- Promoting worker #{candidate.index} (PID: #{candidate.pid}) to mold"
714
+ @workers.delete candidate
715
+ @promoted_mold = candidate
716
+ candidate.mold!
717
+ end
718
+
719
+ def activate_mold(mold)
720
+ return unless mold == @promoted_mold
721
+
722
+ log "- Mold (PID: #{mold.pid}) ready"
723
+ @promoted_mold = nil
724
+ retire_molds
725
+ @mold = mold
726
+ @mold_ready = true
727
+ end
728
+
729
+ # A promoted mold has forked no workers, so it is stopped now. An active
730
+ # mold stops forking, and is stopped once the restart completes.
731
+ def retire_molds
732
+ @promoted_mold&.term
733
+ @mold&.retire!
734
+ @retired_molds.concat [@promoted_mold, @mold].compact
735
+ @promoted_mold = nil
736
+ @mold = nil
737
+ end
738
+
611
739
  # @version 5.0.0
612
740
  def timeout_workers
613
- @workers.each do |w|
741
+ (@workers + molds).each do |w|
614
742
  if !w.term? && w.ping_timeout <= Time.now
615
743
  details = if w.booted?
616
744
  "(Worker #{w.index} failed to check in within #{@options[:worker_timeout]} seconds)"
@@ -73,7 +73,7 @@ module Puma
73
73
  env[SERVER_PROTOCOL],
74
74
  now - began_at ]
75
75
 
76
- write(msg)
76
+ write(env, msg)
77
77
  end
78
78
 
79
79
  def log(env, status, header, began_at)
@@ -92,10 +92,10 @@ module Puma
92
92
  length,
93
93
  now - began_at ]
94
94
 
95
- write(msg)
95
+ write(env, msg)
96
96
  end
97
97
 
98
- def write(msg)
98
+ def write(env, msg)
99
99
  logger = @logger || env['rack.errors']
100
100
 
101
101
  # Standard library logger doesn't support write but it supports << which actually
@@ -133,6 +133,7 @@ module Puma
133
133
  class NotClampedError < StandardError; end
134
134
 
135
135
  DEFAULTS = {
136
+ allow_underscore_headers: true,
136
137
  auto_trim_time: 30,
137
138
  binds: ['tcp://[::]:9292'.freeze],
138
139
  debug: false,
@@ -144,6 +145,7 @@ module Puma
144
145
  first_data_timeout: 30,
145
146
  force_shutdown_after: -1,
146
147
  http_content_length_limit: nil,
148
+ shutdown_body_timeout: 25,
147
149
  # Number of seconds to wait until the next request before shutting down.
148
150
  idle_timeout: nil,
149
151
  io_selector_backend: :auto,
@@ -283,11 +285,17 @@ module Puma
283
285
  #
284
286
  # This also calls load if it hasn't been called yet.
285
287
  def clamp
288
+ if @clamped
289
+ warn "Configuration already clamped, calling clamp multiple times is deprecated and will raise in Puma v9"
290
+ return options
291
+ end
292
+
286
293
  load unless @loaded
287
294
  run_mode_hooks
288
295
  set_conditional_default_options
289
296
  @_options.finalize_values
290
297
  rewrite_unavailable_ipv6_binds!
298
+ validate_options
291
299
  @clamped = true
292
300
  warn_hooks
293
301
  options
@@ -356,8 +364,7 @@ module Puma
356
364
  block.call arg
357
365
  end
358
366
  rescue => e
359
- log_writer.log "WARNING hook #{key} failed with exception (#{e.class}) #{e.message}"
360
- log_writer.debug e.backtrace.join("\n")
367
+ handle_hook_error e, key, log_writer
361
368
  end
362
369
  end
363
370
  end
@@ -398,6 +405,15 @@ module Puma
398
405
 
399
406
  private
400
407
 
408
+ def handle_hook_error(e, key, log_writer)
409
+ if (handler = options[:hook_error_handler])
410
+ handler.call(e, key)
411
+ else
412
+ log_writer.log "WARNING hook #{key} failed with exception (#{e.class}) #{e.message}"
413
+ log_writer.debug e.backtrace.join("\n")
414
+ end
415
+ end
416
+
401
417
  def rewrite_unavailable_ipv6_binds!
402
418
  return if self.class.ipv6_interface_available?
403
419
 
@@ -491,7 +507,7 @@ module Puma
491
507
  @_options.all_of(key).each(&:call)
492
508
 
493
509
  unless @_options[:workers] == workers_before
494
- raise "cannot change the number of workers inside a #{key} configuration hook"
510
+ raise "Cannot change the number of workers inside a #{key} configuration hook"
495
511
  end
496
512
  end
497
513
 
@@ -500,6 +516,10 @@ module Puma
500
516
  (@_options[:workers] > 1) && Puma.forkable?
501
517
  end
502
518
 
519
+ def validate_options
520
+ raise "fork_worker and mold_worker cannot be used together" if @_options[:fork_worker] && @_options[:mold_worker]
521
+ end
522
+
503
523
  def warn_hooks
504
524
  return if options[:workers] > 0
505
525
  return if options[:silence_fork_callback_warning]
data/lib/puma/const.rb CHANGED
@@ -100,8 +100,8 @@ module Puma
100
100
  # too taxing on performance.
101
101
  module Const
102
102
 
103
- PUMA_VERSION = VERSION = "8.0.2"
104
- CODE_NAME = "Into the Arena"
103
+ PUMA_VERSION = VERSION = "8.1.2"
104
+ CODE_NAME = "Puma Psychosis"
105
105
 
106
106
  PUMA_SERVER_STRING = ["puma", PUMA_VERSION, CODE_NAME].join(" ").freeze
107
107
 
@@ -109,6 +109,9 @@ module Puma
109
109
  # sending data back
110
110
  WRITE_TIMEOUT = 10
111
111
 
112
+ # How long the accept loop waits after running out of file descriptors
113
+ ACCEPT_ERROR_DELAY = 0.1
114
+
112
115
  # The original URI requested by the client.
113
116
  REQUEST_URI= "REQUEST_URI"
114
117
  REQUEST_PATH = "REQUEST_PATH"
@@ -147,12 +150,12 @@ module Puma
147
150
  REQUEST_METHOD = "REQUEST_METHOD"
148
151
  HEAD = "HEAD"
149
152
 
150
- # based on https://www.rfc-editor.org/rfc/rfc9110.html#name-overview,
151
- # with CONNECT removed, and PATCH added
152
- SUPPORTED_HTTP_METHODS = %w[HEAD GET POST PUT DELETE OPTIONS TRACE PATCH].freeze
153
+ # based on https://www.rfc-editor.org/rfc/rfc9110.html#name-overview and https://www.rfc-editor.org/rfc/rfc10008.html,
154
+ # with CONNECT removed, and PATCH & QUERY added
155
+ SUPPORTED_HTTP_METHODS = %w[HEAD GET POST PUT DELETE OPTIONS TRACE PATCH QUERY].freeze
153
156
 
154
157
  # list from https://www.iana.org/assignments/http-methods/http-methods.xhtml
155
- # as of 04-May-23
158
+ # as of 17-Jun-26, with QUERY added based on https://www.rfc-editor.org/rfc/rfc10008.html
156
159
  IANA_HTTP_METHODS = %w[
157
160
  ACL
158
161
  BASELINE-CONTROL
@@ -182,6 +185,7 @@ module Puma
182
185
  PROPFIND
183
186
  PROPPATCH
184
187
  PUT
188
+ QUERY
185
189
  REBIND
186
190
  REPORT
187
191
  SEARCH
@@ -232,6 +236,7 @@ module Puma
232
236
  PUMA_SOCKET = "puma.socket"
233
237
  PUMA_CONFIG = "puma.config"
234
238
  PUMA_PEERCERT = "puma.peercert"
239
+ PUMA_UNDERSCORE_HEADERS = "puma.underscore_headers"
235
240
 
236
241
  HTTP = "http"
237
242
  HTTPS = "https"
@@ -280,9 +285,8 @@ module Puma
280
285
  # header values can contain HTAB?
281
286
  ILLEGAL_HEADER_VALUE_REGEX = /[\x00-\x08\x0A-\x1F]/.freeze
282
287
 
283
- # The keys of headers that should not be convert to underscore
284
- # normalized versions. These headers are ignored at the request reading layer,
285
- # but if we normalize them after reading, it's just confusing for the application.
288
+ # Framing aliases must never be normalized into standard header keys.
289
+ # Client#setup_body rejects requests containing these keys before body reads.
286
290
  UNMASKABLE_HEADERS = {
287
291
  "HTTP_TRANSFER,ENCODING" => true,
288
292
  "HTTP_CONTENT,LENGTH" => true,
@@ -303,6 +307,7 @@ module Puma
303
307
  PIPE_TERM = "t"
304
308
  PIPE_PING = "p"
305
309
  PIPE_IDLE = "i"
310
+ PIPE_MOLD = "m"
306
311
  end
307
312
  end
308
313
  end
@@ -44,6 +44,7 @@ module Puma
44
44
  @pid = nil
45
45
  @control_url = nil
46
46
  @control_auth_token = nil
47
+ @control_ca_file = nil
47
48
  @config_file = nil
48
49
  @command = nil
49
50
  @environment = env['APP_ENV'] || env['RACK_ENV'] || env['RAILS_ENV']
@@ -80,6 +81,10 @@ module Puma
80
81
  @control_auth_token = arg
81
82
  end
82
83
 
84
+ o.on "--control-ca-file PATH", "CA certificate bundle for the TLS control server" do |arg|
85
+ @control_ca_file = arg
86
+ end
87
+
83
88
  o.on "-F", "--config-file PATH", "Puma config script" do |arg|
84
89
  @config_file = arg
85
90
  end
@@ -179,11 +184,16 @@ module Puma
179
184
  when 'ssl'
180
185
  require 'openssl'
181
186
  host = host[1..-2] if host&.start_with? '['
182
- OpenSSL::SSL::SSLSocket.new(
187
+ ctx = OpenSSL::SSL::SSLContext.new
188
+ ctx.ca_file = @control_ca_file if @control_ca_file
189
+ ctx.set_params
190
+ ssl = OpenSSL::SSL::SSLSocket.new(
183
191
  TCPSocket.new(host, uri.port),
184
- OpenSSL::SSL::SSLContext.new)
185
- .tap { |ssl| ssl.sync_close = true } # default is false
192
+ ctx)
193
+ ssl.tap { |socket| socket.sync_close = true }
194
+ .tap { |socket| socket.hostname = host }
186
195
  .tap(&:connect)
196
+ .tap { |socket| socket.post_connection_check host }
187
197
  when 'tcp'
188
198
  host = host[1..-2] if host&.start_with? '['
189
199
  TCPSocket.new host, uri.port
@@ -230,6 +240,7 @@ module Puma
230
240
  message response.last if PRINTABLE_COMMANDS.include?(@command)
231
241
  end
232
242
  ensure
243
+ server ||= ssl
233
244
  if server
234
245
  if uri.scheme == 'ssl'
235
246
  server.sysclose