puma 4.3.12 → 5.0.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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +79 -32
  3. data/LICENSE +23 -20
  4. data/README.md +26 -13
  5. data/bin/puma-wild +0 -0
  6. data/docs/architecture.md +3 -3
  7. data/docs/deployment.md +9 -3
  8. data/docs/fork_worker.md +31 -0
  9. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  10. data/docs/images/puma-connection-flow.png +0 -0
  11. data/docs/images/puma-general-arch.png +0 -0
  12. data/docs/jungle/README.md +13 -0
  13. data/{tools → docs}/jungle/rc.d/README.md +0 -0
  14. data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
  15. data/{tools → docs}/jungle/upstart/README.md +0 -0
  16. data/{tools → docs}/jungle/upstart/puma-manager.conf +0 -0
  17. data/{tools → docs}/jungle/upstart/puma.conf +0 -0
  18. data/docs/nginx.md +0 -0
  19. data/docs/plugins.md +0 -0
  20. data/docs/restart.md +0 -0
  21. data/docs/signals.md +7 -6
  22. data/docs/systemd.md +1 -63
  23. data/ext/puma_http11/PumaHttp11Service.java +2 -4
  24. data/ext/puma_http11/ext_help.h +0 -0
  25. data/ext/puma_http11/extconf.rb +3 -10
  26. data/ext/puma_http11/http11_parser.c +10 -23
  27. data/ext/puma_http11/http11_parser.h +0 -0
  28. data/ext/puma_http11/http11_parser.java.rl +0 -0
  29. data/ext/puma_http11/http11_parser.rl +0 -0
  30. data/ext/puma_http11/http11_parser_common.rl +1 -1
  31. data/ext/puma_http11/mini_ssl.c +62 -84
  32. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  33. data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
  34. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +46 -48
  35. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +77 -18
  36. data/ext/puma_http11/puma_http11.c +6 -38
  37. data/lib/puma/accept_nonblock.rb +0 -0
  38. data/lib/puma/app/status.rb +14 -1
  39. data/lib/puma/binder.rb +88 -68
  40. data/lib/puma/cli.rb +7 -15
  41. data/lib/puma/client.rb +49 -59
  42. data/lib/puma/cluster.rb +191 -74
  43. data/lib/puma/commonlogger.rb +2 -2
  44. data/lib/puma/configuration.rb +31 -42
  45. data/lib/puma/const.rb +6 -8
  46. data/lib/puma/control_cli.rb +29 -17
  47. data/lib/puma/detect.rb +17 -0
  48. data/lib/puma/dsl.rb +144 -70
  49. data/lib/puma/error_logger.rb +97 -0
  50. data/lib/puma/events.rb +35 -31
  51. data/lib/puma/io_buffer.rb +9 -2
  52. data/lib/puma/jruby_restart.rb +0 -58
  53. data/lib/puma/launcher.rb +49 -31
  54. data/lib/puma/minissl/context_builder.rb +0 -3
  55. data/lib/puma/minissl.rb +60 -18
  56. data/lib/puma/null_io.rb +1 -1
  57. data/lib/puma/plugin/tmp_restart.rb +0 -0
  58. data/lib/puma/plugin.rb +1 -10
  59. data/lib/puma/rack/builder.rb +0 -4
  60. data/lib/puma/rack/urlmap.rb +0 -0
  61. data/lib/puma/rack_default.rb +0 -0
  62. data/lib/puma/reactor.rb +9 -4
  63. data/lib/puma/runner.rb +8 -36
  64. data/lib/puma/server.rb +122 -215
  65. data/lib/puma/single.rb +7 -64
  66. data/lib/puma/state_file.rb +6 -3
  67. data/lib/puma/thread_pool.rb +94 -49
  68. data/lib/puma/util.rb +0 -0
  69. data/lib/puma.rb +17 -0
  70. data/lib/rack/handler/puma.rb +1 -3
  71. data/tools/{docker/Dockerfile → Dockerfile} +0 -0
  72. data/tools/trickletest.rb +0 -0
  73. metadata +18 -20
  74. data/docs/tcp_mode.md +0 -96
  75. data/ext/puma_http11/io_buffer.c +0 -155
  76. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +0 -72
  77. data/lib/puma/tcp_logger.rb +0 -41
  78. data/tools/jungle/README.md +0 -19
  79. data/tools/jungle/init.d/README.md +0 -61
  80. data/tools/jungle/init.d/puma +0 -421
  81. data/tools/jungle/init.d/run-puma +0 -18
  82. /data/{tools → docs}/jungle/rc.d/puma +0 -0
