dead_bro 0.2.41 → 0.2.42

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5104a2ccbe8f37e739b8afea8ef9484cd4bcdbbb25faf186162ad64fef738e9
4
- data.tar.gz: f045de2f9295c82f4627b03f2ffeab694bb2d0c2d1595e2c67b2a0c63bad8a03
3
+ metadata.gz: 57cca0618ac0878fd8494b88720c8b1912215061800412f4afb6c302938fe39c
4
+ data.tar.gz: 51f7d737ac01a8e06a1ab9b2161236455ee2179176f04029e28839347a93c500
5
5
  SHA512:
6
- metadata.gz: c4d754d6913e744f5a371c986f79afc312b1d6a0da6aba00e0e6a60884688242615694b777a4a2da648689f6144f925c8fd681c8172ddb6f00a72d53732cce38
7
- data.tar.gz: c0a63e81986773bed9f499a073880cf6fb7b9f25adbb38f4ba6a5ecd6d004b8982d0b318319e6787fd27f25bbd2a9d05dbb19768dd33f93938b419decfcde99a
6
+ metadata.gz: 4172f48d98db35973dbcbbfc4a9888ff6a0930b21c8f6e2e6e1a64bbcd9a2202f1e568fd7784d76811951ac627ef8b41e0412d4d37a6ffd964bbea57e70f0c1a
7
+ data.tar.gz: 2ab4d0f18724083e0ea8582249a1dcd3e9c2a750142c353f45b1a81af2c973954cf80eaa30e809ab0f7c99d8f6eda45de03a643171ae5ee336229d504f1d7e72
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.42] - 2026-12-01
4
+
5
+ ### Fixed
6
+ - **An upload to DeadBro could spin forever at 100% CPU.** If the descriptor number of an upload's socket was closed and handed to another file while the upload waited for a response, Net::HTTP's read loop never ended: each wait for data returned at once, so `read_timeout`, which limits each wait rather than the whole response, never fired. The dispatcher thread then held a core and the GVL until the process restarted. Each upload now has an overall deadline of `open_timeout + read_timeout + 15` seconds. The deadline covers only the POST to DeadBro, so requests and jobs that take longer than that are tracked as before.
7
+ - **A stuck dispatcher worker is replaced.** A worker still on one job after 120 seconds is killed and a new one started, so uploads keep going even if a job outlives its deadline. `DeadBro::Dispatcher.instance.stuck_count` counts the replacements.
8
+
9
+ ### Changed
10
+ - **No uploads near the open-file limit.** While the process has 90% or more of its open-file limit (`ulimit -n`) in use, DeadBro skips its uploads instead of opening sockets the app needs. The check runs at most every 5 seconds. Where the open descriptors or the limit can't be read, uploads go out as before.
11
+
3
12
  ## [0.2.41] - 2026-11-29
4
13
 
5
14
  ### Changed
@@ -7,6 +7,14 @@ require "timeout"
7
7
 
8
8
  module DeadBro
9
9
  class Client
10
+ # Added to open_timeout + read_timeout to give an upload its overall
11
+ # deadline. read_timeout limits each wait for data, not the upload as a
12
+ # whole: if the socket's descriptor number ends up pointing at a regular
13
+ # file, every wait returns at once and Net::HTTP's read loop never ends.
14
+ # The padding leaves room for a large payload (a super sample) on a slow
15
+ # link. It bounds only the POST to DeadBro, never the request being tracked.
16
+ UPLOAD_DEADLINE_PADDING_SECONDS = 15
17
+
10
18
  def initialize(configuration = DeadBro.configuration)
11
19
  @configuration = configuration
12
20
  @circuit_breaker = create_circuit_breaker
@@ -65,6 +73,7 @@ module DeadBro
65
73
  # applied before the first collection tick.
66
74
  json = serialize(body)
67
75
  return unless json
76
+ return if DeadBro::FdPressure.high?
68
77
 
69
78
  uri = URI.parse(metrics_endpoint_url)
70
79
  http = Net::HTTP.new(uri.host, uri.port)
@@ -151,6 +160,8 @@ module DeadBro
151
160
  def dispatch_request(url:, body:, event_name:, apply_settings: false, json: nil)
152
161
  json ||= serialize(body)
153
162
  return unless json
163
+ # Don't open a socket for ourselves while the host is short of files.
164
+ return if DeadBro::FdPressure.high?
154
165
 
155
166
  uri = URI.parse(url)
