puma 8.0.2-java → 8.1.0-java

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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +136 -0
  3. data/README.md +20 -5
  4. data/docs/6.0-Upgrade.md +1 -1
  5. data/docs/8.0-Upgrade.md +2 -2
  6. data/docs/deployment.md +3 -3
  7. data/docs/fork_worker.md +34 -4
  8. data/docs/kubernetes.md +3 -3
  9. data/docs/plugins.md +2 -2
  10. data/docs/puma_support.md +78 -0
  11. data/docs/rails_dev_mode.md +2 -2
  12. data/docs/restart.md +4 -3
  13. data/docs/signals.md +2 -2
  14. data/docs/stats.md +5 -5
  15. data/docs/systemd.md +4 -2
  16. data/docs/testing_benchmarks_local_files.md +1 -1
  17. data/docs/testing_test_rackup_ci_files.md +2 -2
  18. data/ext/puma_http11/extconf.rb +2 -0
  19. data/ext/puma_http11/http11_parser.c +6 -2
  20. data/ext/puma_http11/http11_parser_common.rl +4 -1
  21. data/ext/puma_http11/mini_ssl.c +16 -5
  22. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +36 -36
  23. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +44 -34
  24. data/ext/puma_http11/puma_http11.c +28 -3
  25. data/lib/puma/binder.rb +29 -0
  26. data/lib/puma/cli.rb +6 -0
  27. data/lib/puma/client.rb +178 -44
  28. data/lib/puma/client_env.rb +32 -18
  29. data/lib/puma/cluster/worker.rb +109 -33
  30. data/lib/puma/cluster/worker_handle.rb +16 -0
  31. data/lib/puma/cluster.rb +173 -44
  32. data/lib/puma/commonlogger.rb +3 -3
  33. data/lib/puma/configuration.rb +23 -3
  34. data/lib/puma/const.rb +14 -9
  35. data/lib/puma/control_cli.rb +14 -3
  36. data/lib/puma/dsl.rb +279 -92
  37. data/lib/puma/launcher.rb +6 -4
  38. data/lib/puma/minissl.rb +101 -56
  39. data/lib/puma/plugin/systemd.rb +1 -1
  40. data/lib/puma/puma_http11.jar +0 -0
  41. data/lib/puma/reactor.rb +35 -3
  42. data/lib/puma/response.rb +28 -6
  43. data/lib/puma/runner.rb +5 -1
  44. data/lib/puma/sd_notify.rb +4 -1
  45. data/lib/puma/server.rb +131 -35
  46. data/lib/puma/thread_pool.rb +12 -1
  47. data/lib/puma/util.rb +8 -4
  48. data/lib/puma.rb +1 -1
  49. metadata +3 -2
data/lib/puma/server.rb CHANGED
@@ -51,7 +51,7 @@ module Puma
51
51
  # @todo the following may be deprecated in the future
52
52
  attr_reader :auto_trim_time, :early_hints, :first_data_timeout,
53
53
  :leak_stack_on_error,
54
- :persistent_timeout, :reaping_time
54
+ :persistent_timeout, :reaping_time, :shutdown_body_timeout
55
55
 
56
56
  attr_accessor :app
57
57
  attr_accessor :binder
@@ -98,6 +98,8 @@ module Puma
98
98
  @early_hints = @options[:early_hints]
99
99
  @first_data_timeout = @options[:first_data_timeout]
100
100
  @persistent_timeout = @options[:persistent_timeout]
101
+ @shutdown_body_timeout = @options[:shutdown_body_timeout]
102
+ @shutdown_body_deadline = nil
101
103
  @idle_timeout = @options[:idle_timeout]
102
104
  @min_threads = @options[:min_threads]
103
105
  @max_threads = @options[:max_threads]
@@ -107,6 +109,8 @@ module Puma
107
109
  @enable_keep_alives &&= @queue_requests
108
110
  @io_selector_backend = @options[:io_selector_backend]
109
111
  @http_content_length_limit = @options[:http_content_length_limit]