data/lib/puma/server.rb CHANGED
@@ -9,10 +9,8 @@ require 'puma/null_io'
9
9
  require 'puma/reactor'
10
10
  require 'puma/client'
11
11
  require 'puma/binder'
12
- require 'puma/accept_nonblock'
13
12
  require 'puma/util'
14
-
15
- require 'puma/puma_http11'
13
+ require 'puma/io_buffer'
16
14
 
17
15
  require 'socket'
18
16
  require 'forwardable'
@@ -36,6 +34,7 @@ module Puma
36
34
 
37
35
  attr_reader :thread
38
36
  attr_reader :events
37
+ attr_reader :requests_count # @version 5.0.0
39
38
  attr_accessor :app
40
39
 
41
40
  attr_accessor :min_threads
@@ -57,8 +56,7 @@ module Puma
57
56
  @app = app
58
57
  @events = events
59
58
 
60
- @check, @notify = Puma::Util.pipe
61
-
59
+ @check, @notify = nil
62
60
  @status = :stop
63
61
 
64
62
  @min_threads = 0
@@ -85,24 +83,34 @@ module Puma
85
83
  @mode = :http
86
84
 
87
85
  @precheck_closing = true
86
+
87
+ @requests_count = 0
88
88
  end
89
89
 
90
90
  attr_accessor :binder, :leak_stack_on_error, :early_hints
91
91
 
92
- def_delegators :@binder, :add_tcp_listener, :add_ssl_listener, :add_unix_listener, :connected_port
92
+ def_delegators :@binder, :add_tcp_listener, :add_ssl_listener, :add_unix_listener, :connected_ports
93
93
 
94
94
  def inherit_binder(bind)
95
95
  @binder = bind
96
96
  end
97
97
 
98
- def tcp_mode!
99
- @mode = :tcp
98
+ class << self
99
+ # :nodoc:
100
+ # @version 5.0.0
101
+ def tcp_cork_supported?
102
+ RbConfig::CONFIG['host_os'] =~ /linux/ &&
103
+ Socket.const_defined?(:IPPROTO_TCP) &&
104
+ Socket.const_defined?(:TCP_CORK) &&
105
+ Socket.const_defined?(:TCP_INFO)
106
+ end
107
+ private :tcp_cork_supported?
100
108
  end
101
109
 
102
110
  # On Linux, use TCP_CORK to better control how the TCP stack
103
111
  # packetizes our stream. This improves both latency and throughput.
104
112
  #
105
- if RUBY_PLATFORM =~ /linux/
113
+ if tcp_cork_supported?
106
114
  UNPACK_TCP_STATE_FROM_TCP_INFO = "C".freeze
107
115
 
108
116
  # 6 == Socket::IPPROTO_TCP
@@ -110,7 +118,7 @@ module Puma
110
118
  # 1/0 == turn on/off
111
119
  def cork_socket(socket)
112
120
  begin
113
- socket.setsockopt(6, 3, 1) if socket.kind_of? TCPSocket
121
+ socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_CORK, 1) if socket.kind_of? TCPSocket
114
122
  rescue IOError, SystemCallError
115
123
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
116
124
  end
@@ -118,7 +126,7 @@ module Puma
118
126
 
119
127
  def uncork_socket(socket)