156
167
  http = Net::HTTP.new(uri.host, uri.port)
@@ -197,7 +208,7 @@ module DeadBro
197
208
  end
198
209
 
199
210
  def perform_request(http, request, event_name:, apply_settings: false)
200
- response = http.request(request)
211
+ response = Timeout.timeout(upload_deadline_seconds) { http.request(request) }
201
212
 
202
213
  if response
203
214
  if @circuit_breaker && @configuration.circuit_breaker_enabled
@@ -234,6 +245,10 @@ module DeadBro
234
245
  nil
235
246
  end
236
247
 
248
+ def upload_deadline_seconds
249
+ @configuration.open_timeout.to_f + @configuration.read_timeout.to_f + UPLOAD_DEADLINE_PADDING_SECONDS
250
+ end
251
+
237
252
  def apply_settings_from_response(response)
238
253
  return unless response.is_a?(Net::HTTPSuccess)
239
254
 
@@ -10,6 +10,11 @@ module DeadBro
10
10
  DEFAULT_QUEUE_SIZE = 500
11
11
  DEFAULT_WORKERS = 2
12
12
  SHUTDOWN = Object.new
13
+ # A worker still on one job after this long is killed and replaced. Each
14
+ # upload already has its own deadline (Client::UPLOAD_DEADLINE_PADDING_SECONDS);
15
+ # this is the backstop for a job that outlives it anyway.
16
+ STUCK_JOB_SECONDS = 120
17
+ JOB_STARTED_AT = :dead_bro_job_started_at
13
18
 
14
19
  class << self
15
20
  def instance
@@ -33,6 +38,7 @@ module DeadBro
33
38
  @worker_count = workers
34
39
  @mutex = Mutex.new
35
40
  @dropped = 0
41
+ @stuck = 0
36
42
  @shutting_down = false
37
43
  boot_workers(Process.pid)
38
44
  install_at_exit_hook
@@ -65,6 +71,11 @@ module DeadBro
65
71
  @mutex.synchronize { @dropped }
66
72
  end
67
73
 
74
+ # Workers killed and replaced because a job ran past STUCK_JOB_SECONDS.
75
+ def stuck_count
76
+ @mutex.synchronize { @stuck }
77
+ end
78
+
68
79
  def shutdown
69
80
  return if @shutting_down
70
81
  @shutting_down = true
@@ -88,28 +99,61 @@ module DeadBro
88
99
  def boot_workers(pid)
89
100
  @pid = pid
90
101
  @queue = SizedQueue.new(@queue_size)
91
- @workers = Array.new(@worker_count) do
92
- t = Thread.new { run }
93
- begin
94
- t.name = "dead_bro-dispatcher"
95
- rescue
96
- end
97
- t.abort_on_exception = false
98
- t
102
+ @workers = Array.new(@worker_count) { start_worker }
103
+ end
104
+
105
+ def start_worker
106
+ t = Thread.new { run }
107
+ begin
108
+ t.name = "dead_bro-dispatcher"
109
+ rescue
99
110
  end
111
+ t.abort_on_exception = false
112
+ t
100
113
  end
101
114
 
115
+ # Runs on every dispatch, so it doubles as the watchdog: a worker stuck on
116
+ # one job is noticed the next time the host sends anything.
102
117
  def ensure_workers_alive!
103
- return if @pid == Process.pid && @workers && @workers.all?(&:alive?)
118
+ return if pool_healthy?
104
119
 
105
120
  @mutex.synchronize do
106
- return if @pid == Process.pid && @workers && @workers.all?(&:alive?)
107
- # Post-fork (new PID) or a worker died — bring up a fresh pool.
108
- boot_workers(Process.pid)
121
+ return if pool_healthy?
122
+
123
+ if @pid != Process.pid || @workers.nil?
124
+ # Post-fork (new PID) — bring up a fresh pool.
125
+ boot_workers(Process.pid)
126
+ else
127
+ @workers = @workers.map do |t|
128
+ if !t.alive?
129
+ start_worker
130
+ elsif stuck?(t)
131
+ @stuck += 1
132
+ begin
133
+ t.kill
134
+ rescue
135
+ end
136
+ start_worker
137
+ else
138
+ t
139
+ end
140
+ end
141
+ end
109
142
  @shutting_down = false
110
143
  end
111
144
  end
112
145
 
