puma 5.2.1 → 5.3.1

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: d702fffe078664ed49a411a31910737455c819d6acadd3875c89e00368f68dd8
4
- data.tar.gz: 972f53b07a05363ff839b6f176150bdf4cbc19f7445601ad10f41e9bcaf73af2
3
+ metadata.gz: bcabc9a852bfbed380445b6649ee9e27a2e4741994a27b579e5c4fe94ab6e282
4
+ data.tar.gz: 00ae9ffe82807c9419afaaf955af384f2c87b07914ab36e32c060a83df4075e9
5
5
  SHA512:
6
- metadata.gz: fff3451918f8e34324189c0fa6c161baa1860181920f934541a4db9b5e484d20edae74dd9ac33baa3c14514c75d052cbb5d42539f371bf33cca82e644aa52c57
7
- data.tar.gz: 86c7ba8cb909bedb1dceb259d28677295a71e66b53ed1f1fb6ce324ddab0dbe05c5469aaa120f50f7427cc257d6fa6b6898c7b0346810871f1fceb2ad25bab93
6
+ metadata.gz: 6a6d4ca8d9ffeea284756f5cc6418b0d6649e360ab5b76bec8db8a6bd6d5cf059957725f3ac50949fd15bc1e54f31c6f50b1b5073cef6814dfca042d84cc04e6
7
+ data.tar.gz: f5ad303fd4d4d3256fa0b20a0d608c2cfdea87901c8c56407420078cac5cd69591a686d12153865855532d0702122d34c80bf60ae4aed1f5ec081d20d3414f7e
data/History.md CHANGED
@@ -1,3 +1,42 @@
1
+ ## 5.3.1 / 2021-05-11
2
+
3
+ * Security
4
+ * Close keepalive connections after the maximum number of fast inlined requests (#2625)
5
+
6
+ ## 5.3.0 / 2021-05-07
7
+
8
+ * Features
9
+ * Add support for Linux's abstract sockets ([#2564], [#2526])
10
+ * Add debug to worker timeout and startup ([#2559], [#2528])
11
+ * Print warning when running one-worker cluster ([#2565], [#2534])
12
+ * Don't close systemd activated socket on pumactl restart ([#2563], [#2504])
13
+
14
+ * Bugfixes
15
+ * systemd - fix event firing ([#2591], [#2572])
16
+ * Immediately unlink temporary files ([#2613])
17
+ * Improve parsing of HTTP_HOST header ([#2605], [#2584])
18
+ * Handle fatal error that has no backtrace ([#2607], [#2552])
19
+ * Fix timing out requests too early ([#2606], [#2574])
20
+ * Handle segfault in Ruby 2.6.6 on thread-locals ([#2567], [#2566])
21
+ * Server#closed_socket? - parameter may be a MiniSSL::Socket ([#2596])
22
+ * Define UNPACK_TCP_STATE_FROM_TCP_INFO in the right place ([#2588], [#2556])
23
+ * request.rb - fix chunked assembly for ascii incompatible encodings, add test ([#2585], [#2583])
24
+
25
+ * Performance
26
+ * Reset peerip only if remote_addr_header is set ([#2609])
27
+ * Reduce puma_parser struct size ([#2590])
28
+
29
+ * Refactor
30
+ * Refactor drain on shutdown ([#2600])
31
+ * Micro optimisations in `wait_for_less_busy_worker` feature ([#2579])
32
+ * Lots of test fixes
33
+
34
+ ## 5.2.2 / 2021-02-22
35
+
36
+ * Bugfixes
37
+ * Add `#flush` and `#sync` methods to `Puma::NullIO` ([#2553])
38
+ * Restore `sync=true` on `STDOUT` and `STDERR` streams ([#2557])
39
+
1
40
  ## 5.2.1 / 2021-02-05
2
41
 
3
42
  * Bugfixes
@@ -174,6 +213,11 @@
174
213
  * Support parallel tests in verbose progress reporting ([#2223])
175
214
  * Refactor error handling in server accept loop ([#2239])
176
215
 
216
+ ## 4.3.8 / 2021-05-11
217
+
218
+ * Security
219
+ * Close keepalive connections after the maximum number of fast inlined requests (#2625)
220
+
177
221
  ## 4.3.7 / 2020-11-30
178
222
 
179
223
  * Bugfixes
@@ -1702,6 +1746,36 @@ be added back in a future date when a java Puma::MiniSSL is added.
1702
1746
  * Bugfixes
1703
1747
  * Your bugfix goes here <Most recent on the top, like GitHub> (#Github Number)
1704
1748
 
1749
+ [#2564]:https://github.com/puma/puma/pull/2564 "PR by @MSP-Greg, merged 2021-04-24"
1750
+ [#2526]:https://github.com/puma/puma/issues/2526 "Issue by @nerdrew, closed 2021-04-24"
1751
+ [#2559]:https://github.com/puma/puma/pull/2559 "PR by @ylecuyer, merged 2021-03-11"
1752
+ [#2528]:https://github.com/puma/puma/issues/2528 "Issue by @cjlarose, closed 2021-03-11"
1753
+ [#2565]:https://github.com/puma/puma/pull/2565 "PR by @CGA1123, merged 2021-03-09"
1754
+ [#2534]:https://github.com/puma/puma/issues/2534 "Issue by @nateberkopec, closed 2021-03-09"
1755
+ [#2563]:https://github.com/puma/puma/pull/2563 "PR by @MSP-Greg, merged 2021-03-06"
1756
+ [#2504]:https://github.com/puma/puma/issues/2504 "Issue by @fsateler, closed 2021-03-06"
1757
+ [#2591]:https://github.com/puma/puma/pull/2591 "PR by @MSP-Greg, merged 2021-05-05"
1758
+ [#2572]:https://github.com/puma/puma/issues/2572 "Issue by @josefbilendo, closed 2021-05-05"
1759
+ [#2613]:https://github.com/puma/puma/pull/2613 "PR by @smcgivern, merged 2021-04-27"
1760
+ [#2605]:https://github.com/puma/puma/pull/2605 "PR by @pascalbetz, merged 2021-04-26"
1761
+ [#2584]:https://github.com/puma/puma/issues/2584 "Issue by @kaorihinata, closed 2021-04-26"
1762
+ [#2607]:https://github.com/puma/puma/pull/2607 "PR by @calvinxiao, merged 2021-04-23"
1763
+ [#2552]:https://github.com/puma/puma/issues/2552 "Issue by @feliperaul, opened 2021-02-09"
1764
+ [#2606]:https://github.com/puma/puma/pull/2606 "PR by @wjordan, merged 2021-04-20"
1765
+ [#2574]:https://github.com/puma/puma/issues/2574 "Issue by @darkhelmet, closed 2021-04-20"
1766
+ [#2567]:https://github.com/puma/puma/pull/2567 "PR by @kddeisz, merged 2021-04-19"
1767
+ [#2566]:https://github.com/puma/puma/issues/2566 "Issue by @kddeisz, closed 2021-04-19"
1768
+ [#2596]:https://github.com/puma/puma/pull/2596 "PR by @MSP-Greg, merged 2021-04-18"
1769
+ [#2588]:https://github.com/puma/puma/pull/2588 "PR by @dentarg, merged 2021-04-02"
1770
+ [#2556]:https://github.com/puma/puma/issues/2556 "Issue by @gamecreature, closed 2021-04-02"
1771
+ [#2585]:https://github.com/puma/puma/pull/2585 "PR by @MSP-Greg, merged 2021-03-26"
1772
+ [#2583]:https://github.com/puma/puma/issues/2583 "Issue by @jboler, closed 2021-03-26"
1773
+ [#2609]:https://github.com/puma/puma/pull/2609 "PR by @calvinxiao, merged 2021-04-26"
1774
+ [#2590]:https://github.com/puma/puma/pull/2590 "PR by @calvinxiao, merged 2021-04-05"
1775
+ [#2600]:https://github.com/puma/puma/pull/2600 "PR by @wjordan, merged 2021-04-30"
1776
+ [#2579]:https://github.com/puma/puma/pull/2579 "PR by @ghiculescu, merged 2021-03-17"
1777
+ [#2553]:https://github.com/puma/puma/pull/2553 "PR by @olivierbellone, merged 2021-02-10"
1778
+ [#2557]:https://github.com/puma/puma/pull/2557 "PR by @cjlarose, merged 2021-02-22"
1705
1779
  [#2550]:https://github.com/puma/puma/pull/2550 "PR by @MSP-Greg, merged 2021-02-05"
1706
1780
  [#2547]:https://github.com/puma/puma/pull/2547 "PR by @wildmaples, merged 2021-02-03"
1707
1781
  [#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
@@ -4,34 +4,38 @@
4
4
 
5
5
  ![https://bit.ly/2iJuFky](images/puma-general-arch.png)
6
6
 
7
- Puma is a threaded web server, processing requests across a TCP or UNIX socket.
7
+ Puma is a threaded Ruby HTTP application server, processing requests across a TCP or UNIX socket.
8
8
 
9
- Workers accept connections from the socket and a thread in the worker's thread pool processes the client's request.
10
9
 
11
- Clustered mode is shown/discussed here. Single mode is analogous to having a single worker process.
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).
12
11
 
13
- ## Connection pipeline
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.
13
+
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_.
15
+
16
+ ## How Requests Work
14
17
 
15
18
  ![https://bit.ly/2zwzhEK](images/puma-connection-flow.png)
16
19
 
17
20
  * Upon startup, Puma listens on a TCP or UNIX socket.
18
- * The backlog of this socket is configured (with a default of 1024), determining how many established but unaccepted connections can exist concurrently.
19
- * This socket backlog is distinct from the "backlog" of work as reported by the control server stats. The latter is the number of connections in that worker's "todo" set waiting for a worker thread.
20
- * By default, a single, separate thread is used to receive HTTP requests across the socket.
21
- * When at least one worker thread is available for work, a connection is accepted and placed in this request buffer
22
- * This thread waits for entire HTTP requests to be received over the connection
23
- * 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)
24
- * Once received, the connection is pushed into the "todo" set
25
- * Worker threads pop work off the "todo" set for processing
26
- * The thread processes the request via the rack application (which generates the HTTP response)
27
- * The thread writes the response to the connection
28
- * Finally, the thread become available to process another connection in the "todo" set
29
-
30
- ### Disabling `queue_requests`
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.
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.
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.
25
+ * The reactor thread waits for the entire HTTP request to be received.
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).
27
+ * Once fully buffered and received, the connection is pushed into the "todo" set.
28
+ * Worker threads pop work off the "todo" set for processing.
29
+ * The worker thread processes the request via `call`ing the configured Rack application. The Rack application generates the HTTP response.
30
+ * The worker thread writes the response to the connection. Note that while Puma buffers requests via a separate thread, it does not use a separate thread for responses.
31
+ * Once done, the thread become available to process another connection in the "todo" set.
32
+
33
+ ### `queue_requests`
31
34
 
32
35
  ![https://bit.ly/2zxCJ1Z](images/puma-connection-flow-no-reactor.png)
33
36
 
34
- The `queue_requests` option is `true` by default, enabling the separate thread used to buffer requests as described above.
37
+ The `queue_requests` option is `true` by default, enabling the separate reactor thread used to buffer requests as described above.
35
38
 
36
39
  If set to `false`, this buffer will not be used for connections while waiting for the request to arrive.
40
+
37
41
  In this mode, when a connection is accepted, it is added to the "todo" queue immediately, and a worker will synchronously do any waiting necessary to read the HTTP request from the socket.
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,
@@ -206,7 +207,8 @@ module Puma
206
207
  :first_data_timeout => Const::FIRST_DATA_TIMEOUT,
207
208
  :raise_exception_on_sigterm => true,
208
209
  :max_fast_inline => Const::MAX_FAST_INLINE,
209
- :io_selector_backend => :auto
210
+ :io_selector_backend => :auto,
211
+ :mutate_stdout_and_stderr_to_sync_on_write => true,
210
212
  }
211
213
  end
212
214
 
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.1".freeze
104
- CODE_NAME = "Fettisdagsbulle".freeze
103
+ PUMA_VERSION = VERSION = "5.3.1".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
@@ -888,5 +906,9 @@ module Puma
888
906
  def io_selector_backend(backend)
889
907
  @options[:io_selector_backend] = backend.to_sym
890
908
  end
909
+
910
+ def mutate_stdout_and_stderr_to_sync_on_write(enabled=true)
911
+ @options[:mutate_stdout_and_stderr_to_sync_on_write] = enabled
912
+ end
891
913
  end
892
914
  end
@@ -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
data/lib/puma/null_io.rb CHANGED
@@ -36,6 +36,10 @@ module Puma
36
36
  true
37
37
  end
38
38
 
39
+ def sync
40
+ true
41
+ end
42
+
39
43
  def sync=(v)
40
44
  end
41
45
 
@@ -44,5 +48,9 @@ module Puma
44
48
 
45
49
  def write(*ary)
46
50
  end
51
+
52
+ def flush
53
+ self
54
+ end
47
55
  end
48
56
  end
@@ -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/runner.rb CHANGED
@@ -126,6 +126,11 @@ module Puma
126
126
  STDERR.puts "=== puma startup: #{Time.now} ==="
127
127
  STDERR.flush unless STDERR.sync
128
128
  end
129
+
130
+ if @options[:mutate_stdout_and_stderr_to_sync_on_write]
131
+ STDOUT.sync = true
132
+ STDERR.sync = true
133
+ end
129
134
  end
130
135
 
131
136
  def load_and_bind
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
@@ -441,15 +447,20 @@ module Puma
441
447
 
442
448
  requests += 1
443
449
 
444
- check_for_more_data = @status == :run
450
+ # Closing keepalive sockets after they've made a reasonable
451
+ # number of requests allows Puma to service many connections
452
+ # fairly, even when the number of concurrent connections exceeds
453
+ # the size of the threadpool. It also allows cluster mode Pumas
454
+ # to keep load evenly distributed across workers, because clients
455
+ # are randomly assigned a new worker when opening a new connection.
456
+ #
457
+ # Previously, Puma would kick connections in this conditional back
458
+ # to the reactor. However, because this causes the todo set to increase
459
+ # in size, the wait_until_full mutex would never unlock, leaving
460
+ # any additional connections unserviced.
461
+ break if requests >= @max_fast_inline
445
462
 
446
- if requests >= @max_fast_inline
447
- # This will mean that reset will only try to use the data it already
448
- # has buffered and won't try to read more data. What this means is that
449
- # every client, independent of their request speed, gets treated like a slow
450
- # one once every max_fast_inline requests.
451
- check_for_more_data = false
452
- end
463
+ check_for_more_data = @status == :run
453
464
 
454
465
  next_request_ready = with_force_shutdown(client) do
455
466
  client.reset(check_for_more_data)
@@ -526,7 +537,8 @@ module Puma
526
537
  end
527
538
 
528
539
  if @leak_stack_on_error
529
- [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
540
+ backtrace = e.backtrace.nil? ? '<no backtrace available>' : e.backtrace.join("\n")
541
+ [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{backtrace}"]]
530
542
  else
531
543
  [status, {}, ["An unhandled lowlevel error occurred. The application logs may have details.\n"]]
532
544
  end
@@ -550,28 +562,6 @@ module Puma
550
562
  $stdout.syswrite "#{pid}: === End thread backtrace dump ===\n"
551
563
  end
552
564
 
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
565
  if @status != :restart
576
566
  @binder.close
577
567
  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.1
4
+ version: 5.3.1
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-02-05 00:00:00.000000000 Z
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
142
  requirements: []
143
- rubygems_version: 3.2.1
143
+ rubygems_version: 3.2.3
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for