120
128
  begin
121
- socket.setsockopt(6, 3, 0) if socket.kind_of? TCPSocket
129
+ socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_CORK, 0) if socket.kind_of? TCPSocket
122
130
  rescue IOError, SystemCallError
123
131
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
124
132
  end
@@ -129,7 +137,7 @@ module Puma
129
137
  return false unless @precheck_closing
130
138
 
131
139
  begin
132
- tcp_info = socket.getsockopt(Socket::SOL_TCP, Socket::TCP_INFO)
140
+ tcp_info = socket.getsockopt(Socket::IPPROTO_TCP, Socket::TCP_INFO)
133
141
  rescue IOError, SystemCallError
134
142
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
135
143
  @precheck_closing = false
@@ -172,107 +180,6 @@ module Puma
172
180
  @thread_pool and @thread_pool.pool_capacity
173
181
  end
174
182
 
175
- # Lopez Mode == raw tcp apps
176
-
177
- def run_lopez_mode(background=true)
178
- @thread_pool = ThreadPool.new(@min_threads,
179
- @max_threads,
180
- Hash) do |client, tl|
181
-
182
- io = client.to_io
183
- addr = io.peeraddr.last
184
-
185
- if addr.empty?
186
- # Set unix socket addrs to localhost
187
- addr = "127.0.0.1:0"
188
- else
189
- addr = "#{addr}:#{io.peeraddr[1]}"
190
- end
191
-
192
- env = { 'thread' => tl, REMOTE_ADDR => addr }
193
-
194
- begin
195
- @app.call env, client.to_io
196
- rescue Object => e
197
- STDERR.puts "! Detected exception at toplevel: #{e.message} (#{e.class})"
198
- STDERR.puts e.backtrace
199
- end
200
-
201
- client.close unless env['detach']
202
- end
203
-
204
- @events.fire :state, :running
205
-
206
- if background
207
- @thread = Thread.new do
208
- Puma.set_thread_name "server"
209
- handle_servers_lopez_mode
210
- end
211
- return @thread
212
- else
213
- handle_servers_lopez_mode
214
- end
215
- end
216
-
217
- def handle_servers_lopez_mode
218
- begin
219
- check = @check
220
- sockets = [check] + @binder.ios
221
- pool = @thread_pool
222
-
223
- while @status == :run
224
- begin
225
- ios = IO.select sockets
226
- ios.first.each do |sock|
227
- if sock == check
228
- break if handle_check
229
- else
230
- begin
231
- if io = sock.accept_nonblock
232
- client = Client.new io, nil
233
- pool << client
234
- end
235
- rescue SystemCallError
236
- # nothing
237
- rescue Errno::ECONNABORTED
238
- # client closed the socket even before accept
239
- begin
240
- io.close
241
- rescue
242
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
243
- end
244
- end
245
- end
246
- end
247
- rescue Object => e
248
- @events.unknown_error self, e, "Listen loop"
249
- end
250
- end
251
-
252
- @events.fire :state, @status
253
-
254
- graceful_shutdown if @status == :stop || @status == :restart
255
-
256
- rescue Exception => e
257
- STDERR.puts "Exception handling servers: #{e.message} (#{e.class})"
258
- STDERR.puts e.backtrace
259
- ensure
260
- begin
261
- @check.close
262
- rescue
263
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
264
- end
265
-
266
- # Prevent can't modify frozen IOError (RuntimeError)
267
- begin
268
- @notify.close
269
- rescue IOError
270
- # no biggy
271
- end
272
- end
273
-
274
- @events.fire :state, :done
275
- end
276
183
  # Runs the server.
277
184
  #
278
185
  # If +background+ is true (the default) then a thread is spun
@@ -286,15 +193,9 @@ module Puma
286
193
 
287
194
  @status = :run
288
195
 