146
+ def pool_healthy?
147
+ @pid == Process.pid && @workers && @workers.all? { |t| t.alive? && !stuck?(t) }
148
+ end
149
+
150
+ def stuck?(thread)
151
+ started_at = thread.thread_variable_get(JOB_STARTED_AT)
152
+ return false unless started_at
153
+
154
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at > STUCK_JOB_SECONDS
155
+ end
156
+
113
157
  def install_at_exit_hook
114
158
  at_exit { shutdown }
115
159
  rescue
@@ -120,9 +164,12 @@ module DeadBro
120
164
  job = @queue.pop
121
165
  break if job.equal?(SHUTDOWN)
122
166
  begin
167
+ Thread.current.thread_variable_set(JOB_STARTED_AT, Process.clock_gettime(Process::CLOCK_MONOTONIC))
123
168
  job.call
124
169
  rescue
125
170
  # Never let a job crash the worker.
171
+ ensure
172
+ Thread.current.thread_variable_set(JOB_STARTED_AT, nil)
126
173
  end
127
174
  end
128
175
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadBro
4
+ # Tells whether the process is close to its open-file limit, so DeadBro can
5
+ # stop opening sockets for its own uploads while the host app needs every
6
+ # file descriptor it has left. Near the limit, a socket closed and reopened
7
+ # under load can end up sharing a descriptor number with another file.
8
+ module FdPressure
9
+ HIGH_RATIO = 0.9
10
+ CACHE_SECONDS = 5.0
11
+ FD_DIRS = ["/proc/self/fd", "/dev/fd"].freeze
12
+
13
+ @mutex = Mutex.new
14
+ @cached = nil # [high?, checked_at_monotonic]
15
+
16
+ class << self
17
+ def high?
18
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
19
+ cached = @cached
20
+ return cached[0] if cached && (now - cached[1]) < CACHE_SECONDS
21
+
22
+ value = compute
23
+ @mutex.synchronize { @cached = [value, now] }
24
+ value
25
+ rescue
26
+ false
27
+ end
28
+
29
+ def reset!
30
+ @mutex.synchronize { @cached = nil }
31
+ end
32
+
33
+ # Descriptors open in this process, or nil where that can't be read.
34
+ def open_fd_count
35
+ dir = FD_DIRS.find { |d| File.directory?(d) }
36
+ return nil unless dir
37
+
38
+ # Listing the directory opens one descriptor itself; don't count it.
39
+ Dir.children(dir).size - 1
40
+ end
41
+
42
+ private
43
+
44
+ def compute
45
+ limit = soft_limit
46
+ return false unless limit
47
+
48
+ count = begin
49
+ open_fd_count
50
+ rescue Errno::EMFILE, Errno::ENFILE
51
+ # Not even one descriptor left to list the others with.
52
+ return true
53
+ end
54
+ return false unless count
55
+
56
+ count >= limit * HIGH_RATIO
57
+ end
58
+
59
+ def soft_limit
60
+ soft, _hard = Process.getrlimit(:NOFILE)
61
+ return nil if soft.nil? || soft <= 0 || soft == Process::RLIM_INFINITY
62
+
63
+ soft
64
+ rescue NotImplementedError, ArgumentError, SystemCallError, NameError
65
+ nil
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeadBro
4
- VERSION = "0.2.41"
4
+ VERSION = "0.2.42"
5
5
  end
data/lib/dead_bro.rb CHANGED
@@ -6,6 +6,7 @@ module DeadBro
6
6
  autoload :Configuration, "dead_bro/configuration"
7
7
  autoload :Client, "dead_bro/client"
8
8
  autoload :Dispatcher, "dead_bro/dispatcher"
9
+ autoload :FdPressure, "dead_bro/fd_pressure"
9
10
  autoload :CircuitBreaker, "dead_bro/circuit_breaker"
10
11
  autoload :Collectors, "dead_bro/collectors"
11
12
  autoload :Subscriber, "dead_bro/subscriber"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dead_bro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.41
4
+ version: 0.2.42
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emanuel Comsa
@@ -40,6 +40,7 @@ files:
40
40
  - lib/dead_bro/dispatcher.rb
41
41
  - lib/dead_bro/elasticsearch_subscriber.rb
42
42
  - lib/dead_bro/error_middleware.rb
43
+ - lib/dead_bro/fd_pressure.rb
43
44
  - lib/dead_bro/gc_tracker.rb
44
45
  - lib/dead_bro/http_instrumentation.rb
45
46
  - lib/dead_bro/job_sql_tracking_middleware.rb