puma 5.2.2 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puma might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 472ee56cda171d14d1ba6dee3f83d6b5cce8458e6346d36443295c6aae8ca243
4
- data.tar.gz: 625953f46071794b66b44974a74a4382015ecef0ce85b629cf7448935d1f15c8
3
+ metadata.gz: d9709c7f9af0da844635e40251071200eff2784848441a2b8122d6541856997d
4
+ data.tar.gz: 65f33ca45bac17196d5c89b250bed05fc9163c3b394e7f233d4186faa12ffb69
5
5
  SHA512:
6
- metadata.gz: b8510e5f6e17815d2d36be57806f70294d4796cf7a63c7c9e76cfc31454f314b46da8ab3a83b2f35436e4ae98a1a138dbb20c9f75fee89a41edeeb5689459ff4
7
- data.tar.gz: ef268e432c892a993831786227eee70ab3c013b5aaf306538e603a405cbc40457dffd961acfefa98a0fef6117f4b6463634f60fd4a5e111c79f361f7e5ae28e4
6
+ metadata.gz: 9b8387e123c59e059aaef46b3c96cdb5f52c0e70b43521a6813cfed18e0d8274d63128df24ad3cad889277fc2bd86381a76bc1c4a464bced44d7de9f3aa468f6
7
+ data.tar.gz: 3edab3f759e88fa5335cae9c64c782ee664d84c6d3b20ee387a060c8529e85bbf8716ebf25f783609f20916247a5d6529024c50a4280dc687e761f816c2f8a06
data/History.md CHANGED
@@ -1,3 +1,31 @@
1
+ ## 5.3.0 / 2021-05-07
2
+
3
+ * Features
4
+ * Add support for Linux's abstract sockets ([#2564], [#2526])
5
+ * Add debug to worker timeout and startup ([#2559], [#2528])
6
+ * Print warning when running one-worker cluster ([#2565], [#2534])
7
+ * Don't close systemd activated socket on pumactl restart ([#2563], [#2504])
8
+
9
+ * Bugfixes
10
+ * systemd - fix event firing ([#2591], [#2572])
11
+ * Immediately unlink temporary files ([#2613])
12
+ * Improve parsing of HTTP_HOST header ([#2605], [#2584])
13
+ * Handle fatal error that has no backtrace ([#2607], [#2552])
14
+ * Fix timing out requests too early ([#2606], [#2574])
15
+ * Handle segfault in Ruby 2.6.6 on thread-locals ([#2567], [#2566])
16
+ * Server#closed_socket? - parameter may be a MiniSSL::Socket ([#2596])
17
+ * Define UNPACK_TCP_STATE_FROM_TCP_INFO in the right place ([#2588], [#2556])
18
+ * request.rb - fix chunked assembly for ascii incompatible encodings, add test ([#2585], [#2583])
19
+
20
+ * Performance
21
+ * Reset peerip only if remote_addr_header is set ([#2609])
22
+ * Reduce puma_parser struct size ([#2590])
23
+
24
+ * Refactor
25
+ * Refactor drain on shutdown ([#2600])
26
+ * Micro optimisations in `wait_for_less_busy_worker` feature ([#2579])
27
+ * Lots of test fixes
28
+
1
29
  ## 5.2.2 / 2021-02-22
2
30
 
3
31
  * Bugfixes
@@ -1708,8 +1736,36 @@ be added back in a future date when a java Puma::MiniSSL is added.
1708
1736
  * Bugfixes
1709
1737
  * Your bugfix goes here <Most recent on the top, like GitHub> (#Github Number)
1710
1738
 
1739
+ [#2564]:https://github.com/puma/puma/pull/2564 "PR by @MSP-Greg, merged 2021-04-24"
1740
+ [#2526]:https://github.com/puma/puma/issues/2526 "Issue by @nerdrew, closed 2021-04-24"
1741
+ [#2559]:https://github.com/puma/puma/pull/2559 "PR by @ylecuyer, merged 2021-03-11"
1742
+ [#2528]:https://github.com/puma/puma/issues/2528 "Issue by @cjlarose, closed 2021-03-11"
1743
+ [#2565]:https://github.com/puma/puma/pull/2565 "PR by @CGA1123, merged 2021-03-09"
1744
+ [#2534]:https://github.com/puma/puma/issues/2534 "Issue by @nateberkopec, closed 2021-03-09"
1745
+ [#2563]:https://github.com/puma/puma/pull/2563 "PR by @MSP-Greg, merged 2021-03-06"
1746
+ [#2504]:https://github.com/puma/puma/issues/2504 "Issue by @fsateler, closed 2021-03-06"
1747
+ [#2591]:https://github.com/puma/puma/pull/2591 "PR by @MSP-Greg, merged 2021-05-05"
1748
+ [#2572]:https://github.com/puma/puma/issues/2572 "Issue by @josefbilendo, closed 2021-05-05"
1749
+ [#2613]:https://github.com/puma/puma/pull/2613 "PR by @smcgivern, merged 2021-04-27"
1750
+ [#2605]:https://github.com/puma/puma/pull/2605 "PR by @pascalbetz, merged 2021-04-26"
1751
+ [#2584]:https://github.com/puma/puma/issues/2584 "Issue by @kaorihinata, closed 2021-04-26"
1752
+ [#2607]:https://github.com/puma/puma/pull/2607 "PR by @calvinxiao, merged 2021-04-23"
1753
+ [#2552]:https://github.com/puma/puma/issues/2552 "Issue by @feliperaul, opened 2021-02-09"
1754
+ [#2606]:https://github.com/puma/puma/pull/2606 "PR by @wjordan, merged 2021-04-20"
1755
+ [#2574]:https://github.com/puma/puma/issues/2574 "Issue by @darkhelmet, closed 2021-04-20"
1756
+ [#2567]:https://github.com/puma/puma/pull/2567 "PR by @kddeisz, merged 2021-04-19"
1757
+ [#2566]:https://github.com/puma/puma/issues/2566 "Issue by @kddeisz, closed 2021-04-19"
1758
+ [#2596]:https://github.com/puma/puma/pull/2596 "PR by @MSP-Greg, merged 2021-04-18"
1759
+ [#2588]:https://github.com/puma/puma/pull/2588 "PR by @dentarg, merged 2021-04-02"
1760
+ [#2556]:https://github.com/puma/puma/issues/2556 "Issue by @gamecreature, closed 2021-04-02"
1761
+ [#2585]:https://github.com/puma/puma/pull/2585 "PR by @MSP-Greg, merged 2021-03-26"
1762
+ [#2583]:https://github.com/puma/puma/issues/2583 "Issue by @jboler, closed 2021-03-26"
1763
+ [#2609]:https://github.com/puma/puma/pull/2609 "PR by @calvinxiao, merged 2021-04-26"
1764
+ [#2590]:https://github.com/puma/puma/pull/2590 "PR by @calvinxiao, merged 2021-04-05"
1765
+ [#2600]:https://github.com/puma/puma/pull/2600 "PR by @wjordan, merged 2021-04-30"
1766
+ [#2579]:https://github.com/puma/puma/pull/2579 "PR by @ghiculescu, merged 2021-03-17"
1767
+ [#2553]:https://github.com/puma/puma/pull/2553 "PR by @olivierbellone, merged 2021-02-10"
1711
1768
  [#2557]:https://github.com/puma/puma/pull/2557 "PR by @cjlarose, merged 2021-02-22"
1712
- [#2553]:https://github.com/puma/puma/pull/2553 "PR by @olivierbellone, merged 02-10-22"
1713
1769
  [#2550]:https://github.com/puma/puma/pull/2550 "PR by @MSP-Greg, merged 2021-02-05"
1714
1770
  [#2547]:https://github.com/puma/puma/pull/2547 "PR by @wildmaples, merged 2021-02-03"
1715
1771
  [#2543]:https://github.com/puma/puma/pull/2543 "PR by @MSP-Greg, merged 2021-02-01"
data/README.md CHANGED
@@ -16,7 +16,7 @@ Puma is a **simple, fast, multi-threaded, and highly concurrent HTTP 1.1 server
16
16
 
17
17
  Puma processes requests using a C-optimized Ragel extension (inherited from Mongrel) that provides fast, accurate HTTP 1.1 protocol parsing in a portable way. Puma then serves the request using a thread pool. Each request is served in a separate thread, so truly concurrent Ruby implementations (JRuby, Rubinius) will use all available CPU cores.
18
18
 
19
- Puma was designed to be the go-to server for [Rubinius](https://rubinius.com), but also works well with JRuby and MRI.
19
+ Originally designed as a server for [Rubinius](https://github.com/rubinius/rubinius), Puma also works well with Ruby (MRI) and JRuby.
20
20
 
21
21
  On MRI, there is a Global VM Lock (GVL) that ensures only one thread can run Ruby code at a time. But if you're doing a lot of blocking IO (such as HTTP calls to external APIs like Twitter), Puma still improves MRI's throughput by allowing IO waiting to be done in parallel.
22
22
 
data/docs/architecture.md CHANGED
@@ -6,11 +6,12 @@
6
6
 
7
7
  Puma is a threaded Ruby HTTP application server, processing requests across a TCP or UNIX socket.
8
8
 
9
- Puma processes (there can be one or many) accept connections from the socket via a thread (in the `Reactor` class). The connection, once fully buffered and read, moves in to the `todo` list, where it will be picked up by a free/waiting thread in the threadpool (the `ThreadPool` class).
9
+
10
+ Puma processes (there can be one or many) accept connections from the socket via a thread (in the [`Reactor`](../lib/puma/reactor.rb) class). The connection, once fully buffered and read, moves in to the `todo` list, where it will be picked up by a free/waiting thread in the threadpool (the [`ThreadPool`](../lib/puma/thread_pool.rb) class).
10
11
 
11
12
  Puma works in two main modes: cluster and single. In single mode, only one Puma process is booted. In cluster mode, a `master` process is booted, which prepares (and may boot) the application, and then uses the `fork()` system call to create 1 or more `child` processes. These `child` processes all listen to the same socket. The `master` process does not listen to the socket or process requests - its purpose is mostly to manage and listen for UNIX signals and possibly kill or boot `child` processes.
12
13
 
13
- We sometimes call `child` processes (or Puma processes in `single` mode) _workers_, and we sometimes call the threads created by Puma's `ThreadPool` _worker threads_.
14
+ We sometimes call `child` processes (or Puma processes in `single` mode) _workers_, and we sometimes call the threads created by Puma's [`ThreadPool`](../lib/puma/thread_pool.rb) _worker threads_.
14
15
 
15
16
  ## How Requests Work
16
17
 
@@ -18,8 +19,8 @@ We sometimes call `child` processes (or Puma processes in `single` mode) _worker
18
19
 
19
20
  * Upon startup, Puma listens on a TCP or UNIX socket.
20
21
  * The backlog of this socket is configured (with a default of 1024). This determines the size of the queue for unaccepted connections. Generally, this setting is unimportant and will never be hit in production use. If the backlog is full, the connection will be refused by the operating system.
21
- * This socket backlog is distinct from the `backlog` of work as reported by `Puma.stats` or the control server. The backlog as reported by Puma is the number of connections in the process' `todo` set waiting for a thread from the `ThreadPool`.
22
- * By default, a single, separate thread (created by the `Reactor` class) is used to read and buffer requests from the socket.
22
+ * This socket backlog is distinct from the `backlog` of work as reported by `Puma.stats` or the control server. The backlog as reported by Puma is the number of connections in the process' `todo` set waiting for a thread from the [`ThreadPool`](../lib/puma/thread_pool.rb).
23
+ * By default, a single, separate thread (created by the [`Reactor`](../lib/puma/reactor.rb) class) is used to read and buffer requests from the socket.
23
24
  * When at least one worker thread is available for work, the reactor thread listens to the socket and accepts a request, if one is waiting.
24
25
  * The reactor thread waits for the entire HTTP request to be received.
25
26
  * The time spent waiting for the HTTP request body to be received is exposed to the Rack app as `env['puma.request_body_wait']` (milliseconds).
data/docs/deployment.md CHANGED
@@ -16,7 +16,7 @@ Welcome back!
16
16
 
17
17
  ## Single vs Cluster mode
18
18
 
19
- Puma was originally conceived as a thread-only webserver, but grew the ability to
19
+ Puma was originally conceived as a thread-only web server, but grew the ability to
20
20
  also use processes in version 2.
21
21
 
22
22
  To run `puma` in single mode (e.g. for a development environment) you will need to
@@ -1,6 +1,6 @@
1
1
  # Puma as a service using rc.d
2
2
 
3
- Manage multilpe Puma servers as services on one box using FreeBSD's rc.d service.
3
+ Manage multiple Puma servers as services on one box using FreeBSD's rc.d service.
4
4
 
5
5
  ## Dependencies
6
6
 
data/docs/kubernetes.md CHANGED
@@ -61,6 +61,6 @@ For some high-throughput systems, it is possible that some HTTP requests will re
61
61
 
62
62
  There is a subtle race condition between step 2 and 3: The replication controller does not synchronously remove the pod from the Services AND THEN call the pre-stop hook of the pod, but rather it asynchronously sends "remove this pod from your endpoints" requests to the Services and then immediately proceeds to invoke the pods' pre-stop hook. If the Service controller (typically something like nginx or haproxy) receives this request handles this request "too" late (due to internal lag or network latency between the replication and Service controllers) then it is possible that the Service controller will send one or more requests to a Puma process which has already shut down its listening socket. These requests will then fail with 5XX error codes.
63
63
 
64
- The way Kubernetes works this way, rather than handling step 2 synchronously, is due to the CAP theorem: in a distributed system there is no way to guarantuee that any message will arrive promptly. In particular, waiting for all Service controllers to report back might get stuck for an indefinite time if one of them has already been terminated or if there has been a net split. A way to work around this is to add a sleep to the pre-stop hook of the same time as the `terminationGracePeriodSeconds` time. This will allow the Puma process to keep serving new requests during the entire grace period, although it will no longer receive new requests after all Service controllers have propagated the removal of the pod from their endpoint lists. Then, after `terminationGracePeriodSeconds`, the pod receives `SIGKILL` and closes down. If your process can't handle SIGKILL properly, for example because it needs to release locks in different services, you can also sleep for a shorter period (and/or increase `terminationGracePeriodSeconds`) as long as the time slept is longer than the time that your Service controllers take to propagate the pod removal. The downside of this workaround is that all pods will take at minimum the amount of time slept to shut down and this will increase the time required for your rolling deploy.
64
+ The way Kubernetes works this way, rather than handling step 2 synchronously, is due to the CAP theorem: in a distributed system there is no way to guarantee that any message will arrive promptly. In particular, waiting for all Service controllers to report back might get stuck for an indefinite time if one of them has already been terminated or if there has been a net split. A way to work around this is to add a sleep to the pre-stop hook of the same time as the `terminationGracePeriodSeconds` time. This will allow the Puma process to keep serving new requests during the entire grace period, although it will no longer receive new requests after all Service controllers have propagated the removal of the pod from their endpoint lists. Then, after `terminationGracePeriodSeconds`, the pod receives `SIGKILL` and closes down. If your process can't handle SIGKILL properly, for example because it needs to release locks in different services, you can also sleep for a shorter period (and/or increase `terminationGracePeriodSeconds`) as long as the time slept is longer than the time that your Service controllers take to propagate the pod removal. The downside of this workaround is that all pods will take at minimum the amount of time slept to shut down and this will increase the time required for your rolling deploy.
65
65
 
66
66
  More discussions and links to relevant articles can be found in https://github.com/puma/puma/issues/2343.
data/docs/plugins.md CHANGED
@@ -34,5 +34,5 @@ functionality to augment puma.
34
34
  `config` runs when the server is being configured and is passed a `Puma::DSL`
35
35
  object that can be used to add additional configuration.
36
36
 
37
- Any public methods in `Puma::Plugin` are the public API that any plugin may
37
+ Any public methods in [`Puma::Plugin`](../lib/puma/plugin.rb) are the public API that any plugin may
38
38
  use.
data/docs/restart.md CHANGED
@@ -45,7 +45,7 @@ Any of the following will cause a Puma server to perform a phased restart:
45
45
  ### Supported configurations
46
46
 
47
47
  * Works in cluster mode only
48
- * To support upgrading the application that Puma is serving, ensure `prune_bundler` is enabled and that `preload_app` is disabled (it is disabled by default).
48
+ * To support upgrading the application that Puma is serving, ensure `prune_bundler` is enabled and that `preload_app!` is disabled
49
49
  * Supported on all platforms where cluster mode is supported
50
50
 
51
51
  ### Client experience
data/docs/stats.md CHANGED
@@ -53,7 +53,7 @@ end
53
53
 
54
54
  ### single mode and individual workers in cluster mode
55
55
 
56
- When Puma is run in single mode, these stats ar available at the top level. When Puma is run in cluster mode, these stats are available within the `worker_status` array in a hash labeled `last_status`, in an array of hashes, one hash for each worker.
56
+ When Puma is run in single mode, these stats are available at the top level. When Puma is run in cluster mode, these stats are available within the `worker_status` array in a hash labeled `last_status`, in an array of hashes, one hash for each worker.
57
57
 
58
58
  * backlog: requests that are waiting for an available thread to be available. if this is above 0, you need more capacity [always true?]
59
59
  * running: how many threads are running
data/docs/systemd.md CHANGED
@@ -8,7 +8,7 @@ useful features for running Puma in production.
8
8
  ## Service Configuration
9
9
 
10
10
  Below is a sample puma.service configuration file for systemd, which
11
- can be copied or symlinked to /etc/systemd/system/puma.service, or if
11
+ can be copied or symlinked to `/etc/systemd/system/puma.service`, or if
12
12
  desired, using an application or instance specific name.
13
13
 
14
14
  Note that this uses the systemd preferred "simple" type where the
@@ -29,8 +29,8 @@ typedef void (*field_cb)(struct puma_parser* hp,
29
29
 
30
30
  typedef struct puma_parser {
31
31
  int cs;
32
- size_t body_start;
33
32
  int content_len;
33
+ size_t body_start;
34
34
  size_t nread;
35
35
  size_t mark;
36
36
  size_t field_start;
data/lib/puma.rb CHANGED
@@ -23,6 +23,8 @@ module Puma
23
23
  # not in minissl.rb
24
24
  HAS_SSL = const_defined?(:MiniSSL, false) && MiniSSL.const_defined?(:Engine, false)
25
25
 
26
+ HAS_UNIX_SOCKET = Object.const_defined? :UNIXSocket
27
+
26
28
  if HAS_SSL
27
29
  require 'puma/minissl'
28
30
  else
@@ -37,6 +39,20 @@ module Puma
37
39
  HAS_SSL
38
40
  end
39
41
 
42
+ def self.abstract_unix_socket?
43
+ @abstract_unix ||=
44
+ if HAS_UNIX_SOCKET
45
+ begin
46
+ ::UNIXServer.new("\0puma.temp.unix").close
47
+ true
48
+ rescue ArgumentError # darwin
49
+ false
50
+ end
51
+ else
52
+ false
53
+ end
54
+ end
55
+
40
56
  # @!attribute [rw] stats_object=
41
57
  def self.stats_object=(val)
42
58
  @get_stats = val
data/lib/puma/binder.rb CHANGED
@@ -13,7 +13,7 @@ module Puma
13
13
  require 'puma/minissl'
14
14
  require 'puma/minissl/context_builder'
15
15
 
16
- # Odd bug in 'pure Ruby' nio4r verion 2.5.2, which installs with Ruby 2.3.
16
+ # Odd bug in 'pure Ruby' nio4r version 2.5.2, which installs with Ruby 2.3.
17
17
  # NIO doesn't create any OpenSSL objects, but it rescues an OpenSSL error.
18
18
  # The bug was that it did not require openssl.
19
19
  # @todo remove when Ruby 2.3 support is dropped
@@ -177,11 +177,19 @@ module Puma
177
177
  @listeners << [str, io] if io
178
178
  when "unix"
179
179
  path = "#{uri.host}#{uri.path}".gsub("%20", " ")
180
+ abstract = false
181
+ if str.start_with? 'unix://@'
182
+ raise "OS does not support abstract UNIXSockets" unless Puma.abstract_unix_socket?
183
+ abstract = true
184
+ path = "@#{path}"
185
+ end
180
186
 
181
187
  if fd = @inherited_fds.delete(str)
188
+ @unix_paths << path unless abstract
182
189
  io = inherit_unix_listener path, fd
183
190
  logger.log "* Inherited #{str}"
184
191
  elsif sock = @activated_sockets.delete([ :unix, path ])
192
+ @unix_paths << path unless abstract || File.exist?(path)
185
193
  io = inherit_unix_listener path, sock
186
194
  logger.log "* Activated #{str}"
187
195
  else
@@ -205,6 +213,7 @@ module Puma
205
213
  end
206
214
  end
207
215
 
216
+ @unix_paths << path unless abstract || File.exist?(path)
208
217
  io = add_unix_listener path, umask, mode, backlog
209
218
  logger.log "* #{log_msg} on #{str}"
210
219
  end
@@ -258,14 +267,18 @@ module Puma
258
267
  end
259
268
 
260
269
  # Also close any unused activated sockets
261
- @activated_sockets.each do |key, sock|
262
- logger.log "* Closing unused activated socket: #{key.join ':'}"
263
- begin
264
- sock.close
265
- rescue SystemCallError
270
+ unless @activated_sockets.empty?
271
+ fds = @ios.map(&:to_i)
272
+ @activated_sockets.each do |key, sock|
273
+ next if fds.include? sock.to_i
274
+ logger.log "* Closing unused activated socket: #{key.first}://#{key[1..-1].join ':'}"
275
+ begin
276
+ sock.close
277
+ rescue SystemCallError
278
+ end
279
+ # We have to unlink a unix socket path that's not being used
280
+ File.unlink key[1] if key.first == :unix
266
281
  end
267
- # We have to unlink a unix socket path that's not being used
268
- File.unlink key[1] if key[0] == :unix
269
282
  end
270
283
  end
271
284
 
@@ -351,8 +364,6 @@ module Puma
351
364
  # Tell the server to listen on +path+ as a UNIX domain socket.
352
365
  #
353
366
  def add_unix_listener(path, umask=nil, mode=nil, backlog=1024)
354
- @unix_paths << path unless File.exist? path
355
-
356
367
  # Let anyone connect by default
357
368
  umask ||= 0
358
369
 
@@ -369,8 +380,7 @@ module Puma
369
380
  raise "There is already a server bound to: #{path}"
370
381
  end
371
382
  end
372
-
373
- s = UNIXServer.new(path)
383
+ s = UNIXServer.new path.sub(/\A@/, "\0") # check for abstract UNIXSocket
374
384
  s.listen backlog
375
385
  @ios << s
376
386
  ensure
@@ -389,8 +399,6 @@ module Puma
389
399
  end
390
400
 
391
401
  def inherit_unix_listener(path, fd)
392
- @unix_paths << path unless File.exist? path
393
-
394
402
  s = fd.kind_of?(::TCPServer) ? fd : ::UNIXServer.for_fd(fd)
395
403
 
396
404
  @ios << s
@@ -403,24 +411,24 @@ module Puma
403
411
  end
404
412
 
405
413
  def close_listeners
406
- listeners.each do |l, io|
407
- io.close unless io.closed? # Ruby 2.2 issue
408
- uri = URI.parse(l)
414
+ @listeners.each do |l, io|
415
+ io.close unless io.closed?
416
+ uri = URI.parse l
409
417
  next unless uri.scheme == 'unix'
410
418
  unix_path = "#{uri.host}#{uri.path}"
411
- File.unlink unix_path if unix_paths.include? unix_path
419
+ File.unlink unix_path if @unix_paths.include?(unix_path) && File.exist?(unix_path)
412
420
  end
413
421
  end
414
422
 
415
423
  def redirects_for_restart
416
- redirects = listeners.map { |a| [a[1].to_i, a[1].to_i] }.to_h
424
+ redirects = @listeners.map { |a| [a[1].to_i, a[1].to_i] }.to_h
417
425
  redirects[:close_others] = true
418
426
  redirects
419
427
  end
420
428
 
421
429
  # @version 5.0.0
422
430
  def redirects_for_restart_env
423
- listeners.each_with_object({}).with_index do |(listen, memo), i|
431
+ @listeners.each_with_object({}).with_index do |(listen, memo), i|
424
432
  memo["PUMA_INHERIT_#{i}"] = "#{listen[1].to_i}:#{listen[0]}"
425
433
  end
426
434
  end
data/lib/puma/client.rb CHANGED
@@ -126,7 +126,7 @@ module Puma
126
126
  @parsed_bytes = 0
127
127
  @ready = false
128
128
  @body_remain = 0
129
- @peerip = nil
129
+ @peerip = nil if @remote_addr_header
130
130
  @in_last_chunk = false
131
131
 
132
132
  if @buffer
@@ -295,6 +295,7 @@ module Puma
295
295
 
296
296
  if remain > MAX_BODY
297
297
  @body = Tempfile.new(Const::PUMA_TMP_BASE)
298
+ @body.unlink
298
299
  @body.binmode
299
300
  @tempfile = @body
300
301
  else
@@ -386,6 +387,7 @@ module Puma
386
387
  @prev_chunk = ""
387
388
 
388
389
  @body = Tempfile.new(Const::PUMA_TMP_BASE)
390
+ @body.unlink
389
391
  @body.binmode
390
392
  @tempfile = @body
391
393
  @chunked_content_length = 0
data/lib/puma/cluster.rb CHANGED
@@ -43,6 +43,7 @@ module Puma
43
43
  end
44
44
 
45
45
  def start_phased_restart
46
+ @events.fire_on_restart!
46
47
  @phase += 1
47
48
  log "- Starting phased worker restart, phase: #{@phase}"
48
49
 
@@ -317,7 +318,7 @@ module Puma
317
318
 
318
319
  stop_workers
319
320
  stop
320
-
321
+ @events.fire_on_stopped!
321
322
  raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm]
322
323
  exit 0 # Clean exit, workers were stopped
323
324
  end
@@ -332,16 +333,22 @@ module Puma
332
333
  # This is aligned with the output from Runner, see Runner#output_header
333
334
  log "* Workers: #{@options[:workers]}"
334
335
 
335
- # Threads explicitly marked as fork safe will be ignored.
336
- # Used in Rails, but may be used by anyone.
337
- before = Thread.list.reject { |t| t.thread_variable_get(:fork_safe) }
338
-
339
336
  if preload?
337
+ # Threads explicitly marked as fork safe will be ignored. Used in Rails,
338
+ # but may be used by anyone. Note that we need to explicit
339
+ # Process::Waiter check here because there's a bug in Ruby 2.6 and below
340
+ # where calling thread_variable_get on a Process::Waiter will segfault.
341
+ # We can drop that clause once those versions of Ruby are no longer
342
+ # supported.
343
+ fork_safe = ->(t) { !t.is_a?(Process::Waiter) && t.thread_variable_get(:fork_safe) }
344
+
345
+ before = Thread.list.reject(&fork_safe)
346
+
340
347
  log "* Restarts: (\u2714) hot (\u2716) phased"
341
348
  log "* Preloading application"
342
349
  load_and_bind
343
350
 
344
- after = Thread.list.reject { |t| t.thread_variable_get(:fork_safe) }
351
+ after = Thread.list.reject(&fork_safe)
345
352
 
346
353
  if after.size > before.size
347
354
  threads = (after - before)
@@ -382,6 +389,8 @@ module Puma
382
389
 
383
390
  log "Use Ctrl-C to stop"
384
391
 
392
+ single_worker_warning
393
+
385
394
  redirect_io
386
395
 
387
396
  Plugins.fire_background
@@ -403,12 +412,16 @@ module Puma
403
412
 
404
413
  begin
405
414
  booted = false
415
+ in_phased_restart = false
416
+ workers_not_booted = @options[:workers]
406
417
 
407
418
  while @status == :run
408
419
  begin
409
420
  if @phased_restart
410
421
  start_phased_restart
411
422
  @phased_restart = false
423
+ in_phased_restart = true
424
+ workers_not_booted = @options[:workers]
412
425
  end
413
426
 
414
427
  check_workers
@@ -434,8 +447,9 @@ module Puma
434
447
  case req
435
448
  when "b"
436
449
  w.boot!
437
- log "- Worker #{w.index} (PID: #{pid}) booted, phase: #{w.phase}"
450
+ log "- Worker #{w.index} (PID: #{pid}) booted in #{w.uptime.round(2)}s, phase: #{w.phase}"
438
451
  @next_check = Time.now
452
+ workers_not_booted -= 1
439
453
  when "e"
440
454
  # external term, see worker method, Signal.trap "SIGTERM"
441
455
  w.instance_variable_set :@term, true
@@ -453,6 +467,10 @@ module Puma
453
467
  log "! Out-of-sync worker list, no #{pid} worker"
454
468
  end
455
469
  end
470
+ if in_phased_restart && workers_not_booted.zero?
471
+ @events.fire_on_booted!
472
+ in_phased_restart = false
473
+ end
456
474
 
457
475
  rescue Interrupt
458
476
  @status = :stop
@@ -470,6 +488,15 @@ module Puma
470
488
 
471
489
  private
472
490
 
491
+ def single_worker_warning
492
+ return if @options[:workers] != 1 || @options[:silence_single_worker_warning]
493
+
494
+ log "! WARNING: Detected running cluster mode with 1 worker."
495
+ log "! Running Puma in cluster mode with a single worker is often a misconfiguration."
496
+ log "! Consider running Puma in single-mode (workers = 0) in order to reduce memory overhead."
497
+ log "! Set the `silence_single_worker_warning` option to silence this warning message."
498
+ end
499
+
473
500
  # loops thru @workers, removing workers that exited, and calling
474
501
  # `#term` if needed
475
502
  def wait_workers
@@ -499,7 +526,12 @@ module Puma
499
526
  def timeout_workers
500
527
  @workers.each do |w|
501
528
  if !w.term? && w.ping_timeout <= Time.now
502
- log "! Terminating timed out worker: #{w.pid}"
529
+ details = if w.booted?
530
+ "(worker failed to check in within #{@options[:worker_timeout]} seconds)"
531
+ else
532
+ "(worker failed to boot within #{@options[:worker_boot_timeout]} seconds)"
533
+ end
534
+ log "! Terminating timed out worker #{details}: #{w.pid}"
503
535
  w.kill
504
536
  end
505
537
  end
@@ -31,6 +31,10 @@ module Puma
31
31
  @stage == :booted
32
32
  end
33
33
 
34
+ def uptime
35
+ Time.now - started_at
36
+ end
37
+
34
38
  def boot!
35
39
  @last_checkin = Time.now
36
40
  @stage = :booted
@@ -193,6 +193,7 @@ module Puma
193
193
  :debug => false,
194
194
  :binds => ["tcp://#{DefaultTCPHost}:#{DefaultTCPPort}"],
195
195
  :workers => Integer(ENV['WEB_CONCURRENCY'] || 0),
196
+ :silence_single_worker_warning => false,
196
197
  :mode => :http,
197
198
  :worker_timeout => DefaultWorkerTimeout,
198
199
  :worker_boot_timeout => DefaultWorkerTimeout,
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 = "5.2.2".freeze
104
- CODE_NAME = "Fettisdagsbulle".freeze
103
+ PUMA_VERSION = VERSION = "5.3.0".freeze
104
+ CODE_NAME = "Sweetnighter".freeze
105
105
 
106
106
  PUMA_SERVER_STRING = ['puma', PUMA_VERSION, CODE_NAME].join(' ').freeze
107
107
 
@@ -235,7 +235,7 @@ module Puma
235
235
 
236
236
  EARLY_HINTS = "rack.early_hints".freeze
237
237
 
238
- # Mininum interval to checks worker health
238
+ # Minimum interval to checks worker health
239
239
  WORKER_CHECK_INTERVAL = 5
240
240
 
241
241
  # Illegal character in the key or value of response header
@@ -176,7 +176,9 @@ module Puma
176
176
  when 'tcp'
177
177
  TCPSocket.new uri.host, uri.port
178
178
  when 'unix'
179
- UNIXSocket.new "#{uri.host}#{uri.path}"
179
+ # check for abstract UNIXSocket
180
+ UNIXSocket.new(@control_url.start_with?('unix://@') ?
181
+ "\0#{uri.host}#{uri.path}" : "#{uri.host}#{uri.path}")
180
182
  else
181
183
  raise "Invalid scheme: #{uri.scheme}"
182
184
  end
data/lib/puma/dsl.rb CHANGED
@@ -482,6 +482,24 @@ module Puma
482
482
  @options[:workers] = count.to_i
483
483
  end
484
484
 
485
+ # Disable warning message when running in cluster mode with a single worker.
486
+ #
487
+ # Cluster mode has some overhead of running an additional 'control' process
488
+ # in order to manage the cluster. If only running a single worker it is
489
+ # likely not worth paying that overhead vs running in single mode with
490
+ # additional threads instead.
491
+ #
492
+ # There are some scenarios where running cluster mode with a single worker
493
+ # may still be warranted and valid under certain deployment scenarios, see
494
+ # https://github.com/puma/puma/issues/2534
495
+ #
496
+ # Moving from workers = 1 to workers = 0 will save 10-30% of memory use.
497
+ #
498
+ # @note Cluster mode only.
499
+ def silence_single_worker_warning
500
+ @options[:silence_single_worker_warning] = true
501
+ end
502
+
485
503
  # Code to run immediately before master process
486
504
  # forks workers (once on boot). These hooks can block if necessary
487
505
  # to wait for background operations unknown to Puma to finish before
@@ -23,7 +23,7 @@ module Puma
23
23
  new $stderr
24
24
  end
25
25
 
26
- # Print occured error details.
26
+ # Print occurred error details.
27
27
  # +options+ hash with additional options:
28
28
  # - +error+ is an exception object
29
29
  # - +req+ the http request
@@ -34,7 +34,7 @@ module Puma
34
34
  log title(options)
35
35
  end
36
36
 
37
- # Print occured error details only if
37
+ # Print occurred error details only if
38
38
  # environment variable PUMA_DEBUG is defined.
39
39
  # +options+ hash with additional options:
40
40
  # - +error+ is an exception object
@@ -5,22 +5,22 @@ module Puma
5
5
  # Add a simple implementation for earlier Ruby versions.
6
6
  #
7
7
  module QueueClose
8
- def initialize
9
- @closed = false
10
- super
11
- end
12
8
  def close
9
+ num_waiting.times {push nil}
13
10
  @closed = true
14
11
  end
15
12
  def closed?
16
- @closed
13
+ @closed ||= false
17
14
  end
18
15
  def push(object)
19
- @closed ||= false
20
- raise ClosedQueueError if @closed
16
+ raise ClosedQueueError if closed?
21
17
  super
22
18
  end
23
19
  alias << push
20
+ def pop(non_block=false)
21
+ return nil if !non_block && closed? && empty?
22
+ super
23
+ end
24
24
  end
25
25
  ::Queue.prepend QueueClose
26
26
  end
data/lib/puma/request.rb CHANGED
@@ -148,8 +148,9 @@ module Puma
148
148
  res_body.each do |part|
149
149
  next if part.bytesize.zero?
150
150
  if chunked
151
- str = part.bytesize.to_s(16) << line_ending << part << line_ending
152
- fast_write io, str
151
+ fast_write io, (part.bytesize.to_s(16) << line_ending)
152
+ fast_write io, part # part may have different encoding
153
+ fast_write io, line_ending
153
154
  else
154
155
  fast_write io, part
155
156
  end
@@ -230,7 +231,11 @@ module Puma
230
231
  #
231
232
  def normalize_env(env, client)
232
233
  if host = env[HTTP_HOST]
233
- if colon = host.index(":")
234
+ # host can be a hostname, ipv4 or bracketed ipv6. Followed by an optional port.
235
+ if colon = host.rindex("]:") # IPV6 with port
236
+ env[SERVER_NAME] = host[0, colon+1]
237
+ env[SERVER_PORT] = host[colon+2, host.bytesize]
238
+ elsif !host.start_with?("[") && colon = host.index(":") # not hostname or IPV4 with port
234
239
  env[SERVER_NAME] = host[0, colon]
235
240
  env[SERVER_PORT] = host[colon+1, host.bytesize]
236
241
  else
data/lib/puma/server.rb CHANGED
@@ -137,8 +137,6 @@ module Puma
137
137
  # socket parameter may be an MiniSSL::Socket, so use to_io
138
138
  #
139
139
  if tcp_cork_supported?
140
- UNPACK_TCP_STATE_FROM_TCP_INFO = "C".freeze
141
-
142
140
  # 6 == Socket::IPPROTO_TCP
143
141
  # 3 == TCP_CORK
144
142
  # 1/0 == turn on/off
@@ -168,12 +166,14 @@ module Puma
168
166
  end
169
167
 
170
168
  if closed_socket_supported?
169
+ UNPACK_TCP_STATE_FROM_TCP_INFO = "C".freeze
170
+
171
171
  def closed_socket?(socket)
172
- return false unless socket.kind_of? TCPSocket
173
- return false unless @precheck_closing
172
+ skt = socket.to_io
173
+ return false unless skt.kind_of?(TCPSocket) && @precheck_closing
174
174
 
175
175
  begin
176
- tcp_info = socket.getsockopt(Socket::IPPROTO_TCP, Socket::TCP_INFO)
176
+ tcp_info = skt.getsockopt(Socket::IPPROTO_TCP, Socket::TCP_INFO)
177
177
  rescue IOError, SystemCallError
178
178
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
179
179
  @precheck_closing = false
@@ -295,6 +295,9 @@ module Puma
295
295
  @thread_pool << client
296
296
  elsif shutdown || client.timeout == 0
297
297
  client.timeout!
298
+ else
299
+ client.set_timeout(@first_data_timeout)
300
+ false
298
301
  end
299
302
  rescue StandardError => e
300
303
  client_error(e, client)
@@ -308,6 +311,7 @@ module Puma
308
311
  sockets = [check] + @binder.ios
309
312
  pool = @thread_pool
310
313
  queue_requests = @queue_requests
314
+ drain = @options[:drain_on_shutdown] ? 0 : nil
311
315
 
312
316
  remote_addr_value = nil
313
317
  remote_addr_header = nil
@@ -319,22 +323,23 @@ module Puma
319
323
  remote_addr_header = @options[:remote_address_header]
320
324
  end
321
325
 
322
- while @status == :run
326
+ while @status == :run || (drain && shutting_down?)
323
327
  begin
324
- ios = IO.select sockets
328
+ ios = IO.select sockets, nil, nil, (shutting_down? ? 0 : nil)
329
+ break unless ios
325
330
  ios.first.each do |sock|
326
331
  if sock == check
327
332
  break if handle_check
328
333
  else
329
334
  pool.wait_until_not_full
330
- pool.wait_for_less_busy_worker(
331
- @options[:wait_for_less_busy_worker].to_f)
335
+ pool.wait_for_less_busy_worker(@options[:wait_for_less_busy_worker])
332
336
 
333
337
  io = begin
334
338
  sock.accept_nonblock
335
339
  rescue IO::WaitReadable
336
340
  next
337
341
  end
342
+ drain += 1 if shutting_down?
338
343
  client = Client.new io, @binder.env(sock)
339
344
  if remote_addr_value
340
345
  client.peerip = remote_addr_value
@@ -349,6 +354,7 @@ module Puma
349
354
  end
350
355
  end
351
356
 
357
+ @events.debug "Drained #{drain} additional connections." if drain
352
358
  @events.fire :state, @status
353
359
 
354
360
  if queue_requests
@@ -526,7 +532,8 @@ module Puma
526
532
  end
527
533
 
528
534
  if @leak_stack_on_error
529
- [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
535
+ backtrace = e.backtrace.nil? ? '<no backtrace available>' : e.backtrace.join("\n")
536
+ [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{backtrace}"]]
530
537
  else
531
538
  [status, {}, ["An unhandled lowlevel error occurred. The application logs may have details.\n"]]
532
539
  end
@@ -550,28 +557,6 @@ module Puma
550
557
  $stdout.syswrite "#{pid}: === End thread backtrace dump ===\n"
551
558
  end
552
559
 
553
- if @options[:drain_on_shutdown]
554
- count = 0
555
-
556
- while true
557
- ios = IO.select @binder.ios, nil, nil, 0
558
- break unless ios
559
-
560
- ios.first.each do |sock|
561
- begin
562
- if io = sock.accept_nonblock
563
- count += 1
564
- client = Client.new io, @binder.env(sock)
565
- @thread_pool << client
566
- end
567
- rescue SystemCallError
568
- end
569
- end
570
- end
571
-
572
- @events.debug "Drained #{count} additional connections."
573
- end
574
-
575
560
  if @status != :restart
576
561
  @binder.close
577
562
  end
@@ -13,7 +13,7 @@ module Puma
13
13
  # a thread pool via the `Puma::ThreadPool#<<` operator where it is stored in a `@todo` array.
14
14
  #
15
15
  # Each thread in the pool has an internal loop where it pulls a request from the `@todo` array
16
- # and proceses it.
16
+ # and processes it.
17
17
  class ThreadPool
18
18
  class ForceShutdown < RuntimeError
19
19
  end
@@ -220,7 +220,7 @@ module Puma
220
220
  # then the `@todo` array would stay the same size as the reactor works
221
221
  # to try to buffer the request. In that scenario the next call to this
222
222
  # method would not block and another request would be added into the reactor
223
- # by the server. This would continue until a fully bufferend request
223
+ # by the server. This would continue until a fully buffered request
224
224
  # makes it through the reactor and can then be processed by the thread pool.
225
225
  def wait_until_not_full
226
226
  with_mutex do
@@ -240,11 +240,12 @@ module Puma
240
240
 
241
241
  # @version 5.0.0
242
242
  def wait_for_less_busy_worker(delay_s)
243
+ return unless delay_s && delay_s > 0
244
+
243
245
  # Ruby MRI does GVL, this can result
244
246
  # in processing contention when multiple threads
245
247
  # (requests) are running concurrently
246
248
  return unless Puma.mri?
247
- return unless delay_s > 0
248
249
 
249
250
  with_mutex do
250
251
  return if @shutdown
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puma
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.2
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Phoenix
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-02 00:00:00.000000000 Z
11
+ date: 2021-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r