289
- if @mode == :tcp
290
- return run_lopez_mode(background)
291
- end
292
-
293
- queue_requests = @queue_requests
294
-
295
196
  @thread_pool = ThreadPool.new(@min_threads,
296
197
  @max_threads,
297
- IOBuffer) do |client, buffer|
198
+ ::Puma::IOBuffer) do |client, buffer|
298
199
 
299
200
  # Advertise this server into the thread
300
201
  Thread.current[ThreadLocalKey] = self
@@ -302,10 +203,10 @@ module Puma
302
203
  process_now = false
303
204
 
304
205
  begin
305
- if queue_requests
206
+ if @queue_requests
306
207
  process_now = client.eagerly_finish
307
208
  else
308
- client.finish
209
+ client.finish(@first_data_timeout)
309
210
  process_now = true
310
211
  end
311
212
  rescue MiniSSL::SSLError => e
@@ -315,18 +216,16 @@ module Puma
315
216
 
316
217
  client.close
317
218
 
318
- @events.ssl_error self, addr, cert, e
219
+ @events.ssl_error e, addr, cert
319
220
  rescue HttpParserError => e
320
221
  client.write_error(400)
321
222
  client.close
322
223
 
323
- @events.parse_error self, client.env, e
324
- rescue HttpParserError501 => e
325
- client.write_error(501)
326
- client.close
327
- @events.parse_error self, client.env, e
328
- rescue ConnectionError, EOFError
224
+ @events.parse_error e, client
225
+ rescue ConnectionError, EOFError => e
329
226
  client.close
227
+
228
+ @events.connection_error e, client
330
229
  else
331
230
  if process_now
332
231
  process_client client, buffer
@@ -335,11 +234,14 @@ module Puma
335
234
  @reactor.add client
336
235
  end
337
236
  end
237
+
238
+ process_now
338
239
  end
339
240
 
241
+ @thread_pool.out_of_band_hook = @options[:out_of_band]
340
242
  @thread_pool.clean_thread_locals = @options[:clean_thread_locals]
341
243
 
342
- if queue_requests
244
+ if @queue_requests
343
245
  @reactor = Reactor.new self, @thread_pool
344
246
  @reactor.run_in_thread
345
247
  end
@@ -366,6 +268,7 @@ module Puma
366
268
  end
367
269
 
368
270
  def handle_servers
271
+ @check, @notify = Puma::Util.pipe unless @notify
369
272
  begin
370
273
  check = @check
371
274
  sockets = [check] + @binder.ios
@@ -389,51 +292,49 @@ module Puma
389
292
  if sock == check
390
293
  break if handle_check
391
294
  else
392
- begin
393
- if io = sock.accept_nonblock
394
- client = Client.new io, @binder.env(sock)
395
- if remote_addr_value
396
- client.peerip = remote_addr_value
397
- elsif remote_addr_header
398
- client.remote_addr_header = remote_addr_header
399
- end
400
-
401
- pool << client
402
- busy_threads = pool.wait_until_not_full
403
- if busy_threads == 0
404
- @options[:out_of_band].each(&:call) if @options[:out_of_band]
405
- end
406
- end
407
- rescue SystemCallError
408
- # nothing
409
- rescue Errno::ECONNABORTED
410
- # client closed the socket even before accept
411
- begin
412
- io.close
413
- rescue
414
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
415
- end
295
+ pool.wait_until_not_full
296
+ pool.wait_for_less_busy_worker(
297
+ @options[:wait_for_less_busy_worker].to_f)
298
+
299
+ io = begin
300
+ sock.accept_nonblock
301
+ rescue IO::WaitReadable
302
+ next
303
+ end
304
+ client = Client.new io, @binder.env(sock)
305
+ if remote_addr_value
306
+ client.peerip = remote_addr_value
307
+ elsif remote_addr_header
308
+ client.remote_addr_header = remote_addr_header
416
309
  end
310
+ pool << client
417
311
  end
418
312
  end
419
313
  rescue Object => e