112
+ @continue_callback = @options[:continue_callback]
113
+ @allow_underscore_headers = @options.fetch(:allow_underscore_headers, true)
110
114
  @cluster_accept_loop_delay = ClusterAcceptLoopDelay.new(
111
115
  workers: @options[:workers],
112
116
  max_delay: @options[:wait_for_less_busy_worker] || 0 # Real default is in Configuration::DEFAULTS, this is for unit testing
@@ -258,6 +262,7 @@ module Puma
258
262
  @events.fire :state, :booting
259
263
 
260
264
  @status = :run
265
+ @shutdown_body_deadline = nil
261
266
 
262
267
  @thread_pool = ThreadPool.new(thread_name, options, server: self) do |processor, client|
263
268
  process_client(processor, client)
@@ -312,6 +317,9 @@ module Puma
312
317
  # If a client object times out, a 408 response is written, its connection is closed,
313
318
  # and the object is removed from the reactor (return `true`).
314
319
  #
320
+ # If the request cannot be read, the Client is passed to the ThreadPool with its
321
+ # `reactor_error` set, so that a processor thread writes the error response.
322
+ #
315
323
  # If the Reactor is shutting down, all Clients are either timed out or passed to the
316
324
  # ThreadPool, depending on their current state (#can_close?).
317
325
  #
@@ -320,18 +328,33 @@ module Puma
320
328
  # will wake up and again be checked to see if it's ready to be passed to the thread pool.
321
329
  def reactor_wakeup(client)
322
330
  shutdown = !@queue_requests
323
- if client.try_to_finish || (shutdown && !client.can_close?)
324
- @thread_pool << client
331
+ read = client.bytes_read
332
+ if client.try_to_finish { shutdown_body_limit } || (shutdown && !client.can_close?)
333
+ hand_to_thread_pool client
325
334
  elsif shutdown || client.timeout == 0
326
335
  client.timeout!
327
336
  else
328
- client.set_timeout(@first_data_timeout)
337
+ client.set_timeout(@first_data_timeout) if client.bytes_read > read
329
338
  false
330
339
  end
331
- rescue StandardError => e
332
- client_error(e, client)
340
+ rescue ConnectionError, EOFError
333
341
  close_client_safely(client)
334
342
  true
343
+ rescue StandardError => e
344
+ # Writing the error response may block on a client that is not reading,
345
+ # so leave it to a thread pool thread.
346
+ client.reactor_error = e
347
+ hand_to_thread_pool client
348
+ end
349
+
350
+ def hand_to_thread_pool(client)
351
+ @thread_pool << client
352
+ true
353
+ rescue ThreadError
354
+ true
355
+ rescue StandardError
356
+ close_client_safely client
357
+ true
335
358
  end
336
359
 
337
360
  def handle_servers
@@ -361,13 +384,16 @@ module Puma
361
384
  ios = IO.select sockets, nil, nil, (shutting_down? ? 0 : @idle_timeout)
362
385
  unless ios
363
386
  unless shutting_down?
364
- @idle_timeout_reached = true
365
-
366
387
  if @clustered
367
- @worker_write << "#{PipeRequest::PIPE_IDLE}#{Process.pid}\n" rescue nil
388
+ unless @idle_timeout_reached
389
+ @idle_timeout_reached = true
390
+ @worker_write << "#{PipeRequest::PIPE_IDLE}#{Process.pid}\n" rescue nil
391
+ end
368
392
  next
369
393
  else
394
+ @idle_timeout_reached = true
370
395
  @log_writer.log "- Idle timeout reached"
396
+ shutdown_body_deadline
371
397
  @status = :stop
372
398
  end
373
399
  end
@@ -388,19 +414,30 @@ module Puma
388
414
  # clients until the code is finished.
389
415
  pool.wait_while_out_of_band_running
390
416
 
391
- # A well rested herd (cluster) runs faster
392
- if @cluster_accept_loop_delay.on? && (busy_threads_plus_todo = pool.busy_threads) > 0
393
- delay = @cluster_accept_loop_delay.calculate(
394
- max_threads: @max_threads,
395
- busy_threads_plus_todo: busy_threads_plus_todo
396
- )
397
- sleep(delay)
417
+ unless shutting_down?
418
+ if @queue_requests
419
+ if @cluster_accept_loop_delay.on? && (busy_threads_plus_todo = pool.busy_threads) > 0
420
+ delay = @cluster_accept_loop_delay.calculate(
421
+ max_threads: @max_threads,
422
+ busy_threads_plus_todo: busy_threads_plus_todo
423
+ )
424
+ sleep(delay)
425
+ end
426
+ else
427
+ pool.wait_until_not_full
428
+
429
+ # A stop may have arrived while waiting for a thread
430
+ break if check.wait_readable(0) && handle_check
431
+ end
398
432
  end
399
433
 
400
434
  io = begin
401
435
  sock.accept_nonblock
402
436
  rescue IO::WaitReadable
403
437
  next
438
+ rescue Errno::EMFILE, Errno::ENFILE, Errno::ENOBUFS, Errno::ENOMEM => e
439
+ accept_resource_error e
440
+ next
404
441
  end
405
442
  drain += 1 if shutting_down?
406
443
 
@@ -413,11 +450,17 @@ module Puma
413
450
  # In the case that any of the sockets are unexpectedly close.
414
451
  raise
415
452
  rescue StandardError => e
416
- @log_writer.unknown_error e, nil, "Listen loop"
453
+ begin
454
+ @log_writer.unknown_error e, nil, "Listen loop"
455
+ rescue StandardError
456
+ end
417
457
  end
418
458
  end
419
459
 
420
- @log_writer.debug { "Drained #{drain} additional connections." } if drain
460
+ begin
461
+ @log_writer.debug { "Drained #{drain} additional connections." } if drain
462
+ rescue StandardError
463
+ end
421
464
  @events.fire :state, @status
422
465
 
423
466
  if queue_requests
@@ -427,7 +470,10 @@ module Puma
427
470
 
428
471
  graceful_shutdown if @status == :stop || @status == :restart
429
472
  rescue Exception => e
430
- @log_writer.unknown_error e, nil, "Exception handling servers"
473
+ begin
474
+ @log_writer.unknown_error e, nil, "Exception handling servers"
475
+ rescue StandardError
476
+ end
431
477
  ensure
432
478
  # Errno::EBADF is infrequently raised
433
479
  [@check, @notify].each do |io|
@@ -450,21 +496,25 @@ module Puma
450
496
  client.env_set_http_version = @env_set_http_version
451
497
  client.http_content_length_limit = @http_content_length_limit
452
498
  client.supported_http_methods = @supported_http_methods
499
+ client.allow_underscore_headers = @allow_underscore_headers
500
+ client.continue_callback = @continue_callback
453
501
  client
454
502
  end
455
503
 
456
504
  # :nodoc:
457
505
  def handle_check
458
- cmd = @check.read(1)
506
+ cmd = @check.sysread 1
459
507
 
460
508
  case cmd
461
509
  when STOP_COMMAND
510
+ shutdown_body_deadline
462
511
  @status = :stop
463
512
  return true
464
513
  when HALT_COMMAND
465
514
  @status = :halt
466
515
  return true
467
516
  when RESTART_COMMAND
517
+ shutdown_body_deadline
468
518
  @status = :restart
469
519
  return true
470
520
  end
@@ -488,7 +538,9 @@ module Puma
488
538
  requests = 0
489
539
 
490
540
  begin
491
- if @queue_requests && !client.eagerly_finish
541
+ raise client.reactor_error if client.reactor_error
542
+
543
+ if @queue_requests && !client.eagerly_finish { shutdown_body_limit }
492
544
 
493
545
  client.set_timeout(@first_data_timeout)
494
546
  if @reactor.add client
@@ -498,7 +550,7 @@ module Puma
498
550
  end
499
551
 
500
552
  with_force_shutdown(client) do
501
- client.finish(@first_data_timeout)
553
+ client.finish(@first_data_timeout) { shutdown_body_limit }
502
554
  end
503
555
 
504
556
  can_loop = true
@@ -519,7 +571,7 @@ module Puma
519
571
  next_request_ready = if client.has_back_to_back_requests?
520
572
  with_force_shutdown(client) { client.process_back_to_back_requests }
521
573
  else
522
- with_force_shutdown(client) { client.eagerly_finish }
574
+ with_force_shutdown(client) { client.eagerly_finish { shutdown_body_limit } }
523
575
  end
524
576
 
525
577
  if next_request_ready
@@ -528,8 +580,12 @@ module Puma
528
580
  if @thread_pool.waiting > 0
529
581
  can_loop = true
530
582
  else
531
- @thread_pool << client
532
- close_socket = false
583
+ @thread_pool.with_mutex do
584
+ unless shutting_down?
585
+ @thread_pool << client
586
+ close_socket = false
587
+ end
588
+ end
533
589
  end
534
590
  elsif @queue_requests
535
591
  client.set_timeout @persistent_timeout
@@ -551,6 +607,25 @@ module Puma
551
607
  end
552
608
  end
553
609
 
610
+ # Accepting fails while the process is out of file descriptors or memory.
611
+ # The connection stays in the backlog, so retrying at once would spin and
612
+ # log at full speed. Wait briefly and log at most once per second.
613
+ def accept_resource_error(error)
614
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
615
+ if @accept_error_logged_at.nil? || now - @accept_error_logged_at >= 1
616
+ suffix = @accept_errors_suppressed.to_i > 0 ? " (#{@accept_errors_suppressed} more suppressed)" : ""
617
+ @accept_error_logged_at = now
618
+ @accept_errors_suppressed = 0
619
+ begin
620
+ @log_writer.unknown_error error, nil, "Listen loop#{suffix}"
621
+ rescue StandardError
622
+ end
623
+ else
624
+ @accept_errors_suppressed += 1
625
+ end
626
+ sleep ACCEPT_ERROR_DELAY
627
+ end
628
+
554
629
  # :nodoc:
555
630
  def close_client_safely(client)
556
631
  client.close
@@ -587,6 +662,13 @@ module Puma
587
662
  when HttpParserError501
588
663
  response_to_error(client, requests, e, 501)
589
664
  @log_writer.parse_error e, client
665
+ when ContinueRejected
666
+ status, headers, res_body = e.response
667
+ begin
668
+ prepare_response(status, headers, res_body, requests, client)
669
+ ensure
670
+ res_body.close if res_body.respond_to? :close
671
+ end
590
672
  else
591
673
  response_to_error(client, requests, e, 500)
592
674
  @log_writer.unknown_error e, nil, "Read"
@@ -628,9 +710,19 @@ module Puma
628
710
  end
629
711
  private :response_to_error
630
712
 
713
+ def shutdown_body_deadline
714
+ @shutdown_body_deadline ||= Process.clock_gettime(Process::CLOCK_MONOTONIC) + @shutdown_body_timeout
715
+ end
716
+
717
+ def shutdown_body_limit
718
+ @shutdown_body_deadline
719
+ end
720
+
631
721
  # Wait for all outstanding requests to finish.
632
722
  #
633
723
  def graceful_shutdown
724
+ shutdown_body_deadline
725
+
634
726
  if @status != :restart
635
727
  @binder.close
636
728
  end
@@ -639,16 +731,18 @@ module Puma
639
731
  end
640
732
 
641
733
  def notify_safely(message)
642
- @notify << message
643
- rescue IOError, NoMethodError, Errno::EPIPE, Errno::EBADF
644
- # The server, in another thread, is shutting down
645
- rescue RuntimeError => e
646
- # Temporary workaround for https://bugs.ruby-lang.org/issues/13239
647
- if e.message.include?('IOError')
648
- # ignore
649
- else
650
- raise e
651
- end
734
+ # Use write_nonblock rather than `<<` or `syswrite`. This method is
735
+ # called from signal handlers (TERM/USR2/INT/HUP, and the cluster
736
+ # worker's SIGTERM trap). On Ruby < 4.0, a blocking write from trap
737
+ # context that is still in flight when handle_servers' ensure block
738
+ # runs IO#close leaves the closing thread parked in IO#close forever
739
+ # (puma#3677). write_nonblock is a single write(2) that never waits,
740
+ # so IO#close has nothing to wait for. If the pipe is full a command
741
+ # is already pending, so dropping this one is harmless.
742
+ notify = @notify
743
+ notify.write_nonblock(message, exception: false) if notify && !notify.closed?
744
+ rescue IOError, NoMethodError, Errno::EPIPE, Errno::EBADF => e
745
+ @log_writer.unknown_error(e, nil, "notify_safely error") if Puma::IS_MRI && RUBY_VERSION > '3.3'
652
746
  end
653
747
  private :notify_safely
654
748
 
@@ -656,6 +750,7 @@ module Puma
656
750
  # off the request queue before finally exiting.
657
751
 
658
752
  def stop(sync=false)
753
+ shutdown_body_deadline
659
754
  notify_safely(STOP_COMMAND)
660
755
  @thread.join if @thread && sync
661
756
  end
@@ -666,6 +761,7 @@ module Puma
666
761
  end
667
762
 
668
763
  def begin_restart(sync=false)
764
+ shutdown_body_deadline
669
765
  notify_safely(RESTART_COMMAND)
670
766
  @thread.join if @thread && sync
671
767
  end
@@ -7,7 +7,6 @@ require_relative 'server_plugin_control'
7
7
 
8
8
  module Puma
9
9
 
10
- # Add `Thread#puma_server` and `Thread#puma_server=`
11
10
  Thread.attr_accessor(:puma_server)
12
11
 
13
12
  # Internal Docs for A simple thread pool management object.
@@ -211,6 +210,7 @@ module Puma
211
210
  processor.marked_as_io_thread = false
212
211
  else
213
212
  # We're already at max threads, so we exit the extra io thread.
213
+ @spawned -= 1
214
214
  @processors.delete(processor)
215
215
  trigger_before_thread_exit_hooks
216
216
  Thread.exit
@@ -314,6 +314,17 @@ module Puma
314
314
  end
315
315
  end
316
316
 
317
+ def wait_until_not_full
318
+ with_mutex do
319
+ while true
320
+ return if @shutdown
321
+ return if (@spawned - @waiting + @todo.size) < @max
322
+
323
+ @not_full.wait @mutex
324
+ end
325
+ end
326
+ end
327
+
317
328
  # @version 5.0.0
318
329
  def with_mutex(&block)
319
330
  @mutex.owned? ?
data/lib/puma/util.rb CHANGED
@@ -13,21 +13,25 @@ module Puma
13
13
  # Escapes and unescapes a URI escaped string with
14
14
  # +encoding+. +encoding+ will be the target encoding of the string
15
15
  # returned, and it defaults to UTF-8
16
+ #
17
+ # Unlike form encoding, escape writes a space as '%20' and unescape
18
+ # keeps a literal '+', so values such as OpenSSL cipher strings
19
+ # ("AES+SHA") survive.
16
20
  if defined?(::Encoding)
17
21
  def escape(s, encoding = Encoding::UTF_8)
18
- URI.encode_www_form_component(s, encoding)
22
+ URI.encode_www_form_component(s, encoding).gsub('+', '%20')
19
23
  end
20
24
 
21
25
  def unescape(s, encoding = Encoding::UTF_8)
22
- URI.decode_www_form_component(s, encoding)
26
+ URI.decode_www_form_component(s.gsub('+', '%2B'), encoding)
23
27
  end
24
28
  else
25
29
  def escape(s, encoding = nil)
26
- URI.encode_www_form_component(s, encoding)
30
+ URI.encode_www_form_component(s, encoding).gsub('+', '%20')
27
31
  end
28
32
 
29
33
  def unescape(s, encoding = nil)
30
- URI.decode_www_form_component(s, encoding)
34
+ URI.decode_www_form_component(s.gsub('+', '%2B'), encoding)
31
35
  end
32
36
  end
33
37
  module_function :unescape, :escape
data/lib/puma.rb CHANGED
@@ -82,7 +82,7 @@ module Puma
82
82
  #
83
83
  def self.deprecate_method_change(method_old, method_caller, method_new)
84
84
  if method_old == method_caller
85
- warn "Use '#{method_new}', '#{method_caller}' is deprecated and will be removed in v8"
85
+ warn "Use '#{method_new}', '#{method_caller}' is deprecated and will be removed in Puma v9"
86
86
  end
87
87
  end
88
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puma
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.2
4
+ version: 8.1.0
5
5
  platform: java
6
6
  authors:
7
7
  - Evan Phoenix
@@ -65,6 +65,7 @@ files:
65
65
  - docs/kubernetes.md
66
66
  - docs/nginx.md
67
67
  - docs/plugins.md
68
+ - docs/puma_support.md
68
69
  - docs/rails_dev_mode.md
69
70
  - docs/restart.md
70
71
  - docs/signals.md
@@ -157,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
158
  - !ruby/object:Gem::Version
158
159
  version: '0'
159
160
  requirements: []
160
- rubygems_version: 4.0.3
161
+ rubygems_version: 4.0.21
161
162
  specification_version: 4
162
163
  summary: A Ruby/Rack web server built for parallelism.
163
164
  test_files: []