420
- @events.unknown_error self, e, "Listen loop"
314
+ @events.unknown_error e, nil, "Listen loop"
421
315
  end
422
316
  end
423
317
 
424
318
  @events.fire :state, @status
425
319
 
426
- graceful_shutdown if @status == :stop || @status == :restart
427
320
  if queue_requests
321
+ @queue_requests = false
428
322
  @reactor.clear!
429
323
  @reactor.shutdown
430
324
  end
325
+ graceful_shutdown if @status == :stop || @status == :restart
431
326
  rescue Exception => e
432
- STDERR.puts "Exception handling servers: #{e.message} (#{e.class})"
433
- STDERR.puts e.backtrace
327
+ @events.unknown_error e, nil, "Exception handling servers"
434
328
  ensure
435
- @check.close
329
+ begin
330
+ @check.close unless @check.closed?
331
+ rescue Errno::EBADF, RuntimeError
332
+ # RuntimeError is Ruby 2.2 issue, can't modify frozen IOError
333
+ # Errno::EBADF is infrequently raised
334
+ end
436
335
  @notify.close
336
+ @notify = nil
337
+ @check = nil
437
338
  end
438
339
 
439
340
  @events.fire :state, :done
@@ -480,29 +381,24 @@ module Puma
480
381
  close_socket = false
481
382
  return
482
383
  when true
483
- return unless @queue_requests
484
384
  buffer.reset
485
385
 
486
386
  ThreadPool.clean_thread_locals if clean_thread_locals
487
387
 
488
388
  requests += 1
489
389
 
490
- # Closing keepalive sockets after they've made a reasonable
491
- # number of requests allows Puma to service many connections
492
- # fairly, even when the number of concurrent connections exceeds
493
- # the size of the threadpool. It also allows cluster mode Pumas
494
- # to keep load evenly distributed across workers, because clients
495
- # are randomly assigned a new worker when opening a new connection.
496
- #
497
- # Previously, Puma would kick connections in this conditional back
498
- # to the reactor. However, because this causes the todo set to increase
499
- # in size, the wait_until_full mutex would never unlock, leaving
500
- # any additional connections unserviced.
501
- break if requests >= MAX_FAST_INLINE
502
-
503
390
  check_for_more_data = @status == :run
504
391
 
392
+ if requests >= MAX_FAST_INLINE
393
+ # This will mean that reset will only try to use the data it already
394
+ # has buffered and won't try to read more data. What this means is that
395
+ # every client, independent of their request speed, gets treated like a slow
396
+ # one once every MAX_FAST_INLINE requests.
397
+ check_for_more_data = false
398
+ end
399
+
505
400
  unless client.reset(check_for_more_data)
401
+ return unless @queue_requests
506
402
  close_socket = false
507
403
  client.set_timeout @persistent_timeout
508
404
  @reactor.add client
@@ -525,7 +421,7 @@ module Puma
525
421
 
526
422
  close_socket = true
527
423
 
528
- @events.ssl_error self, addr, cert, e
424
+ @events.ssl_error e, addr, cert
529
425
 
530
426
  # The client doesn't know HTTP well
531
427
  rescue HttpParserError => e
@@ -533,21 +429,15 @@ module Puma
533
429
 
534
430
  client.write_error(400)
535
431
 
536
- @events.parse_error self, client.env, e
537
- rescue HttpParserError501 => e
538
- lowlevel_error(e, client.env)
539
-
540
- client.write_error(501)
432
+ @events.parse_error e, client
541
433
 
542
- @events.parse_error self, client.env, e
543
434
  # Server error
544
435
  rescue StandardError => e
545
436
  lowlevel_error(e, client.env)
546
437
 
547
438
  client.write_error(500)
548
439
 
549
- @events.unknown_error self, e, "Read"
550
-
440
+ @events.unknown_error e, nil, "Read"
551
441
  ensure
552
442
  buffer.reset
553
443
 
@@ -557,7 +447,7 @@ module Puma
557
447
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
558
448
  # Already closed
559
449
  rescue StandardError => e
560
- @events.unknown_error self, e, "Client"
450
+ @events.unknown_error e, nil, "Client"
561
451
  end
562
452
  end
563
453
  end
@@ -593,7 +483,7 @@ module Puma
593
483
 
594
484
  env[PATH_INFO] = env[REQUEST_PATH]
595
485
 
596
- # From http://www.ietf.org/rfc/rfc3875 :
486
+ # From https://www.ietf.org/rfc/rfc3875 :
597
487
  # "Script authors should be aware that the REMOTE_ADDR and
598
488
  # REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
599
489
  # may not identify the ultimate source of the request.
@@ -640,6 +530,8 @@ module Puma
640
530
  #
641
531
  # Finally, it'll return +true+ on keep-alive connections.
642
532
  def handle_request(req, lines)
533
+ @requests_count +=1
534
+
643
535
  env = req.env
644
536
  client = req.io
645
537
 
@@ -680,7 +572,8 @@ module Puma
680
572
  end
681
573
 
682
574
  fast_write client, "\r\n".freeze
683
- rescue ConnectionError
575
+ rescue ConnectionError => e
576
+ @events.debug_error e
684
577
  # noop, if we lost the socket we just won't send the early hints
685
578
  end
686
579
  }
@@ -738,17 +631,14 @@ module Puma
738
631
  return :async
739
632
  end
740
633
  rescue ThreadPool::ForceShutdown => e
741
- @events.log "Detected force shutdown of a thread, returning 503"
742
- @events.unknown_error self, e, "Rack app"
743
-
744
- status = 503
745
- headers = {}
746
- res_body = ["Request was internally terminated early\n"]
634
+ @events.unknown_error e, req, "Rack app"
635
+ @events.log "Detected force shutdown of a thread"
747
636
 
637
+ status, headers, res_body = lowlevel_error(e, env, 503)
748
638
  rescue Exception => e
749
- @events.unknown_error self, e, "Rack app", env
639
+ @events.unknown_error e, req, "Rack app"
750
640
 
751
- status, headers, res_body = lowlevel_error(e, env)
641
+ status, headers, res_body = lowlevel_error(e, env, 500)
752
642
  end
753
643
 
754
644
  content_length = nil
@@ -763,10 +653,10 @@ module Puma
763
653
  line_ending = LINE_END
764
654
  colon = COLON
765
655
 
766
- http_11 = if env[HTTP_VERSION] == HTTP_11
656
+ http_11 = env[HTTP_VERSION] == HTTP_11
657
+ if http_11
767
658
  allow_chunked = true
768
659
  keep_alive = env.fetch(HTTP_CONNECTION, "").downcase != CLOSE
769
- include_keepalive_header = false
770
660
 
771
661
  # An optimization. The most common response is 200, so we can
772
662
  # reply with the proper 200 status without having to compute
@@ -780,11 +670,9 @@ module Puma
780
670
 
781
671
  no_body ||= status < 200 || STATUS_WITH_NO_ENTITY_BODY[status]
782
672
  end
783
- true
784
673
  else
785
674
  allow_chunked = false
786
675
  keep_alive = env.fetch(HTTP_CONNECTION, "").downcase == KEEP_ALIVE
787
- include_keepalive_header = keep_alive
788
676
 
789
677
  # Same optimization as above for HTTP/1.1
790
678
  #
@@ -796,9 +684,12 @@ module Puma
796
684
 
797
685
  no_body ||= status < 200 || STATUS_WITH_NO_ENTITY_BODY[status]
798
686
  end
799
- false
800
687
  end
801
688
 
689
+ # regardless of what the client wants, we always close the connection
690
+ # if running without request queueing
691
+ keep_alive &&= @queue_requests
692
+
802
693
  response_hijack = nil
803
694
 
804
695
  headers.each do |k, vs|
@@ -825,10 +716,15 @@ module Puma
825
716
  end
826
717
  end
827
718
 
828
- if include_keepalive_header
829
- lines << CONNECTION_KEEP_ALIVE
830
- elsif http_11 && !keep_alive
831
- lines << CONNECTION_CLOSE
719
+ # HTTP/1.1 & 1.0 assume different defaults:
720
+ # - HTTP 1.0 assumes the connection will be closed if not specified
721
+ # - HTTP 1.1 assumes the connection will be kept alive if not specified.
722
+ # Only set the header if we're doing something which is not the default
723
+ # for this protocol version
724
+ if http_11
725
+ lines << CONNECTION_CLOSE if !keep_alive
726
+ else
727
+ lines << CONNECTION_KEEP_ALIVE if keep_alive
832
728
  end
833
729
 
834
730
  if no_body
@@ -882,14 +778,11 @@ module Puma
882
778
  end
883
779
 
884
780
  ensure
885
- begin
886
- uncork_socket client
781
+ uncork_socket client
887
782
 
888
- body.close
889
- req.tempfile.unlink if req.tempfile
890
- ensure
891
- res_body.close if res_body.respond_to? :close
892
- end
783
+ body.close
784
+ req.tempfile.unlink if req.tempfile
785
+ res_body.close if res_body.respond_to? :close
893
786
 
894
787
  after_reply.each { |o| o.call }
895
788
  end
@@ -958,19 +851,21 @@ module Puma
958
851
 
959
852
  # A fallback rack response if +@app+ raises as exception.
960
853
  #
961
- def lowlevel_error(e, env)
854
+ def lowlevel_error(e, env, status=500)
962
855
  if handler = @options[:lowlevel_error_handler]
963
856
  if handler.arity == 1
964
857
  return handler.call(e)
965
- else
858
+ elsif handler.arity == 2
966
859
  return handler.call(e, env)
860
+ else
861
+ return handler.call(e, env, status)
967
862
  end
968
863
  end
969
864
 
970
865
  if @leak_stack_on_error
971
- [500, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
866
+ [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
972
867
  else
973
- [500, {}, ["An unhandled lowlevel error occurred. The application logs may have details.\n"]]
868
+ [status, {}, ["An unhandled lowlevel error occurred. The application logs may have details.\n"]]
974
869
  end
975
870
  end
976
871
 
@@ -1028,9 +923,10 @@ module Puma
1028
923
  end
1029
924
 
1030
925
  def notify_safely(message)
926
+ @check, @notify = Puma::Util.pipe unless @notify
1031
927
  begin
1032
928
  @notify << message
1033
- rescue IOError
929
+ rescue IOError, NoMethodError, Errno::EPIPE
1034
930
  # The server, in another thread, is shutting down
1035
931
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
1036
932
  rescue RuntimeError => e
@@ -1057,8 +953,9 @@ module Puma
1057
953
  @thread.join if @thread && sync
1058
954
  end
1059
955
 
1060
- def begin_restart
956
+ def begin_restart(sync=false)
1061
957
  notify_safely(RESTART_COMMAND)
958
+ @thread.join if @thread && sync
1062
959
  end
1063
960
 
1064
961
  def fast_write(io, str)
@@ -1096,5 +993,15 @@ module Puma
1096
993
  HTTP_INJECTION_REGEX =~ header_value.to_s
1097
994
  end
1098
995
  private :possible_header_injection?
996
+
997
+ # List of methods invoked by #stats.
998
+ # @version 5.0.0
999
+ STAT_METHODS = [:backlog, :running, :pool_capacity, :max_threads, :requests_count].freeze
1000
+
1001
+ # Returns a hash of stats about the running server for reporting purposes.
1002
+ # @version 5.0.0
1003
+ def stats
1004
+ STAT_METHODS.map {|name| [name, send(name) || 0]}.to_h
1005
+ end
1099
1006
  end
1100
1007
  end